This is almost the end of the Python programming part of this course, so congratulations if you've
already made it that far.
And after this lesson and the next activity, you will be ready to create programs for Gpio, the camera,
web servers, etc..
So in this lesson I'm going to introduce you to Python lists.
First of all, why do we need lists?
Well, for now you have worked with one value at a time.
What if you need to process ten different values?
Let's say to make a computation on ten different temperatures that you have measured?
And what if it's not ten but 10,000 values?
This can look like a nightmare.
But don't worry, you won't have to create 10,000 variables to manipulate 10,000 values.
You can put all of those values inside only one container, which is called a list.
You may also have heard of arrays.
Well, a list is simply the Python way of saying an array.
So what is a list?
A list is a collection of variables.
Okay, it is very useful when you need to store multiple variables which are related.
Let's create a simple list.
So to create a list you will first to well define a name for that list.
So let's say we want a list that contains numbers I'm going to write for example number underscore list
okay.
As simple as that.
You can put what you store and then underscore list.
Or you could also use array if you want to.
But in Python we use list.
You could also put just the word in plural okay.
So you could have a variable named number and then a list named numbers.
Okay here I'm going to follow this convention.
So I call it number list.
And then you put the equal sign.
And then to create a list you just open and close some brackets like that.
You can create a list with zero elements okay.
Just like this to initialize an empty list.
But here we're going to start with some values.
So for example I'm going to add some integers let's say three.
And then I put a comma to separate different values.
So five I can put negative number.
Okay.
I can put any integers I want.
-856 etc..
So here I only add four values in this list.
And one important thing to note is that all those values should be related okay.
And because they are related they should also be of the same data type.
Okay.
It's important that you follow this.
Don't mix strings with float numbers with integers with booleans okay.
Try to be consistent and use just one data type in the list for all the elements.
Otherwise, you will surely get a lot of errors in the future.
So we have our list.
Just create a name.
So just like a variable, we create a name and equal.
And then we have those brackets with all the elements.
Now if I want to print this list I can just do print number list okay.
Just like I can print a variable I can print a list.
Let's play this.
And you can see I have my list here.
Okay.
So I can just print the entire list.
Okay.
That's great.
But now what if I want to get access.
So by getting access, I mean read one element of the array.
Well, to do that, after you have created the list, of course you can just write the name of the list
followed by some brackets like this and the index of the element.
Okay.
So each element in the list will have an index.
And let's just start with one to see what we have.
So we want to print the index one of the list.
What value will we get.
So you can guess.
And now I'm going to click on play.
And we get the value five.
And the value five is actually the second element of the list.
And why is that.
When we ask for the index one because well in programming and you may be already know that.
And that's what we did also with for loops in programming we start to count at zero okay.
Not one.
So if I ask you to count for like four values you will not do one, two, three, four.
You will do zero, one, two and three.
So if I put zero here zero corresponds to the first element of the list.
Okay that's three.
And then one was this one.
Then I can do two.
Okay.
It's going to be minus eight.
Then I can do three.
It's going to be 156.
And let's try with four.
What's going to happen with four.
You see we get an error.
And this error is a list.
So index error.
List index is out of range okay.
So if you try to access a value here that doesn't exist you will get an error.
So make sure that you always use indexes that are actually corresponding to existing elements in the
list.
So here for example I go back to zero and I print I print the first element.
So that's how you can read an element how you get an element.
Now if you want to set an element.
So if you want to write into the list and change one value, you can do this.
So you just put the list name.
So number list.
And you use the same syntax here with brackets to select an element.
So for example still we want to modify the first one.
So I'm going to put zero.
And then you put an equal sign.
And let's say now I want to write ten okay.
So I want to change the first element instead of three is going to become ten.
And just to make sure let's just print this element again after we modify it okay.
So let's run.
You can see okay I have this error but it's not a problem I have three and then I have ten okay.
So three was the first value.
Then I modify it and then I can print it again.
And it's a different value okay.
And of course same thing.
When you modify an element make sure that you use an index that is inside the list.
Great.
So you can get an element.
You can set an element.
But also in Python lists are dynamic.
So you can also add elements to a list.
You can add a new element.
So here we have the index 0123.
As you can see index four.
We had an error because it doesn't exist.
But you can add a new element.
And then you will have something on the index for how to do this.
Well, you can use the append function.
So you will put the name of the list number list.
And then you will do dot append like this.
You open and close parentheses.
So here make sure to use parentheses okay.
This is a function.
And let's put a new value for example 12 okay.
So what I'm doing here is I'm adding a new element at the end of the list.
Now let's just print the list number list okay.
Let's print that.
You can see that when we print the list here.
So the first element is still ten.
Then we have five minus eight 156.
And we have 12 at the end okay.
So we can add new elements to a list with this function right here okay.
So with this you will be able to do lots of things with lists.
And to finish here I want to show you a great combination you can do with lists and for loops.
Okay.
So now that you have seen for loops we can do that.
So let's say that you want to get access to each element of Off the list.
So I thought to read it or to modify it or to do something.
Else.
But you want to get access to each element one by one.
How to do this?
Well, you're not just going to do number list zero and then number one, number two etc. okay.
You're going to use a for loop.
So using for loops with lists is a very powerful combo in programming.
So let's see how to do that.
We have our for keyword.
And then because we are going through a number list instead of just writing I am going to write number.
And then I have the in keyword.
Okay.
So for variable in and then I don't have a range here I'm not going to provide a range I'm going to
provide the list name.
Okay.
So for number in number list I add a colon I go back to a new line.
And then I know that with this well, the block of code that is indented here is going to be repeated
for each number in the number list.
Okay.
So for each item in the number list for now we have.
So you can see we have 12345 elements.
So this list is going to run five times, and each time the number variable is going to contain the
element.
Okay.
So the first one, the second one, the third one, etc..
So let's just print the number actually to see what we have.
Like this.
I run this and you can see that we print each element one by one.
Okay so what happened is that first this variable.
So this is the local variable inside the for loop is going to be created with the first element of number
list.
So the index zero which is not three anymore.
It's ten because we have modified it here.
Then it goes to the second element the third etc. until it has gone through all elements of the list.
And now let's do something a bit more interesting.
Let's say we want to create a new list that contains the same element as that list, but multiplied
by two.
So for this I will create here new list and I'm going to initialize it to.
Actually I'm just going to write this.
So open and close brackets with nothing inside.
All right, so I create an empty list.
This is a quite common practice to do in Python.
You create an empty list and then you will fill some values in it with a for loop.
So here instead of print I will just do new list dot append.
And what do I add?
I will just add the number times two.
Okay, so I go through the number list here with a for loop.
And for each item of the number list, I add a new element in the new list that corresponds to the number
multiplied by two.
And now if I want to print the new list.
So make sure that you go back you see to the previous indentation here.
Print a new list.
That list should contain five values and you can see those five values are the double of the five values
that we have in the number list.
And we did that with just two lines of code here with this for loop.
And just one thing to note here is related to the scope.
You can see that we create the new list list here in that scope, and then we use it in the for loop.
And we can do that because this is a more nested scope.
You can see we add one indentation.
This is a more nested scope.
So we can use that variable that we've created here.
So let's make a very quick recap.
A list in Python will allow you to store a collection of values inside only one container.
You can put as many values as you want in a list, and to create a list you first need to give it a
meaningful name.
Then the equal sign and you initialize values inside brackets, each value being separated by a comma.
You can also use the append function to add a new element to an existing list, and when you access
an element of a list, make sure you use the correct index and don't go out of the range.
Finally, going through lists with a for loop is something that's very common and that you will use
very often in your programs.