WEBVTT

00:00.080 --> 00:05.840
This is almost the end of the Python programming part of this course, so congratulations if you've

00:05.840 --> 00:07.280
already made it that far.

00:07.280 --> 00:13.100
And after this lesson and the next activity, you will be ready to create programs for Gpio, the camera,

00:13.130 --> 00:14.840
web servers, etc..

00:14.840 --> 00:19.130
So in this lesson I'm going to introduce you to Python lists.

00:19.340 --> 00:21.650
First of all, why do we need lists?

00:21.650 --> 00:25.250
Well, for now you have worked with one value at a time.

00:25.250 --> 00:27.860
What if you need to process ten different values?

00:27.860 --> 00:31.880
Let's say to make a computation on ten different temperatures that you have measured?

00:31.880 --> 00:34.910
And what if it's not ten but 10,000 values?

00:34.910 --> 00:36.620
This can look like a nightmare.

00:36.620 --> 00:42.770
But don't worry, you won't have to create 10,000 variables to manipulate 10,000 values.

00:42.770 --> 00:48.830
You can put all of those values inside only one container, which is called a list.

00:48.860 --> 00:50.930
You may also have heard of arrays.

00:50.930 --> 00:55.220
Well, a list is simply the Python way of saying an array.

00:55.220 --> 00:56.300
So what is a list?

00:56.330 --> 00:58.880
A list is a collection of variables.

00:58.910 --> 01:03.720
Okay, it is very useful when you need to store multiple variables which are related.

01:03.810 --> 01:06.120
Let's create a simple list.

01:06.120 --> 01:10.770
So to create a list you will first to well define a name for that list.

01:10.770 --> 01:18.270
So let's say we want a list that contains numbers I'm going to write for example number underscore list

01:18.300 --> 01:18.720
okay.

01:18.750 --> 01:19.920
As simple as that.

01:19.950 --> 01:24.090
You can put what you store and then underscore list.

01:24.120 --> 01:27.540
Or you could also use array if you want to.

01:27.570 --> 01:29.730
But in Python we use list.

01:29.760 --> 01:32.700
You could also put just the word in plural okay.

01:32.730 --> 01:37.890
So you could have a variable named number and then a list named numbers.

01:38.100 --> 01:39.930
Okay here I'm going to follow this convention.

01:39.930 --> 01:41.520
So I call it number list.

01:41.520 --> 01:42.930
And then you put the equal sign.

01:42.960 --> 01:48.270
And then to create a list you just open and close some brackets like that.

01:48.300 --> 01:51.510
You can create a list with zero elements okay.

01:51.540 --> 01:53.730
Just like this to initialize an empty list.

01:53.730 --> 01:56.610
But here we're going to start with some values.

01:56.610 --> 02:00.360
So for example I'm going to add some integers let's say three.

02:00.360 --> 02:02.850
And then I put a comma to separate different values.

02:02.850 --> 02:06.100
So five I can put negative number.

02:06.130 --> 02:06.310
Okay.

02:06.310 --> 02:07.930
I can put any integers I want.

02:07.960 --> 02:10.990
-856 etc..

02:10.990 --> 02:13.930
So here I only add four values in this list.

02:13.930 --> 02:18.490
And one important thing to note is that all those values should be related okay.

02:18.520 --> 02:22.600
And because they are related they should also be of the same data type.

02:22.630 --> 02:22.810
Okay.

02:22.810 --> 02:24.400
It's important that you follow this.

02:24.400 --> 02:28.870
Don't mix strings with float numbers with integers with booleans okay.

02:28.900 --> 02:34.390
Try to be consistent and use just one data type in the list for all the elements.

02:34.420 --> 02:37.780
Otherwise, you will surely get a lot of errors in the future.

02:37.780 --> 02:39.520
So we have our list.

02:39.700 --> 02:41.440
Just create a name.

02:41.440 --> 02:44.830
So just like a variable, we create a name and equal.

02:44.830 --> 02:48.040
And then we have those brackets with all the elements.

02:48.070 --> 02:52.540
Now if I want to print this list I can just do print number list okay.

02:52.570 --> 02:55.750
Just like I can print a variable I can print a list.

02:55.810 --> 02:57.820
Let's play this.

02:57.910 --> 02:59.920
And you can see I have my list here.

03:00.160 --> 03:00.370
Okay.

03:00.400 --> 03:02.260
So I can just print the entire list.

03:02.290 --> 03:02.530
Okay.

03:02.560 --> 03:02.980
That's great.

03:02.980 --> 03:05.920
But now what if I want to get access.

03:05.920 --> 03:11.070
So by getting access, I mean read one element of the array.

03:11.100 --> 03:17.040
Well, to do that, after you have created the list, of course you can just write the name of the list

03:17.040 --> 03:23.040
followed by some brackets like this and the index of the element.

03:23.070 --> 03:23.280
Okay.

03:23.310 --> 03:26.730
So each element in the list will have an index.

03:26.760 --> 03:29.460
And let's just start with one to see what we have.

03:29.460 --> 03:32.370
So we want to print the index one of the list.

03:32.400 --> 03:34.170
What value will we get.

03:34.200 --> 03:35.190
So you can guess.

03:35.190 --> 03:37.110
And now I'm going to click on play.

03:37.110 --> 03:38.460
And we get the value five.

03:38.460 --> 03:42.210
And the value five is actually the second element of the list.

03:42.210 --> 03:43.410
And why is that.

03:43.440 --> 03:48.450
When we ask for the index one because well in programming and you may be already know that.

03:48.450 --> 03:54.060
And that's what we did also with for loops in programming we start to count at zero okay.

03:54.090 --> 03:54.960
Not one.

03:54.960 --> 04:00.270
So if I ask you to count for like four values you will not do one, two, three, four.

04:00.300 --> 04:02.670
You will do zero, one, two and three.

04:02.670 --> 04:08.620
So if I put zero here zero corresponds to the first element of the list.

04:08.650 --> 04:10.030
Okay that's three.

04:10.030 --> 04:12.070
And then one was this one.

04:12.070 --> 04:13.960
Then I can do two.

04:14.650 --> 04:14.920
Okay.

04:14.950 --> 04:16.570
It's going to be minus eight.

04:16.600 --> 04:18.070
Then I can do three.

04:18.100 --> 04:20.200
It's going to be 156.

04:20.230 --> 04:22.120
And let's try with four.

04:22.150 --> 04:23.830
What's going to happen with four.

04:23.950 --> 04:25.600
You see we get an error.

04:25.600 --> 04:27.370
And this error is a list.

04:27.370 --> 04:28.510
So index error.

04:28.540 --> 04:31.810
List index is out of range okay.

04:31.840 --> 04:37.150
So if you try to access a value here that doesn't exist you will get an error.

04:37.150 --> 04:42.790
So make sure that you always use indexes that are actually corresponding to existing elements in the

04:42.790 --> 04:43.240
list.

04:43.240 --> 04:50.500
So here for example I go back to zero and I print I print the first element.

04:50.500 --> 04:54.250
So that's how you can read an element how you get an element.

04:54.280 --> 04:56.470
Now if you want to set an element.

04:56.470 --> 05:02.290
So if you want to write into the list and change one value, you can do this.

05:02.290 --> 05:05.530
So you just put the list name.

05:05.530 --> 05:06.400
So number list.

05:06.400 --> 05:11.660
And you use the same syntax here with brackets to select an element.

05:11.660 --> 05:13.880
So for example still we want to modify the first one.

05:13.880 --> 05:15.260
So I'm going to put zero.

05:15.260 --> 05:16.910
And then you put an equal sign.

05:16.910 --> 05:20.270
And let's say now I want to write ten okay.

05:20.300 --> 05:24.350
So I want to change the first element instead of three is going to become ten.

05:24.350 --> 05:31.250
And just to make sure let's just print this element again after we modify it okay.

05:31.280 --> 05:32.540
So let's run.

05:32.570 --> 05:39.230
You can see okay I have this error but it's not a problem I have three and then I have ten okay.

05:39.230 --> 05:40.910
So three was the first value.

05:40.940 --> 05:44.060
Then I modify it and then I can print it again.

05:44.060 --> 05:45.530
And it's a different value okay.

05:45.560 --> 05:46.670
And of course same thing.

05:46.670 --> 05:52.340
When you modify an element make sure that you use an index that is inside the list.

05:52.370 --> 05:52.760
Great.

05:52.760 --> 05:53.930
So you can get an element.

05:53.930 --> 05:55.220
You can set an element.

05:55.220 --> 05:58.310
But also in Python lists are dynamic.

05:58.310 --> 06:01.100
So you can also add elements to a list.

06:01.100 --> 06:02.630
You can add a new element.

06:02.630 --> 06:05.480
So here we have the index 0123.

06:05.510 --> 06:07.010
As you can see index four.

06:07.040 --> 06:08.810
We had an error because it doesn't exist.

06:08.810 --> 06:10.310
But you can add a new element.

06:10.310 --> 06:14.430
And then you will have something on the index for how to do this.

06:14.430 --> 06:17.490
Well, you can use the append function.

06:17.490 --> 06:20.370
So you will put the name of the list number list.

06:20.400 --> 06:24.120
And then you will do dot append like this.

06:24.120 --> 06:25.740
You open and close parentheses.

06:25.740 --> 06:27.600
So here make sure to use parentheses okay.

06:27.630 --> 06:28.710
This is a function.

06:28.710 --> 06:32.760
And let's put a new value for example 12 okay.

06:32.790 --> 06:37.230
So what I'm doing here is I'm adding a new element at the end of the list.

06:37.260 --> 06:40.830
Now let's just print the list number list okay.

06:40.860 --> 06:41.970
Let's print that.

06:42.180 --> 06:44.370
You can see that when we print the list here.

06:44.370 --> 06:45.990
So the first element is still ten.

06:46.020 --> 06:48.510
Then we have five minus eight 156.

06:48.510 --> 06:50.760
And we have 12 at the end okay.

06:50.790 --> 06:56.190
So we can add new elements to a list with this function right here okay.

06:56.220 --> 06:59.880
So with this you will be able to do lots of things with lists.

06:59.910 --> 07:05.670
And to finish here I want to show you a great combination you can do with lists and for loops.

07:05.700 --> 07:05.880
Okay.

07:05.910 --> 07:08.040
So now that you have seen for loops we can do that.

07:08.040 --> 07:14.590
So let's say that you want to get access to each element of Off the list.

07:14.590 --> 07:17.560
So I thought to read it or to modify it or to do something.

07:17.590 --> 07:17.830
Else.

07:17.830 --> 07:21.130
But you want to get access to each element one by one.

07:21.160 --> 07:22.030
How to do this?

07:22.060 --> 07:27.820
Well, you're not just going to do number list zero and then number one, number two etc. okay.

07:27.850 --> 07:30.040
You're going to use a for loop.

07:30.220 --> 07:34.570
So using for loops with lists is a very powerful combo in programming.

07:34.570 --> 07:35.920
So let's see how to do that.

07:35.950 --> 07:37.960
We have our for keyword.

07:37.960 --> 07:43.420
And then because we are going through a number list instead of just writing I am going to write number.

07:43.420 --> 07:45.850
And then I have the in keyword.

07:45.880 --> 07:46.060
Okay.

07:46.090 --> 07:50.860
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

07:50.860 --> 07:53.590
provide the list name.

07:54.010 --> 07:54.250
Okay.

07:54.250 --> 07:59.500
So for number in number list I add a colon I go back to a new line.

07:59.500 --> 08:04.420
And then I know that with this well, the block of code that is indented here is going to be repeated

08:04.420 --> 08:06.820
for each number in the number list.

08:06.850 --> 08:07.030
Okay.

08:07.030 --> 08:10.480
So for each item in the number list for now we have.

08:10.480 --> 08:14.470
So you can see we have 12345 elements.

08:14.470 --> 08:20.570
So this list is going to run five times, and each time the number variable is going to contain the

08:20.570 --> 08:21.230
element.

08:21.260 --> 08:21.530
Okay.

08:21.560 --> 08:23.720
So the first one, the second one, the third one, etc..

08:23.720 --> 08:26.900
So let's just print the number actually to see what we have.

08:28.940 --> 08:29.930
Like this.

08:30.770 --> 08:38.180
I run this and you can see that we print each element one by one.

08:38.210 --> 08:41.630
Okay so what happened is that first this variable.

08:41.630 --> 08:46.820
So this is the local variable inside the for loop is going to be created with the first element of number

08:46.820 --> 08:47.060
list.

08:47.060 --> 08:50.060
So the index zero which is not three anymore.

08:50.060 --> 08:52.460
It's ten because we have modified it here.

08:52.490 --> 08:58.400
Then it goes to the second element the third etc. until it has gone through all elements of the list.

08:58.400 --> 09:00.770
And now let's do something a bit more interesting.

09:00.770 --> 09:06.650
Let's say we want to create a new list that contains the same element as that list, but multiplied

09:06.650 --> 09:07.610
by two.

09:07.640 --> 09:13.940
So for this I will create here new list and I'm going to initialize it to.

09:14.060 --> 09:15.950
Actually I'm just going to write this.

09:15.950 --> 09:18.940
So open and close brackets with nothing inside.

09:18.970 --> 09:20.770
All right, so I create an empty list.

09:20.770 --> 09:23.230
This is a quite common practice to do in Python.

09:23.230 --> 09:27.730
You create an empty list and then you will fill some values in it with a for loop.

09:27.730 --> 09:33.790
So here instead of print I will just do new list dot append.

09:33.790 --> 09:35.410
And what do I add?

09:35.440 --> 09:39.820
I will just add the number times two.

09:39.850 --> 09:44.590
Okay, so I go through the number list here with a for loop.

09:44.590 --> 09:51.070
And for each item of the number list, I add a new element in the new list that corresponds to the number

09:51.070 --> 09:52.450
multiplied by two.

09:52.480 --> 09:55.330
And now if I want to print the new list.

09:55.330 --> 09:59.320
So make sure that you go back you see to the previous indentation here.

09:59.350 --> 10:02.230
Print a new list.

10:02.500 --> 10:11.260
That list should contain five values and you can see those five values are the double of the five values

10:11.260 --> 10:13.060
that we have in the number list.

10:13.060 --> 10:16.600
And we did that with just two lines of code here with this for loop.

10:16.630 --> 10:20.030
And just one thing to note here is related to the scope.

10:20.030 --> 10:26.960
You can see that we create the new list list here in that scope, and then we use it in the for loop.

10:26.960 --> 10:30.380
And we can do that because this is a more nested scope.

10:30.380 --> 10:32.300
You can see we add one indentation.

10:32.300 --> 10:33.770
This is a more nested scope.

10:33.770 --> 10:37.310
So we can use that variable that we've created here.

10:37.310 --> 10:40.040
So let's make a very quick recap.

10:40.070 --> 10:46.160
A list in Python will allow you to store a collection of values inside only one container.

10:46.160 --> 10:51.770
You can put as many values as you want in a list, and to create a list you first need to give it a

10:51.770 --> 10:52.850
meaningful name.

10:52.850 --> 11:00.080
Then the equal sign and you initialize values inside brackets, each value being separated by a comma.

11:00.110 --> 11:06.890
You can also use the append function to add a new element to an existing list, and when you access

11:06.890 --> 11:12.980
an element of a list, make sure you use the correct index and don't go out of the range.

11:12.980 --> 11:19.220
Finally, going through lists with a for loop is something that's very common and that you will use

11:19.220 --> 11:21.410
very often in your programs.
