1
00:00:01,839 --> 00:00:08,170
Let's experiment with the code a little bit now so that you get a better understanding of certain aspects

2
00:00:08,170 --> 00:00:09,010
of Python.

3
00:00:09,730 --> 00:00:12,730
I want to focus here on the full loop.

4
00:00:12,910 --> 00:00:20,350
The for loop iterates over the to do lists and accesses the index and the item in each iteration.

5
00:00:21,700 --> 00:00:27,070
It constructs a string and it prints out that string in each iteration.

6
00:00:27,490 --> 00:00:35,860
Now, if I press enter here and then I press backspace to go one indentation level back at this point.

7
00:00:36,600 --> 00:00:39,840
Same level with for loop.

8
00:00:40,020 --> 00:00:49,440
And if I say print index comma item, what do you think we're going to get?

9
00:00:50,400 --> 00:00:53,280
And also I would like to print out.

10
00:00:54,660 --> 00:00:55,380
Hello.

11
00:00:55,410 --> 00:00:56,340
Before that.

12
00:00:57,120 --> 00:00:58,230
So let's see.

13
00:00:58,830 --> 00:01:07,870
I'm going to run this ad A to do clean the keyboard because it has to be clean sometimes.

14
00:01:07,890 --> 00:01:09,090
Add the new one.

15
00:01:12,380 --> 00:01:13,700
The monitor as well.

16
00:01:14,960 --> 00:01:17,840
And a third one add.

17
00:01:21,120 --> 00:01:27,600
Buy grocery press, enter press show and let's look at the output.

18
00:01:28,140 --> 00:01:30,570
So perhaps you guessed what we're going to get.

19
00:01:31,410 --> 00:01:36,660
So we get to clean the keyboards, clean the monitor, buy grocery.

20
00:01:36,660 --> 00:01:43,800
All these three are produced by the print row line in here, this one.

21
00:01:44,010 --> 00:01:48,000
And then we have this third line here to buy grocery.

22
00:01:49,140 --> 00:01:53,970
So that is produced obviously by this line we get or print out.

23
00:01:55,420 --> 00:01:56,710
And then we get to.

24
00:01:58,400 --> 00:02:02,810
Because index is too and item prints out by grocery.

25
00:02:03,170 --> 00:02:05,120
So why did I do this experiment?

26
00:02:05,150 --> 00:02:14,570
Well, this was to show you that once the loop ends, which is the case when you indent the loop, as

27
00:02:14,570 --> 00:02:18,110
we did here, then these are still variables.

28
00:02:18,110 --> 00:02:20,810
Index and items are still variables.

29
00:02:22,000 --> 00:02:27,850
And index has as a value of the last index in the for loop.

30
00:02:28,060 --> 00:02:30,280
And the same goes for item item.

31
00:02:30,280 --> 00:02:35,890
The variable will also have as value the last item of the whole of the list.

32
00:02:36,100 --> 00:02:38,440
So the loop went over and over again.

33
00:02:38,440 --> 00:02:39,910
Index was for zero.

34
00:02:39,910 --> 00:02:43,900
In the second iteration was one, in the third was two.

35
00:02:44,110 --> 00:02:53,200
And so once there were no more items in the loop, we get index assigns the final number, the final

36
00:02:53,200 --> 00:02:55,810
value of two and the same goes for item.

37
00:02:55,810 --> 00:03:00,220
So I hope that gives you some perspective to understand the for loop better.

38
00:03:00,670 --> 00:03:07,630
The index particularly could be used if you want to calculate the length of the list.

39
00:03:07,840 --> 00:03:13,840
So basically you would say length is

40
00:03:16,480 --> 00:03:27,460
index plus one, so that would print out the length A's and index plus one, which would be two +13.

41
00:03:27,460 --> 00:03:29,680
So we would get the length is three.

42
00:03:31,390 --> 00:03:33,660
Or you could also use a knife string if you like.

43
00:03:33,670 --> 00:03:46,720
So f length s and you'd have to put this inside curly brackets and close the string with a quote.

44
00:03:48,460 --> 00:03:58,570
So let me run this and to do one add to the to show and we get length is to.

45
00:03:59,820 --> 00:04:05,040
However, getting the length like this is quite a workaround.

46
00:04:05,490 --> 00:04:10,980
I would even call it a hack because calling it a workaround is giving it too much credit.

47
00:04:11,010 --> 00:04:19,680
The actual way to get the length of lists is by using another more appropriate function, and that is

48
00:04:20,220 --> 00:04:21,470
the length function.

49
00:04:21,480 --> 00:04:23,700
It's a function as argument.

50
00:04:23,700 --> 00:04:30,060
It gets the list and the length function doesn't even need a full loop.

51
00:04:30,360 --> 00:04:34,470
So if you wanted to show the length, all you'd have to do is like that.

52
00:04:35,220 --> 00:04:44,190
Run at 2 to 1 at two, the two show and you get two.

53
00:04:44,220 --> 00:04:47,400
So that is the output of the length function.

54
00:04:48,880 --> 00:04:58,270
So anyway, we don't need the length in this app, so I'll just delete everything and keep the loop

55
00:04:58,270 --> 00:04:59,620
as it was before.

56
00:05:00,600 --> 00:05:06,600
An experiment now over the enumerate method if you open the python console.

57
00:05:08,590 --> 00:05:14,020
And you do for I item or I j whatever you like.

58
00:05:14,020 --> 00:05:16,620
So I will s above the index.

59
00:05:16,630 --> 00:05:18,520
J is about the item itself.

60
00:05:18,550 --> 00:05:22,960
It doesn't matter what variable names you use in enumerate.

61
00:05:24,880 --> 00:05:25,510
Hello.

62
00:05:26,960 --> 00:05:27,430
Great.

63
00:05:28,880 --> 00:05:32,410
I and J execute.

64
00:05:32,410 --> 00:05:33,820
And this is what you get.

65
00:05:35,490 --> 00:05:44,670
So strings can also be used with the enumerate function because strings also are sequences of items

66
00:05:44,670 --> 00:05:46,890
and each item is itself a string.

67
00:05:46,890 --> 00:05:50,340
So each character of the string is a string itself.

68
00:05:51,240 --> 00:05:52,620
That's why we get here.

69
00:05:52,620 --> 00:05:54,350
We get zero in the first iteration.

70
00:05:54,360 --> 00:05:56,520
H So G prints out.

71
00:05:56,520 --> 00:06:00,240
H, Then we get one E and so on.

72
00:06:00,750 --> 00:06:03,000
So that's something to keep in mind.

73
00:06:03,330 --> 00:06:08,850
Now you know that functions return and output, right?

74
00:06:08,970 --> 00:06:11,130
And this here is a function.

75
00:06:11,760 --> 00:06:15,630
So what is the output of this function anyway?

76
00:06:16,560 --> 00:06:19,290
Or let's try a list as we did before.

77
00:06:20,600 --> 00:06:27,350
So let's say A, B, and C.

78
00:06:28,920 --> 00:06:33,360
And if we check a, we get this.

79
00:06:33,360 --> 00:06:35,220
So that's a bit confusing.

80
00:06:36,150 --> 00:06:42,540
The reason we get this is that the enumerate function returns an enumerate object.

81
00:06:42,540 --> 00:06:50,610
So just like we have list object types, we have strings object types, we have integers for numbers.

82
00:06:50,610 --> 00:06:53,310
We also have this strange object here.

83
00:06:54,600 --> 00:06:59,580
The difference here is that Python doesn't know how to display this object.

84
00:07:00,480 --> 00:07:05,130
The object actually contains delta, but you cannot see them.

85
00:07:05,130 --> 00:07:06,950
So it is not designed.

86
00:07:06,960 --> 00:07:11,910
The object itself is not designed to display those data in the command line.

87
00:07:12,300 --> 00:07:20,850
But if you use a popular type such as the list and you place that object in a list, so to basically

88
00:07:20,850 --> 00:07:25,080
to convert it into a list, you press enter and you get this.

89
00:07:25,410 --> 00:07:29,850
So now we have a representation of that enumerate object.

90
00:07:31,380 --> 00:07:35,910
This is actually the internal construction of that object.

91
00:07:36,180 --> 00:07:40,530
But now we can we can visualize it using a list, we can see it.

92
00:07:41,310 --> 00:07:44,550
So it's a list made of tuples.

93
00:07:44,550 --> 00:07:47,550
That is the first of all, the second tuple and the third tuple.

94
00:07:47,670 --> 00:07:54,180
Each tuple contains the index and the item itself of the original list.

95
00:07:55,360 --> 00:07:57,670
The index of B and the B itself.

96
00:07:57,670 --> 00:08:00,640
Index of C and the C itself.

97
00:08:02,220 --> 00:08:15,440
So actually when we do four or four item, whatever you call them in enumerate and you have that list

98
00:08:15,450 --> 00:08:21,390
here, ABC, what you're actually doing is you're doing this for item in.

99
00:08:23,820 --> 00:08:24,840
This list.

100
00:08:25,470 --> 00:08:35,549
So enumerate is a list of tuples and you say print I item press enter, enter and we get this.

101
00:08:35,549 --> 00:08:43,350
So actually I is getting the first item of the first simple and then item is getting the second item

102
00:08:43,350 --> 00:08:44,670
of that first sample.

103
00:08:45,330 --> 00:08:50,010
And we print them out here and we do the same for the other tuples.

104
00:08:52,030 --> 00:08:54,670
So that's about the innumerate object.

105
00:08:56,170 --> 00:09:00,010
Of course, you can do the same with a string.

106
00:09:00,010 --> 00:09:00,940
So hello.

107
00:09:01,600 --> 00:09:04,210
And you can convert it into a list.

108
00:09:04,690 --> 00:09:06,280
Sorry, I did the typo here.

109
00:09:09,540 --> 00:09:11,940
And you may rate.

110
00:09:12,930 --> 00:09:14,820
So be is an enumerate object.

111
00:09:14,820 --> 00:09:16,680
We can convert it into a list.

112
00:09:19,080 --> 00:09:22,230
And now we see that same construction.

113
00:09:23,670 --> 00:09:31,950
However, if you are thinking of converting of of be a numerous object into a string, you'll get a

114
00:09:31,950 --> 00:09:41,040
bit disappointed because all this will do is just convert that representation to a string.

115
00:09:41,040 --> 00:09:42,750
You see, it has quotes now.

116
00:09:44,070 --> 00:09:48,410
In other words, STR doesn't do what you intend to do.

117
00:09:48,420 --> 00:09:50,010
It's actually useless here.

118
00:09:50,760 --> 00:10:01,050
It is useless because the anatomy of an enumerate object would not be able to represent using a string

119
00:10:01,050 --> 00:10:08,370
because a string is just made of character of strings and at least can be made as you see, can be made

120
00:10:08,370 --> 00:10:13,950
of other tuples and other lists or other items, strings, numbers.

121
00:10:14,610 --> 00:10:18,480
That's why it is possible to convert enumerate into a list.

122
00:10:19,950 --> 00:10:25,380
And that was a series of experiments on the for loop and the enumerate object.

123
00:10:25,950 --> 00:10:26,550
Thanks a lot.

