WEBVTT

00:00.440 --> 00:04.220
You can do just about anything with each item in a for loop.

00:04.250 --> 00:10.610
Let's build the previous example by printing a message to each magician and telling them that they performed

00:10.610 --> 00:11.810
a great trick.

00:12.020 --> 00:19.550
Now, after that, inside the form, we will use the print magician format here again.

00:19.850 --> 00:22.220
So we will use a string format.

00:25.020 --> 00:28.560
And here we will magician that title.

00:28.890 --> 00:30.780
And after that we will use.

00:30.810 --> 00:36.420
Yeah, that was a great, great, great trick.

00:36.420 --> 00:36.930
Right?

00:36.960 --> 00:37.770
Trick.

00:38.100 --> 00:40.620
And the only difference in this code here.

00:41.460 --> 00:48.570
Here is where we compose a message to each magician, starting with the magician's name the first time

00:48.570 --> 00:49.380
through the loop.

00:49.380 --> 00:51.570
The value of a magician is Alice.

00:51.570 --> 00:55.590
So Python starts the first message with the name Alice.

00:55.740 --> 01:03.390
The second time, though, the message will begin with a David, and the third time the message will

01:03.420 --> 01:04.830
begin with Caroline.

01:05.910 --> 01:06.840
And the output.

01:06.840 --> 01:10.890
This is the personalized message for each magician and the list.

01:11.310 --> 01:14.490
You can also write as many lines of code as you like in the for loop.

01:14.760 --> 01:21.840
Every indented line following the line for magician in magicians like here.

01:24.380 --> 01:27.110
Considered inside the loop.

01:27.810 --> 01:33.450
And each indented line is executed once for each value in the list.

01:33.480 --> 01:37.560
Therefore, you can do as much work as you like with each value in the list.

01:37.590 --> 01:44.070
Let's add a second line to our message telling each magician that we are looking forward to their to

01:44.070 --> 01:45.120
do their next trick.

01:45.120 --> 01:49.500
So print here f format again.

01:49.500 --> 01:56.130
And now without these braces I or the.

01:56.800 --> 01:57.820
Weavers.

01:58.240 --> 02:06.010
Weavers can not wait to see your next trick.

02:06.270 --> 02:07.300
Dear.

02:08.230 --> 02:16.630
And after that we will enter the magician Magician title because as you remember, our magician names

02:16.630 --> 02:18.820
is written an underscore.

02:18.820 --> 02:22.120
So that's why we're going to write titles here.

02:22.630 --> 02:25.270
So magician dot title.

02:26.250 --> 02:27.060
That's it.

02:27.420 --> 02:29.940
And after that, we will use a new line.

02:29.940 --> 02:30.720
So.

02:35.310 --> 02:38.070
So because now let's rent.

02:38.370 --> 02:39.150
That's it.

02:40.810 --> 02:44.470
Because we have invented both calls to print.

02:44.500 --> 02:48.790
Each line will be executed once for every magicians in the list.

02:48.790 --> 02:52.300
And here the new line here.

02:52.720 --> 02:56.560
The new new line here in the second print.

02:56.740 --> 02:57.580
This is the print.

02:57.610 --> 02:59.020
This is the first print.

03:00.000 --> 03:01.200
This is the second print.

03:03.050 --> 03:03.920
This new line.

03:03.920 --> 03:10.640
The second brand calls insert a blank line after each pass pass through the loop.

03:10.850 --> 03:17.570
So here one new line, two new line, and the third new line is at the bottom here somewhere.

03:17.570 --> 03:18.050
Here.

03:18.850 --> 03:25.270
So this creates a set of messages that are neatly grouped for each person in the list here.

03:26.480 --> 03:29.630
You can use as many lines as you like in your for loops.

03:29.670 --> 03:34.760
In practice, you will often find it useful to do a number of different operations with each item in

03:34.760 --> 03:35.300
a list.

03:35.330 --> 03:38.420
When you use a for loop.

03:41.630 --> 03:44.890
So what happens once a for loop has finished executing?

03:44.900 --> 03:51.800
Usually you will want to summarize a block of output or move on to the other work that your your program

03:51.800 --> 03:53.000
must accomplish.

03:53.000 --> 03:59.540
Any lines of code after the for loop that are not indented are executed once without repetition.

03:59.570 --> 04:06.380
Let's write a thank you to the group of Magicians as a whole and thanking them for putting on an excellent

04:06.380 --> 04:06.770
show.

04:06.770 --> 04:09.530
So to display this, a group message.

04:09.650 --> 04:14.900
After all of the individual messages have been printed, we place a thank you message after the for

04:14.900 --> 04:17.360
loop without indentation.

04:17.750 --> 04:24.260
So now we just write Without indentation, we will press on the backspace.

04:24.260 --> 04:24.920
That's it.

04:24.950 --> 04:27.530
Now signs are crucial.

04:27.530 --> 04:28.970
Is here somewhere.

04:28.970 --> 04:33.530
Here we are out of the loop so we are not in the loop.

04:33.530 --> 04:39.770
So whenever whatever we write here, it will not count as loop here.

04:40.500 --> 04:41.070
Okay.

04:42.480 --> 04:43.860
So that's it.

04:43.890 --> 04:45.300
Now print.

04:46.350 --> 04:49.590
Thank you, everyone.

04:52.200 --> 04:59.280
Everyone for watching and especially for magicians.

05:02.280 --> 05:02.760
Split.

05:03.720 --> 05:05.370
Especially for magicians.

05:06.160 --> 05:09.310
That that was a.

05:10.400 --> 05:12.500
Spectacular spec.

05:13.550 --> 05:14.840
Tacular show.

05:16.430 --> 05:17.240
That's it.

05:18.260 --> 05:18.740
Here.

05:20.800 --> 05:22.360
Now let's run our code again.

05:24.680 --> 05:25.900
That's it.

05:25.910 --> 05:26.980
This is how it works.

05:26.990 --> 05:34.130
The first two cards to print are repeated once for each magicians in the list, as you saw earlier.

05:34.130 --> 05:41.570
However, because the last line is not indented, it's printed only once.

05:42.050 --> 05:42.560
Here.

05:44.310 --> 05:47.340
It's printed only once.

05:47.670 --> 05:48.240
Look at this.

05:51.970 --> 05:52.570
That's it.

05:54.970 --> 05:55.720
And.

05:56.940 --> 05:59.910
This is the output that we wrote here.

05:59.910 --> 06:05.430
And when you're processing data using a for loop, you will find that this is a good way to summarize

06:05.430 --> 06:09.330
an operation that was performed on an entire dataset.

06:09.360 --> 06:14.220
For example, you might use a for loop to initialize a game by running through the list of characters

06:14.220 --> 06:16.620
and displaying each character on the screen.

06:16.620 --> 06:23.160
And you might want to, you might want then write some additional code after the loop that displays

06:23.160 --> 06:23.580
a play.

06:23.580 --> 06:24.150
No button.

06:24.150 --> 06:26.550
After all the characters have been drawn to the screen.
