1
00:00:01,120 --> 00:00:05,380
What happens once a for loop has finished executing.

2
00:00:05,410 --> 00:00:11,590
Usually you will want to summarize a block of output or move on to other work that your program must

3
00:00:11,590 --> 00:00:12,330
accomplish.

4
00:00:12,340 --> 00:00:21,310
Any lines of code after the for loop that are not intended are executed once without repetition.

5
00:00:21,310 --> 00: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

6
00:00:30,760 --> 00:00:33,130
on an excellent cars.

7
00:00:33,130 --> 00:00:39,010
So to display this group of message after all of the individual messages have been printed, we place

8
00:00:39,010 --> 00:00:39,490
them.

9
00:00:39,490 --> 00:00:44,020
Thank you message after the for loop without the indentation here.

10
00:00:44,020 --> 00:00:52,660
So now we will write print and thank you every car makers.

11
00:00:53,440 --> 00:00:59,020
These cars are special and great.

12
00:00:59,200 --> 00:01:07,910
And here now let's run this and you will see all this message is just executed once.

13
00:01:07,910 --> 00:01:17,660
So the first two cars to print are repeated once for each car makers in the list, as you saw earlier.

14
00:01:17,660 --> 00:01:23,870
However, because the last line is not intended, it's printed only once.

15
00:01:23,870 --> 00:01:29,360
If we add indentation to it, you will see, as you can see here, Thank you.

16
00:01:29,360 --> 00:01:35,030
Every car makers, thank you, every car makers, thank you, every car makers and so on.

17
00:01:35,420 --> 00:01:43,760
So we will delete this indentation and we will get this thank you message only once because it's not

18
00:01:43,760 --> 00:01:51,470
included in this loop because of the indentation we didn't put before the print here.

19
00:01:51,470 --> 00:01:57,740
So when you're processing data using a for loop, you will find that this is a good way to summarize

20
00:01:57,740 --> 00:02:01,450
an operation that was performed on an entire dataset.

21
00:02:01,450 --> 00:02:06,670
For example, you might want to use a for loop to initialize a game by running through the list of characters

22
00:02:06,670 --> 00:02:09,130
and displaying each character on the screen.

23
00:02:09,160 --> 00:02:15,640
You might then write some additional code after this loop that displays a play button.

24
00:02:15,640 --> 00:02:19,000
After all the characters have been drawn to the screen.

25
00:02:25,760 --> 00:02:33,360
Python uses indentation to determine how a line or group of lines is related to the rest of the program.

26
00:02:33,440 --> 00:02:41,480
In the previous in this examples the lines that printed messages to individual car makers were part

27
00:02:41,480 --> 00:02:44,510
of the for loop because they were indented.

28
00:02:44,640 --> 00:02:49,760
So python's use of indentation makes code very easy to read.

29
00:02:49,790 --> 00:02:58,430
Basically, it uses whitespace to force you to write neatly formatted code with a clear visual structure.

30
00:02:58,460 --> 00:03:03,400
In longer Python programs, you will notice block of code indented at a few different levels.

31
00:03:03,410 --> 00:03:10,190
These indentations levels help you gain a general sense of the overall program's organization.

32
00:03:10,190 --> 00:03:17,000
So as you begin to write code that relies on proper indentation, you will need to watch for a few common

33
00:03:17,030 --> 00:03:19,080
indentation errors.

34
00:03:19,100 --> 00:03:25,550
For example, people sometimes indent lines of code that don't need to be indented or forget the indented

35
00:03:25,650 --> 00:03:28,260
lines that need to be indented.

36
00:03:28,260 --> 00:03:34,620
So seeing examples of these errors now will help you avoid them in the future and correct them when

37
00:03:34,620 --> 00:03:37,170
they do appear in your own programs.

38
00:03:37,170 --> 00:03:46,740
So now let's examine some some of the more common indentation errors so you can also forget I here indent

39
00:03:46,890 --> 00:03:52,260
so always indent a line after the fourth statement in a loop.

40
00:03:52,260 --> 00:03:55,920
If you forget, Python will not remind you here.

41
00:04:00,760 --> 00:04:04,180
And as you can see here, expected and indented.

42
00:04:04,180 --> 00:04:04,930
Block.

43
00:04:06,180 --> 00:04:07,320
So here.

44
00:04:12,730 --> 00:04:22,390
If we write, for example car makers and just let the segmentation Eleanor also so the let's actually

45
00:04:22,390 --> 00:04:23,980
delete this and I will explain all of this.

46
00:04:24,070 --> 00:04:28,240
So the call to print should be indented.

47
00:04:28,360 --> 00:04:29,920
So but it's not here.

48
00:04:29,920 --> 00:04:36,940
So when Python expects an identity indented block and doesn't find one, it lets you know which line

49
00:04:36,940 --> 00:04:38,650
it had problem with.

50
00:04:38,980 --> 00: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

51
00:04:45,640 --> 00:04:47,590
at line three.

52
00:04:47,620 --> 00:04:58,060
So you can usually resolve these kinds of annotation errors by indenting the lines or lines immediately

53
00:04:58,060 --> 00:05:00,490
after the for statement.

54
00:05:00,520 --> 00:05:05,440
You can also forget things or forget to indent additional lines.

55
00:05:05,440 --> 00:05:13,400
So sometimes your loop will run without any errors but won't produce the expected result.

56
00:05:13,400 --> 00: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

57
00:05:20,690 --> 00:05:21,410
its lines.

58
00:05:21,410 --> 00:05:27,860
For example, this is what happens when we forget to indent the second line in the loop.

59
00:05:27,920 --> 00:05:29,120
And here.

60
00:05:32,130 --> 00:05:32,850
Do here.

61
00:05:32,880 --> 00:05:35,010
We will not get any error here.

62
00:05:35,630 --> 00:05:42,470
And now we'll if we run this and as you can see here, at List Object has no attribute title.

63
00:05:45,450 --> 00:05:48,360
As a result, the first print.

64
00:05:48,940 --> 00:05:53,440
And if if makers here this code will end as you can see.

65
00:05:53,440 --> 00:05:54,040
Let's.

66
00:05:55,520 --> 00:05:57,080
I'll make ups.

67
00:05:57,410 --> 00:05:58,010
I'll make.

68
00:05:59,490 --> 00:06:03,150
This code will run without a problem if you just edit it.

69
00:06:03,300 --> 00:06:06,450
Edit this and if we change this to car make.

70
00:06:07,710 --> 00:06:09,650
And we will get this as as well.

71
00:06:09,660 --> 00:06:11,550
So here.

72
00:06:12,530 --> 00:06:13,280
The.

73
00:06:15,860 --> 00:06:23,510
As the result here, the first printf call is not indented, is indented and.

74
00:06:25,360 --> 00: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.

75
00:06:35,740 --> 00:06:43,870
But here the second print call is not indented, so it's executed only once after the loop has finished

76
00:06:43,870 --> 00:06:52,240
running because the final value associated with with Volkswagen here and Volkswagen is the only one

77
00:06:52,240 --> 00:06:54,880
who receives the looking who receives the.

78
00:06:54,880 --> 00:06:55,520
Thank you, Eric.

79
00:06:55,570 --> 00:06:56,080
Carmakers.

80
00:06:56,080 --> 00:06:57,570
These cars are special and correct.

81
00:06:57,580 --> 00:07:03,970
And Volkswagen, only one who gets the miles per gallon is high on Volkswagen.

82
00:07:04,920 --> 00:07:07,860
And here this is a logical error.

83
00:07:07,890 --> 00:07:13,470
This syntax is valid python code, but the code does not produce the desired result because a problem

84
00:07:13,470 --> 00:07:15,000
occurs in its logic.

85
00:07:15,030 --> 00:07:21,840
So if you expect to see certain action repeated once for each item in place and it's executed only once,

86
00:07:21,840 --> 00:07:26,070
determine whether you need to simply indent a line or group of lines.

87
00:07:26,070 --> 00:07:27,330
So here that's.

88
00:07:27,540 --> 00: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

89
00:07:34,710 --> 00:07:36,600
print message will execute it once.

90
00:07:36,600 --> 00:07:42,090
But your program firstly execute your for loop again, again and again.

91
00:07:42,090 --> 00:07:47,040
And then after completing the for loop it will go to next lines here.
