So far, we have discussed numbers and strings, but the reality in an actual program, we often want
to collect our results and maybe store all of them in one variable.
So of course, we could do this by writing something like, I don't know, a one is equal to one and
then make another one.
A two is equal to five and a three is equal to 10.
But you can see this is really tedious.
We have to continue and we have to define many of these variables, and we'll be a bit difficult to
assess the individual variables because we always have to remember what we named them.
So much better would be to just make a list.
And this is, of course, possible with Python by, for example, defining a variable called List one,
which will be these square brackets.
And then you can just write to individual elements of the list.
For example, these square numbers and what you can also do is you can combine several data types in
a single list.
For example, you have one, then a string, which is a and then another number.
So now we have two lists.
What we can do now is we can select individual elements of this list.
We can, for example, start with list one and then one.
So maybe you are a bit surprised now if you're new to Python, because the result is four, which is
actually the second element of this list.
And the reason for this is that these indices in Python start counting from zero.
So if we write zero, then we actually mean the first element.
If we write one, then we mean the second element.
So this is different in other programming languages, and it can be a bit confusing, but you just have
to get used to it.
This is the way of Python works.
So if we want to have to second element write one now, we can also select multiple elements of the
list at once.
For example, our writing list one.
And then we do not write here only a single value, but two values.
So for example, if we want to have the first two values, you could think that we would have to write
zero to one.
But here it's again a bit confusing, to be honest.
We have to write a zero to two because this means something like start from the index zero, which is
the first element and go until the index is smaller than two, which means only use zero and one and
zero and one correspond to the first and second element.
So I agree this is a bit confusing, but this is what it is.
You have to get used to it, unfortunately.
But once you realize it, it's really not that difficult.
Just have to remember start counting at zero.
And when you want to select several elements, then you have to go one number higher than you actually
want to go.
So the next thing that we can, of course, do is we can manipulate these lists because it would be
very tedious to always redefine the whole list if we want to change something so we can just manipulate
the list.
For example, we could write this one.
And then there's a function called Append.
So we write list one dots append, and then we just write what we want to add to the list.
And the cool thing is that this number is already added to the list.
So we do not have to write something like this one equals list, one appends.
But just this, this is already sufficient and you see the number three has been added to the list.
We can, of course, also remove something from lists.
For example, this time, let's take our lists two.
And let's remove the eight.
So we write list two dots to remove a.
And then I check what it will be.
The result around this and this will give us the results.
The only thing you need to be careful about is that when you run these cells multiple times, for example,
by accident, you run this cell twice and then you have already added the number three twice at the
end of the list.
What we can also do is we can sort the lists by using the command sort and once again, this running
this command will already manipulate the list and sort has some optional arguments that we are not going
to use.
For example, you could select that you want to store that number of First Order.
But we want to we don't want to use any of the optional arguments.
So you just write.
It open bracket close and then it will work, but you need to remember that these brackets here and
then the result will be not one four nine three, but one three four nine.
All right.
So one more thing that we can do when we want to manipulate our lists is to change a single value of
the list.
And what I want to do now is I want to change the number nine to number 10.
So I just write list one.
I want to select the fourth element, which will mean the index has to be three.
Since we're starting counting from zero and this should be 10, then the result will be one three four
10.
Multidimensional lists are also possible, so we can create a new list list three.
And here I just tried to square brackets one to three as an example.
And then here another list which we could use here to square numbers one four nine.
So you see, this is a list consisting of two lists, and now I can run this.
And of course, now I can select individual values of the list.
For example, I could write, I want to take only the zeros element or else that the first element,
which would be this list here.
And then I could also say from this list, I want to take the first element.
So we get the one, this one here, that's all possible.
So now I want to show you why lists are not always perfect because there are some problems with these,
especially if you want to do mathematical operations.
So if you remember all of this one, we, for example, could have the problem or we want to multiply
all the values in the list by a number.
For example, we could then write list times too, and we would think that this would give us two six,
eight 20.
However, if we run this, it doesn't work.
It gives us twice the list and they are merged to a single list.
And the same happens if we
are not the same.
A similar problem happens when we want to add a number.
So here we even get an error because plus like four strings means concatenate, so basically merge lists.
So we tried to merge here this list with a number which is not working because one of these properties
is the list and the other one is just a number.
So it would work if we would write something like this.
But then we would just add another element to our list.
So this is the same as using the append commands.
And this is not really what I had in mind here by adding a number.
So the thing is, lists are really nice to quickly store some, some some variables, some numbers,
some strings, but it's very difficult to do mathematical operations with them and also storing things
in lists requires a whole lot of memory.
So most of the times in our for our purposes lists are not really ideal only if we have a very few elements
that we do not want to do any mathematical calculations with them.
So what we are going to use instead are arrays and these are we'll discuss in the next lecture.