WEBVTT

00:01.120 --> 00:05.380
What happens once a for loop has finished executing.

00:05.410 --> 00:11.590
Usually you will want to summarize a block of output or move on to other work that your program must

00:11.590 --> 00:12.330
accomplish.

00:12.340 --> 00:21.310
Any lines of code after the for loop that are not intended are executed once without repetition.

00:21.310 --> 00:30.760
So let's write a thank you to a group of group of car makers as a whole and thanking them for putting

00:30.760 --> 00:33.130
on an excellent cars.

00:33.130 --> 00:39.010
So to display this group of message after all of the individual messages have been printed, we place

00:39.010 --> 00:39.490
them.

00:39.490 --> 00:44.020
Thank you message after the for loop without the indentation here.

00:44.020 --> 00:52.660
So now we will write print and thank you every car makers.

00:53.440 --> 00:59.020
These cars are special and great.

00:59.200 --> 01:07.910
And here now let's run this and you will see all this message is just executed once.

01:07.910 --> 01:17.660
So the first two cars to print are repeated once for each car makers in the list, as you saw earlier.

01:17.660 --> 01:23.870
However, because the last line is not intended, it's printed only once.

01:23.870 --> 01:29.360
If we add indentation to it, you will see, as you can see here, Thank you.

01:29.360 --> 01:35.030
Every car makers, thank you, every car makers, thank you, every car makers and so on.

01:35.420 --> 01:43.760
So we will delete this indentation and we will get this thank you message only once because it's not

01:43.760 --> 01:51.470
included in this loop because of the indentation we didn't put before the print here.

01:51.470 --> 01:57.740
So when you're processing data using a for loop, you will find that this is a good way to summarize

01:57.740 --> 02:01.450
an operation that was performed on an entire dataset.

02:01.450 --> 02:06.670
For example, you might want to use a for loop to initialize a game by running through the list of characters

02:06.670 --> 02:09.130
and displaying each character on the screen.

02:09.160 --> 02:15.640
You might then write some additional code after this loop that displays a play button.

02:15.640 --> 02:19.000
After all the characters have been drawn to the screen.

02:25.760 --> 02:33.360
Python uses indentation to determine how a line or group of lines is related to the rest of the program.

02:33.440 --> 02:41.480
In the previous in this examples the lines that printed messages to individual car makers were part

02:41.480 --> 02:44.510
of the for loop because they were indented.

02:44.640 --> 02:49.760
So python's use of indentation makes code very easy to read.

02:49.790 --> 02:58.430
Basically, it uses whitespace to force you to write neatly formatted code with a clear visual structure.

02:58.460 --> 03:03.400
In longer Python programs, you will notice block of code indented at a few different levels.

03:03.410 --> 03:10.190
These indentations levels help you gain a general sense of the overall program's organization.

03:10.190 --> 03:17.000
So as you begin to write code that relies on proper indentation, you will need to watch for a few common

03:17.030 --> 03:19.080
indentation errors.

03:19.100 --> 03:25.550
For example, people sometimes indent lines of code that don't need to be indented or forget the indented

03:25.650 --> 03:28.260
lines that need to be indented.

03:28.260 --> 03:34.620
So seeing examples of these errors now will help you avoid them in the future and correct them when

03:34.620 --> 03:37.170
they do appear in your own programs.

03:37.170 --> 03:46.740
So now let's examine some some of the more common indentation errors so you can also forget I here indent

03:46.890 --> 03:52.260
so always indent a line after the fourth statement in a loop.

03:52.260 --> 03:55.920
If you forget, Python will not remind you here.

04:00.760 --> 04:04.180
And as you can see here, expected and indented.

04:04.180 --> 04:04.930
Block.

04:06.180 --> 04:07.320
So here.

04:12.730 --> 04:22.390
If we write, for example car makers and just let the segmentation Eleanor also so the let's actually

04:22.390 --> 04:23.980
delete this and I will explain all of this.

04:24.070 --> 04:28.240
So the call to print should be indented.

04:28.360 --> 04:29.920
So but it's not here.

04:29.920 --> 04:36.940
So when Python expects an identity indented block and doesn't find one, it lets you know which line

04:36.940 --> 04:38.650
it had problem with.

04:38.980 --> 04:45.640
And if we run this code again and let's run it here, and as you can see, we have a indentation error

04:45.640 --> 04:47.590
at line three.

04:47.620 --> 04:58.060
So you can usually resolve these kinds of annotation errors by indenting the lines or lines immediately

04:58.060 --> 05:00.490
after the for statement.

05:00.520 --> 05:05.440
You can also forget things or forget to indent additional lines.

05:05.440 --> 05:13.400
So sometimes your loop will run without any errors but won't produce the expected result.

05:13.400 --> 05:20.690
So this can happen when you are trying to do several tasks in a loop and you forget to indent some of

05:20.690 --> 05:21.410
its lines.

05:21.410 --> 05:27.860
For example, this is what happens when we forget to indent the second line in the loop.

05:27.920 --> 05:29.120
And here.

05:32.130 --> 05:32.850
Do here.

05:32.880 --> 05:35.010
We will not get any error here.

05:35.630 --> 05:42.470
And now we'll if we run this and as you can see here, at List Object has no attribute title.

05:45.450 --> 05:48.360
As a result, the first print.

05:48.940 --> 05:53.440
And if if makers here this code will end as you can see.

05:53.440 --> 05:54.040
Let's.

05:55.520 --> 05:57.080
I'll make ups.

05:57.410 --> 05:58.010
I'll make.

05:59.490 --> 06:03.150
This code will run without a problem if you just edit it.

06:03.300 --> 06:06.450
Edit this and if we change this to car make.

06:07.710 --> 06:09.650
And we will get this as as well.

06:09.660 --> 06:11.550
So here.

06:12.530 --> 06:13.280
The.

06:15.860 --> 06:23.510
As the result here, the first printf call is not indented, is indented and.

06:25.360 --> 06:35.730
So the first that's why first print call is executed once for each name in the list because it's indented.

06:35.740 --> 06:43.870
But here the second print call is not indented, so it's executed only once after the loop has finished

06:43.870 --> 06:52.240
running because the final value associated with with Volkswagen here and Volkswagen is the only one

06:52.240 --> 06:54.880
who receives the looking who receives the.

06:54.880 --> 06:55.520
Thank you, Eric.

06:55.570 --> 06:56.080
Carmakers.

06:56.080 --> 06:57.570
These cars are special and correct.

06:57.580 --> 07:03.970
And Volkswagen, only one who gets the miles per gallon is high on Volkswagen.

07:04.920 --> 07:07.860
And here this is a logical error.

07:07.890 --> 07:13.470
This syntax is valid python code, but the code does not produce the desired result because a problem

07:13.470 --> 07:15.000
occurs in its logic.

07:15.030 --> 07:21.840
So if you expect to see certain action repeated once for each item in place and it's executed only once,

07:21.840 --> 07:26.070
determine whether you need to simply indent a line or group of lines.

07:26.070 --> 07:27.330
So here that's.

07:27.540 --> 07:34.710
Now we are getting this error here because in for loop, if you have a for loop here for now, let's

07:34.710 --> 07:36.600
print message will execute it once.

07:36.600 --> 07:42.090
But your program firstly execute your for loop again, again and again.

07:42.090 --> 07:47.040
And then after completing the for loop it will go to next lines here.
