We can now use variables with different data types.
But what if you want, for example, to keep a collection of 100 temperatures?
Are you going to create 100 variables named
"temperature1", "temperature2", "temperature3", etc?
Well, this would be very impractical, super long to
create and prone to a lot of errors and headaches.
So instead, you are going to use lists. A list is simply a collection of data.
Or in other words, a collection of variables which are related to each other.
So let's create a list so you can see what I'm talking about.
So let's name it, we want temperature list, so let's name it "temperature".
List. Okay, make the name meaningful.
Don't try to make it too complicated, okay?
Aim for simplicity. You want a list of temperatures,
this is going to be called "temperature list".
Then you are going to open and close brackets.
And you are going to put the values you want inside this list.
So for example, I'm going to put 12.3, okay?
23.0, and then let's put a negative number also, minus 12.8.
And then another negative number, minus 5.9.
So here I have a list, I'm going to press enter.
I have a list of four different float numbers.
If I just do "temperature list", I can see the list.
So the list is evaluated here and printed on the Python shell.
Now what if I want to retrieve a specific element of the list?
In this case, I'm going to use "temperature list"
and also using directly brackets here and put the index of the element.
Let's start with one.
Okay, you can see actually with one, what I get
is not the first element, but the second element.
Why is that? Because in programming, usually we start to count at zero.
So you can use the up arrow key to come back to the different instructions you have.
Okay, so you don't need to write "temperature list" again.
And then I'm going to put zero instead.
And zero gives me, you can see the first index here.
So this will be zero, one, two, and three.
So let's get each element.
So we have zero, we have one, let's get two, which is the third element.
And let's get three, which is the last element of the list.
Now, let's say I put four.
What is going to happen is that I have an error, a list index out of range error.
Okay, so that's the common error is that you have a list
and you try to access an element which is outside of the list.
Okay, so just remember when you have a list,
the first element starts at zero for the index and then one, two, three, etc.
A nice function you can use to actually get the length of a list is simply the "len",
so L-E-N function, which is a built-in function inside Python.
So you just put "len", you open the parentheses,
and you can put the temperature list.
You can put the name of the list, close the parentheses, and then you get four.
So this gives you the number of elements in the list.
You can see one, two, three, four.
And so this information is very useful because you can see that the last index,
okay, here is three, okay, zero, one, two, three.
So the last index will simply be the length minus one.
And this can help you avoid getting the index error in your program.
Okay, so now we have seen how to get elements from the list.
Now how you can modify an element.
When you simply do "temperature list", you also put, so
let's say zero, I'm going to modify the first element.
So the same as for getting the element, but then you write equal,
and then you can change the value, let's say 2.4.
So now if I try to get the value, you can see this is 2.4.
And if I print the list, the first value is now 2.4.
What you can also do is, if you want to add
another element here at the end of the list,
okay, add a new element, you can simply do "temperature",
so write the name of the list, "temperature list".
And then use the append function like this, "temperature list.append".
And let's say I'm going to add 10.7.
So this is going to add a new element.
Now I go back to "temperature list".
And you can see I have five elements, okay.
And the last one is 10.7.
If I do "len" with "temperature list", now I have five.
So now I will be able to actually, if I do "temperature
list" 4, like this, I will not get an error like here, okay.
I will get the last element because the length of the list is 5.
Okay, so that's pretty much it about lists,
and we are going to continue to use them later on.
So here you have created a list with float numbers.
But now that you can create a list with integer numbers,
you can also create a list containing booleans, and also a list containing strings.
And one recommendation I'm going to give you is to use,
to stick to the same data type for all elements of a list.
Okay, if you mix, let's say you use integer and booleans,
inside the same list, well, you are probably going to have errors in the future.
And also this can kind of break the logic, okay.
If you want a list of numbers,
you are not going to put a string inside the list of numbers.
So stick to one data type for one list.