WEBVTT

00:00.500 --> 00:05.330
In previous section, you learned how to make a simple list and you learned how to work with individual

00:05.330 --> 00:06.170
elements in list.

00:06.800 --> 00:12.730
In this section, you will learn how to loop through an entire list using just a few lines of code,

00:12.740 --> 00:15.270
regardless of how long the list is.

00:15.290 --> 00:21.350
So looping allows you to make the same action or set of actions with every item in a list.

00:21.380 --> 00:27.590
As a result, you will be able to work efficiently with a list of any length, including those with

00:27.680 --> 00:29.630
thousands or even millions of items.

00:30.620 --> 00:37.340
And you will often want to run through all entries in the list.

00:38.740 --> 00:42.220
So performing the same task with each item.

00:42.220 --> 00:46.720
For example, in a game you might want to move every element.

00:47.560 --> 00:53.350
But on the screen to the same account practice.

00:55.800 --> 01:02.460
Um, or in a list of numbers you might want to perform the same statistical operation on every element.

01:02.460 --> 01:08.700
Or perhaps you will want to display each headline from a list of articles on a website.

01:08.700 --> 01:15.330
So when you want to do the same action with every item in a list, you can use The python's for loop,

01:15.780 --> 01:17.250
Python's for loop.

01:17.640 --> 01:24.210
So say we have a list of magician's names and we want to print out each name in the list.

01:25.190 --> 01:29.720
We could do this by retrieving each name from the list individually, but this approach could cause

01:29.720 --> 01:30.950
several problems here.

01:30.980 --> 01:35.810
For one, it would be repetitive to do this with a long list of names.

01:35.840 --> 01:42.470
Also, we'd have to change our code each time the list length created and using the for loop avoids

01:42.470 --> 01:48.530
both of these uses by letting Python manage these uses internally.

01:48.530 --> 01:52.640
So let's use a for loop to print out each name in a list of magicians.

01:52.640 --> 01:57.740
So here actually mutations.

02:01.930 --> 02:02.650
So just like that.

02:02.650 --> 02:03.160
Yes.

02:03.340 --> 02:04.860
So, magicians.

02:04.900 --> 02:07.330
And let's actually make it Alice.

02:10.010 --> 02:10.760
Alice.

02:10.940 --> 02:11.990
David.

02:13.650 --> 02:15.690
David and Carolina.

02:17.880 --> 02:20.010
And here we will create a for loop here.

02:20.010 --> 02:22.350
So for magician.

02:24.030 --> 02:25.860
In magicians.

02:26.570 --> 02:31.430
And after that we will print the magician every time.

02:32.300 --> 02:39.890
And here we begin by defining a list, just as we did in previous lectures.

02:39.920 --> 02:40.280
Oops.

02:40.280 --> 02:42.690
Here, let me fix my mouse right now.

02:42.710 --> 02:43.430
Yes.

02:43.550 --> 02:44.690
So here we go.

02:44.780 --> 02:50.630
We begin by defining a list, just as we did in previous lectures here.

02:52.280 --> 02:53.120
So.

02:54.580 --> 02:56.320
Then we define a for loop.

02:56.320 --> 02:57.910
This is new to us, right?

02:58.060 --> 03:02.050
So then we define the for loop.

03:03.210 --> 03:05.310
So this line.

03:06.070 --> 03:10.690
Tells Python to pull a name from the list.

03:10.720 --> 03:12.370
Magicians.

03:13.600 --> 03:14.350
And.

03:15.170 --> 03:18.950
Associated with the variable name magician.

03:20.180 --> 03:29.810
Next we tell Python to print the name that just been assigned to this magician here.

03:32.370 --> 03:37.380
Biden then repeats these last two lines, one for each name in the list.

03:37.410 --> 03:41.320
It might help to read this code as like this.

03:41.340 --> 03:46.170
So here we for this code, we have this explanation.

03:51.450 --> 03:57.210
Every magician in the list of magicians.

03:59.570 --> 04:00.650
Magicians.

04:03.450 --> 04:06.120
Print the magician's.

04:09.070 --> 04:09.520
Nay.

04:12.080 --> 04:16.520
So the output is simple printout of each name in the list.

04:16.550 --> 04:17.780
Let's try this out.

04:21.360 --> 04:30.450
As you can see here, we printed every user in the in the our magicians list without a problem.

04:32.470 --> 04:37.840
Now looping is important because it's one of the most common ways of computer automates repetitive tasks.

04:37.870 --> 04:44.860
For example, in a simple loop like we used in this file, in this lecture.

04:45.810 --> 04:46.560
Here.

04:47.880 --> 04:52.270
Python initially reads the first line of the loop.

04:52.290 --> 04:53.400
This line here.

04:53.550 --> 05:01.950
So this line tells Python to retrieve the first value from the list magicians and associated with the

05:01.950 --> 05:05.450
variable magician and the first value here.

05:05.460 --> 05:09.240
So here, actually, let me write that down here.

05:10.210 --> 05:10.810
Okay.

05:10.810 --> 05:14.500
So the first value here, The first.

05:14.530 --> 05:15.400
Oh, sorry.

05:16.000 --> 05:16.480
Here.

05:16.480 --> 05:18.640
The first value here.

05:19.420 --> 05:21.490
Is Alice, right?

05:21.640 --> 05:22.400
Yes.

05:22.420 --> 05:25.090
The first value here is.

05:29.420 --> 05:30.980
And the second value.

05:32.270 --> 05:38.390
And I'll also python prints the first value as Alice and the current.

05:38.510 --> 05:43.730
This is the current value of magician right now which is still Alice because the list contains more

05:43.730 --> 05:49.850
values and Python returns to the first line of the loop.

05:50.090 --> 05:50.960
Right.

05:51.320 --> 05:56.630
So Python retrieves the next name in the list, which is.

05:58.280 --> 05:59.120
In this case.

05:59.120 --> 06:00.110
David Right.

06:00.110 --> 06:02.420
So Python also retrieves the.

06:02.420 --> 06:03.050
David.

06:06.980 --> 06:12.950
And associates that value within the variable magician here.

06:13.250 --> 06:20.720
And Python then executes the line which is print the magician and Python prints the current value of

06:20.720 --> 06:28.760
magician again which is now is David and Python repeats the entire loop once more with the last value

06:28.760 --> 06:31.700
in the list, which is Caroline.

06:32.000 --> 06:33.470
This here.

06:34.080 --> 06:36.330
So because no more values are in the list.

06:36.330 --> 06:39.900
Python moves on to the next line of the program.

06:39.900 --> 06:41.970
In this case, nothing.

06:42.000 --> 06:45.750
Nothing comes after this line here.

06:46.590 --> 06:48.900
So this is empty here for now.

06:50.450 --> 06:51.020
And.

06:53.490 --> 06:55.980
And nothing comes after the for loop.

06:55.980 --> 06:59.160
And so for loop will end here.

06:59.160 --> 07:05.610
So when you are using the loops for the first time, keep in mind that the set of steps is repeated

07:05.610 --> 07:07.980
once for each item in the list.

07:07.980 --> 07:12.630
So no matter how many items are in the list and how much.

07:12.630 --> 07:19.050
So if you have a million items in your list, the python repeats these steps a million times and usually

07:19.050 --> 07:19.950
very quickly.

07:20.320 --> 07:26.820
Also, keep in mind when writing your own for loops that you can choose any name you want for the temporary

07:26.820 --> 07:27.600
variable.

07:28.660 --> 07:30.850
That will be associated with each value in the list.

07:30.850 --> 07:35.710
So however itself will do, choose a meaningful name that represents a single item for the list.

07:35.920 --> 07:38.920
So for example, you can.

07:39.310 --> 07:45.040
Let's actually create a good way to for naming these loops here.

07:45.760 --> 07:56.200
So another good way for naming this loop is, for example, look for Cat in cats or for dog in dogs

07:56.230 --> 08:02.710
or for item in list of items.

08:02.740 --> 08:07.120
Okay, so these are the just examples that you can name your loops.

08:07.120 --> 08:10.430
So to make it more meaningful and readable.

08:10.450 --> 08:10.990
Right.

08:10.990 --> 08:17.710
So these naming conventions can help you to follow the action behind being done on each item within

08:17.710 --> 08:18.610
a for loop.

08:19.120 --> 08:24.190
And using singular and plural names can help you identify whether a section of code is working with

08:24.190 --> 08:28.820
a single element from the list or the entire list at.
