1
00:00:05,310 --> 00:00:06,330
Welcome back, everyone.

2
00:00:06,540 --> 00:00:11,130
We're now going to switch gears and our discussion of control flow to begin discussing loops.

3
00:00:11,370 --> 00:00:12,900
We're going to begin with the for loop.

4
00:00:14,280 --> 00:00:18,330
The term enterable means you can quote unquote iterate over the object.

5
00:00:18,510 --> 00:00:20,460
So what do we mean by iterate?

6
00:00:20,910 --> 00:00:25,620
That means that for every item in a sequence, you can perform an action.

7
00:00:26,130 --> 00:00:32,040
So, for example, remember that a string is a sequence of characters so you can perform an action or

8
00:00:32,040 --> 00:00:34,470
use every character in a string.

9
00:00:34,860 --> 00:00:40,170
Or you could iterate over every item in a list that is perform an action for each item in the list or

10
00:00:40,170 --> 00:00:41,580
use each item in the list.

11
00:00:41,910 --> 00:00:44,760
And you can even iterate over every key in a dictionary.

12
00:00:46,320 --> 00:00:48,210
So let's talk about the syntax for for loop.

13
00:00:48,600 --> 00:00:51,030
This is what the typical syntax for for loop looks like.

14
00:00:51,300 --> 00:00:53,390
Here I have my interval as a variable.

15
00:00:53,400 --> 00:00:55,830
It's equal to a list and then I'm performing the for loop.

16
00:00:56,190 --> 00:00:57,510
Let's break down what's happening here.

17
00:00:58,290 --> 00:01:01,710
So first, I'm actually assigning an interval object here.

18
00:01:02,010 --> 00:01:06,660
We can think of sequences as the adorable object here, and we have a list of one two three.

19
00:01:06,780 --> 00:01:14,820
Besides that variable, then you actually get to choose the name of a temporary variable that is going

20
00:01:14,820 --> 00:01:17,970
to be the placeholder for each item in that list.

21
00:01:18,090 --> 00:01:24,150
So it's going to be either the one, the two or three, and you're going to say for each item in that

22
00:01:24,150 --> 00:01:27,040
list in my interval.

23
00:01:27,060 --> 00:01:35,910
So note the syntax here you say for your made up variable name in then your object, you perform an

24
00:01:35,910 --> 00:01:36,420
action.

25
00:01:36,510 --> 00:01:39,960
This is a very simple action performed here, just printing out the items in the list.

26
00:01:40,320 --> 00:01:42,920
But that's all we need to do here to see the results.

27
00:01:42,930 --> 00:01:43,830
One two three.

28
00:01:44,070 --> 00:01:46,860
Notice how they are on separate lines each one.

29
00:01:47,040 --> 00:01:50,520
So it's going one at a time through each item in that list.

30
00:01:50,920 --> 00:01:53,550
Let's check this out in more detail in our code editor.

31
00:01:54,390 --> 00:02:00,020
OK, so we're going to do is just practice looping through an object so I can create my list here.

32
00:02:00,810 --> 00:02:06,150
And I'm going to say this list is the let's say, numbers one, two, three, four or five.

33
00:02:07,420 --> 00:02:10,539
And then how do I iterate through this list, I simply say for.

34
00:02:11,660 --> 00:02:13,740
Item in my list, Colon.

35
00:02:14,090 --> 00:02:16,770
And note, when you had colon enter, you're actually going in the patient.

36
00:02:17,210 --> 00:02:21,950
And so what's going to happen is I'm going to have this block of code execute for every single item

37
00:02:21,950 --> 00:02:22,490
in that list.

38
00:02:22,820 --> 00:02:28,730
So I could say go ahead and print out the item and note that this is technically just this little temporary

39
00:02:28,730 --> 00:02:29,300
variable.

40
00:02:29,750 --> 00:02:33,080
So if we save this, I should see one print it out.

41
00:02:33,290 --> 00:02:35,120
Then two, then three, then four and five.

42
00:02:36,170 --> 00:02:37,430
So we go ahead and run this.

43
00:02:38,120 --> 00:02:40,670
And there you have it one, two, three, four or five.

44
00:02:41,270 --> 00:02:45,660
Now what's interesting here is you can call this temporary variable whatever you want.

45
00:02:45,710 --> 00:02:48,900
So you can call it for jelly in my list.

46
00:02:48,920 --> 00:02:50,960
Go ahead and print jelly.

47
00:02:50,990 --> 00:02:54,830
I just want to make it clear that it's totally up to you what you call that variable.

48
00:02:55,310 --> 00:02:59,420
So if I run this code again, it still works just fine one, two, three, four or five.

49
00:02:59,960 --> 00:03:06,890
The other thing I want to note here is that you don't necessarily need to use the actual item in the

50
00:03:06,890 --> 00:03:07,310
list.

51
00:03:07,520 --> 00:03:12,530
This is just saying for every item in this list, go ahead and do something.

52
00:03:12,920 --> 00:03:16,670
So if I said for item in my list print.

53
00:03:18,700 --> 00:03:19,180
Hello.

54
00:03:19,780 --> 00:03:21,100
Then it's just going to print hello.

55
00:03:21,130 --> 00:03:21,940
Five times.

56
00:03:23,580 --> 00:03:30,090
This is just to give you the idea that you don't necessarily need to use each particular item in the

57
00:03:30,090 --> 00:03:30,510
list.

58
00:03:31,010 --> 00:03:33,600
Now what would be kind of a good use case for this?

59
00:03:33,930 --> 00:03:35,070
You could say something like.

60
00:03:36,790 --> 00:03:46,360
Prince, let's imagine that these were our sales numbers, so we can say for numb in sales just to give

61
00:03:46,360 --> 00:03:49,130
you an idea of kind of how you would set up a for loop.

62
00:03:49,330 --> 00:03:52,300
We have some sales here, one through a five four number in sales.

63
00:03:52,300 --> 00:03:58,120
I can use again if string literal formatting and say sales, for example.

64
00:03:59,260 --> 00:04:04,270
Sale of and then in curly braces person and um.

65
00:04:05,950 --> 00:04:12,580
Save that change and then run this, you see example sale one two three four five.

66
00:04:13,240 --> 00:04:16,600
OK, so that's the very basics of a for loop on a list.

67
00:04:17,050 --> 00:04:23,500
Again, you just set up the adorable object and then say for whatever temporary variable you want in

68
00:04:23,980 --> 00:04:26,770
that object, then you execute this block of code.

69
00:04:27,130 --> 00:04:28,750
It doesn't have to be a one liner here later on.

70
00:04:28,750 --> 00:04:29,920
This will be a more complicated.

71
00:04:30,430 --> 00:04:32,620
Now, I should note, this works the same for tuples.

72
00:04:32,770 --> 00:04:39,850
So if this were actually a tuples, so if I just change the square brackets for a tuple here, that's

73
00:04:39,850 --> 00:04:40,960
also totally OK.

74
00:04:41,020 --> 00:04:42,820
This will actually work just the same.

75
00:04:43,450 --> 00:04:46,000
Now what happens if this is a dictionary?

76
00:04:46,630 --> 00:04:48,910
So let's kind of set up a dictionary here.

77
00:04:50,280 --> 00:04:51,240
I'm going to say.

78
00:04:52,370 --> 00:04:53,120
Employees.

79
00:04:54,190 --> 00:04:55,210
Is equal to.

80
00:04:55,750 --> 00:04:57,340
And then let's say our CEO.

81
00:04:59,070 --> 00:05:00,180
Is Cindy.

82
00:05:01,230 --> 00:05:02,820
And let's say our CFO.

83
00:05:04,060 --> 00:05:04,810
Is.

84
00:05:05,840 --> 00:05:06,320
James.

85
00:05:07,690 --> 00:05:12,160
OK, then what's going to happen is if you loop through a dictionary, it's going to loop through the

86
00:05:12,160 --> 00:05:12,610
keys.

87
00:05:13,150 --> 00:05:15,520
So for key in.

88
00:05:16,560 --> 00:05:17,430
Employees.

89
00:05:18,680 --> 00:05:19,820
Let's say print the key.

90
00:05:20,270 --> 00:05:22,010
Just so we can explore what happens here.

91
00:05:22,400 --> 00:05:25,460
So we should expect to see CEO and then followed by CFO.

92
00:05:26,660 --> 00:05:27,000
OK.

93
00:05:27,350 --> 00:05:35,240
If I actually want to grab the value here, Cindy and James and print that out, I could be a little

94
00:05:35,240 --> 00:05:37,100
clever and say the following I could say.

95
00:05:38,600 --> 00:05:46,040
Employees and then grabbed the item at that particular key, so it actually pass and see you back into

96
00:05:46,040 --> 00:05:48,020
employees as it loops through these keys.

97
00:05:49,580 --> 00:05:51,290
And then we see Cindy and James.

98
00:05:51,800 --> 00:05:56,900
And so it would probably be better terminology here would be for position and employees.

99
00:05:57,290 --> 00:05:59,000
And then let's actually use both of them.

100
00:05:59,000 --> 00:06:02,600
I can print out using f string literals here.

101
00:06:02,900 --> 00:06:03,710
I can say.

102
00:06:04,840 --> 00:06:05,470
The.

103
00:06:06,660 --> 00:06:09,150
Position, let's make sure we use curly braces.

104
00:06:11,320 --> 00:06:12,460
The position.

105
00:06:13,730 --> 00:06:20,840
Is held by and then will actually then look up the employee inside the curly braces employees.

106
00:06:24,270 --> 00:06:25,290
At that position.

107
00:06:27,680 --> 00:06:28,050
OK.

108
00:06:28,070 --> 00:06:31,970
So again, let's break down what's happening here, I have this dictionary and I know when I loop through

109
00:06:31,970 --> 00:06:33,980
dictionaries, I actually have the keys.

110
00:06:34,000 --> 00:06:41,210
In this case, the keys in my dictionary employee stands for position so I can say the position it employs

111
00:06:41,540 --> 00:06:46,760
and then print out this string literal and her position and then look up who is the employee at that

112
00:06:46,760 --> 00:06:47,240
position.

113
00:06:47,690 --> 00:06:51,560
Hopefully, it's beginning to kind of change some gears in your mind or get those spinning.

114
00:06:51,960 --> 00:06:55,610
I can see the CEO's hold by saying the CFO is held by James.

115
00:06:56,900 --> 00:07:00,410
The last thing I want to point out is there is something called tuple unpacking.

116
00:07:00,710 --> 00:07:08,510
Remember, I said that it's really common to return back items in the forms of tuples and we can actually

117
00:07:08,510 --> 00:07:10,910
see a list of tuples as a result often.

118
00:07:11,270 --> 00:07:14,900
In fact, if you print out employees the items.

119
00:07:16,680 --> 00:07:22,490
Open close parentheses there and run this, I can see an actual list of tuples.

120
00:07:22,630 --> 00:07:24,330
Remember this the key value pairs.

121
00:07:24,900 --> 00:07:30,840
So because this is such a common way of returning back items, a list of tuple pairs.

122
00:07:31,110 --> 00:07:35,130
There's something called tuple and packing, and I want to show it to you now, just so it's not a complete

123
00:07:35,130 --> 00:07:36,720
surprise when you see it later.

124
00:07:37,110 --> 00:07:40,380
So I'm going to make a little artificial list and let's say my list.

125
00:07:40,830 --> 00:07:41,850
Square brackets.

126
00:07:42,330 --> 00:07:45,210
And then I'm going to set up some key value pairs.

127
00:07:45,210 --> 00:07:47,280
So I'm going to say a b.

128
00:07:49,670 --> 00:07:50,270
Comma.

129
00:07:51,210 --> 00:07:52,740
And then say, see the.

130
00:07:54,750 --> 00:07:57,930
So let's actually make one more here, just so it's really clear.

131
00:07:58,440 --> 00:07:59,730
And let's make this last one.

132
00:07:59,940 --> 00:08:00,600
One.

133
00:08:00,720 --> 00:08:01,950
Let's just say one and two.

134
00:08:02,820 --> 00:08:03,730
So what's happening here?

135
00:08:03,750 --> 00:08:05,400
I have a list.

136
00:08:05,970 --> 00:08:08,870
And in this list, there's three tuples.

137
00:08:09,630 --> 00:08:11,430
So I'm just going to space them out a little bit.

138
00:08:11,430 --> 00:08:13,050
So you can see clearly what's happening here.

139
00:08:13,440 --> 00:08:18,780
I have the three tuples, A, B, C D and then one two.

140
00:08:19,410 --> 00:08:21,210
Now look what happens if I were trying to.

141
00:08:22,190 --> 00:08:25,130
Iterate through this list for item in my list.

142
00:08:26,100 --> 00:08:31,220
Prince item, we should expect that to actually print each tuple and there's three pupils.

143
00:08:33,039 --> 00:08:35,860
And there it is, a b c d one two.

144
00:08:36,280 --> 00:08:41,289
But what if I actually wanted to get individual elements from those tuples, let's say only wanted a

145
00:08:41,440 --> 00:08:43,600
C and one and only one of the first items.

146
00:08:43,990 --> 00:08:45,790
This is where tuple and packing comes to play.

147
00:08:46,120 --> 00:08:52,780
Instead of writing something like item, I can write a little tuple here and say key or let's say,

148
00:08:52,780 --> 00:08:55,530
item one and then item two.

149
00:08:56,170 --> 00:08:58,210
And then I can specify I only want a printout.

150
00:08:58,240 --> 00:08:58,810
Item one.

151
00:08:59,380 --> 00:09:01,240
So this is known as tuple unpacking.

152
00:09:01,510 --> 00:09:07,150
Now, this only works if you know for a fact that in your list, you're getting objects returned back

153
00:09:07,270 --> 00:09:08,710
as tuple pairs like this.

154
00:09:08,890 --> 00:09:14,310
And this could extend if you have a third item in the tuple, you could say Item three, etc. But for

155
00:09:14,320 --> 00:09:16,330
now, we're just going to say I don't want item to.

156
00:09:17,390 --> 00:09:21,350
Go ahead and print this out, and we would expect to see a sea in one.

157
00:09:22,130 --> 00:09:30,230
And there it is, a sea and one because I'm saying grab the tuple items, but only print out item one.

158
00:09:30,710 --> 00:09:33,710
And technically speaking, you don't actually even need these parentheses here.

159
00:09:33,740 --> 00:09:39,080
So you typically don't see tuple and packing like this where just says four and then tuple item one

160
00:09:39,080 --> 00:09:42,440
comma tuple item two common to bottom three, etc. in my list.

161
00:09:42,530 --> 00:09:46,910
Then you can do something here and you can actually then print them out on separate lines so you can

162
00:09:46,910 --> 00:09:49,280
do whatever you want with these, so I can print out.

163
00:09:49,670 --> 00:09:54,800
Item one, then print out Item two, so take them over here and just think, what would you expect the

164
00:09:54,800 --> 00:09:56,360
output of this to be?

165
00:09:56,930 --> 00:10:03,530
Well, it's going to print out a then B, then C then D than one than to remember because it's going

166
00:10:03,530 --> 00:10:04,980
through each of these tuples.

167
00:10:05,300 --> 00:10:06,260
One at a time.

168
00:10:06,470 --> 00:10:07,640
Let's actually see the results here.

169
00:10:08,760 --> 00:10:14,220
And there it is, it's a then B, then C, then the then one and two, because remember, it's still

170
00:10:14,220 --> 00:10:16,140
going one two at a time.

171
00:10:16,140 --> 00:10:16,950
It's just up to you.

172
00:10:17,280 --> 00:10:19,230
Which items are printing out in which order?

173
00:10:19,830 --> 00:10:21,510
OK, so that's the very basics.

174
00:10:21,510 --> 00:10:22,110
A for loop.

175
00:10:22,320 --> 00:10:24,480
Main thing to understand is, do you have the four key word?

176
00:10:24,870 --> 00:10:30,200
Then you choose some temporary variable name in and then often we'll be dealing with this.

177
00:10:30,210 --> 00:10:34,620
But you can do this for many different types of objects and then you execute a block of code each time.

178
00:10:34,860 --> 00:10:39,480
And as you mentioned, technically, this block of code does not need to be related to the items in

179
00:10:39,480 --> 00:10:39,810
the list.

180
00:10:40,050 --> 00:10:41,580
I can still say something like prints.

181
00:10:42,180 --> 00:10:42,690
Hello.

182
00:10:43,640 --> 00:10:46,670
And this would still work, it just prints Halo three times, so I see.

183
00:10:46,700 --> 00:10:47,330
Hello, hello.

184
00:10:47,330 --> 00:10:47,710
Hello.

185
00:10:48,350 --> 00:10:48,800
OK.

186
00:10:49,370 --> 00:10:53,150
And that's it for four loops coming up next, we're going to talk about while loops.

