1
00:00:05,460 --> 00:00:10,020
Welcome back to this lecture on loop's We'll talk about for loops and while loops.

2
00:00:10,410 --> 00:00:11,970
Let's start by talking about four loops.

3
00:00:11,970 --> 00:00:16,820
First many objects in Python are what's known as iterable.

4
00:00:16,880 --> 00:00:22,870
That means we can use for loops to execute a block of code for every iteration through these objects.

5
00:00:23,090 --> 00:00:26,880
So that term iterable just means that you can iterate over the object.

6
00:00:26,900 --> 00:00:32,630
For example you can iterate over every character in a string iterate over every item in a list or iterate

7
00:00:32,660 --> 00:00:35,790
over every key in a dictionary.

8
00:00:35,880 --> 00:00:39,180
So let's talk about the actual Python syntax for a for loop.

9
00:00:39,180 --> 00:00:41,750
Let's imagine that my iterable object is a list.

10
00:00:41,760 --> 00:00:45,650
So I created a variable called my iterable and I said equal to this list.

11
00:00:45,690 --> 00:00:47,160
One two three.

12
00:00:47,160 --> 00:00:51,500
So what happens here is I say for item name in my iterable print the item name.

13
00:00:51,510 --> 00:00:53,660
So let's talk about the actual steps that's going on here.

14
00:00:54,530 --> 00:00:56,940
First I'd define an iterable object.

15
00:00:56,990 --> 00:01:02,120
In this case it's a list but this could be a string or a dictionary or a tuple or many other object

16
00:01:02,120 --> 00:01:02,930
types.

17
00:01:02,990 --> 00:01:07,090
So later on you've built an intuitive understanding of what's iterable and what's not.

18
00:01:07,130 --> 00:01:11,080
But right now we'll focus on lists since they're basically the most straightforward.

19
00:01:11,120 --> 00:01:16,430
So once you have your iterable defined then you're going to choose some sort of temporary variable name.

20
00:01:16,550 --> 00:01:21,940
So you're going to say 4 and this item underscore name you can choose whatever you want to plaister.

21
00:01:21,950 --> 00:01:28,580
So in this case I chose underscore name and then we say the in keyword and then we say my iterable basically

22
00:01:28,580 --> 00:01:29,470
that variable.

23
00:01:29,480 --> 00:01:34,780
So for every item in my iterable colon and then we're going to execute a block of code.

24
00:01:35,000 --> 00:01:38,590
In this case I can print out every single item inside of the iterable.

25
00:01:38,750 --> 00:01:41,990
So the output here would just be those numbers 1 2 and 3.

26
00:01:41,990 --> 00:01:47,110
Basically for every item in that list go ahead and print out that item.

27
00:01:47,250 --> 00:01:54,070
Now let's talk about while loops while loops will continue to execute a block of code while some condition

28
00:01:54,070 --> 00:01:55,140
remains true.

29
00:01:55,180 --> 00:02:01,990
For example while my pool is not fool keep filling my pool with water or while my dogs are still hungry

30
00:02:02,140 --> 00:02:04,040
keep feeding my dogs.

31
00:02:04,090 --> 00:02:06,520
So let's talk about the syntax of an actual wild.

32
00:02:06,820 --> 00:02:11,170
Here we're going to say while some boolean condition and then we're going to execute a block of code

33
00:02:11,170 --> 00:02:14,580
continually until that boolean condition is no longer true.

34
00:02:14,770 --> 00:02:19,330
So often we're going to have some sort of way to effect that boolean condition inside the while loop

35
00:02:19,330 --> 00:02:21,420
itself.

36
00:02:21,430 --> 00:02:26,500
Now you can also combine a while and else if you want to show you could say while some boolean condition

37
00:02:26,530 --> 00:02:33,130
do something else do something different although often you just see while loops by themselves ok let's

38
00:02:33,130 --> 00:02:38,230
explore all these concepts by heading over to the Adam text editor and working through some examples.

39
00:02:38,230 --> 00:02:41,620
Ok let's begin by showing some examples of a for loop.

40
00:02:41,620 --> 00:02:46,890
I'm going to start by using a list so I created s e q as my variable name.

41
00:02:46,900 --> 00:02:52,770
You can really choose any variable name you want and I'm going to create a list the numbers 1 2 3 4.

42
00:02:52,790 --> 00:02:55,950
So let's imagine I wanted to perform some sort of action with each element.

43
00:02:56,030 --> 00:03:02,700
I could say for item in CQ or sequence.

44
00:03:02,950 --> 00:03:08,800
Go ahead and print that item and again notice how there's indentation here and this should be automatically

45
00:03:08,800 --> 00:03:10,650
done for you in any ID.

46
00:03:10,720 --> 00:03:14,410
If it's not then you should probably save this as a profile and check again.

47
00:03:14,820 --> 00:03:21,030
So now I'm going to run this code and if I scroll up I get to see one two three four five.

48
00:03:21,100 --> 00:03:24,760
So it went ahead and printed every single number in that list.

49
00:03:24,760 --> 00:03:28,250
What I could do is say maybe print every number squared.

50
00:03:28,420 --> 00:03:32,770
So I'll take the item square it and then print out the result.

51
00:03:32,770 --> 00:03:39,420
So let's run those results and I see 1 4 9 16 25.

52
00:03:39,420 --> 00:03:43,590
Now often beginners get confused they say well we're that this item come from I didn't see the find

53
00:03:43,590 --> 00:03:44,300
anywhere.

54
00:03:44,550 --> 00:03:49,410
Technically it's just a temporary variable name for the iteration through the sequence so you could

55
00:03:49,410 --> 00:03:54,510
have this be anything and I like to make something weird here just to make that very clear so I could

56
00:03:54,510 --> 00:03:56,940
say for jellyfish in the queue.

57
00:03:57,150 --> 00:03:59,470
Go ahead and take jellyfish.

58
00:03:59,580 --> 00:04:01,740
And maybe times it by 2.

59
00:04:01,740 --> 00:04:03,630
So again it's totally up to you.

60
00:04:03,840 --> 00:04:09,210
What term is going to be chosen for your sequence throughout that iteration.

61
00:04:09,210 --> 00:04:11,790
Now often you're probably not going to want to use something like jellyfish.

62
00:04:11,790 --> 00:04:14,070
Instead you should choose something pretty irrelevant.

63
00:04:14,100 --> 00:04:19,410
Maybe nom would be a good one in this case because there numbers so this is how I would do this.

64
00:04:19,410 --> 00:04:24,540
I would say for them sequence print name but keep in mind any variable name there is OK.

65
00:04:25,660 --> 00:04:31,000
So something else you should know is when you're saying for some item in a sequence you don't necessarily

66
00:04:31,000 --> 00:04:33,200
need to actually use that item in the sequence.

67
00:04:33,400 --> 00:04:37,660
Let's say I just wanted to print Hello five times.

68
00:04:37,810 --> 00:04:40,960
This case for every number in that sequence 1 2 3 4 5.

69
00:04:40,980 --> 00:04:42,610
I'm going to perform the action.

70
00:04:42,610 --> 00:04:47,450
Notice I'm not actually using numb instead I'm using almost this as some sort of counting device.

71
00:04:47,740 --> 00:04:49,380
So now when I run this I get back.

72
00:04:49,390 --> 00:04:50,700
Hello hello hello hello.

73
00:04:50,740 --> 00:04:54,320
Just one hello for each item in this list.

74
00:04:54,400 --> 00:04:57,810
OK so that's how a for loop works of a list.

75
00:04:57,820 --> 00:05:00,490
Let's see how it works now with a string.

76
00:05:00,940 --> 00:05:09,700
So I'll say hello is a string and I want to save for now I'm going to make this actually say C H A R

77
00:05:10,270 --> 00:05:13,910
because it's more reflective what it's going to do with a string.

78
00:05:14,230 --> 00:05:23,260
So I'm going to say my string is equal to hello and for C H R for character in my string.

79
00:05:23,260 --> 00:05:25,290
Go ahead and print out that character.

80
00:05:25,390 --> 00:05:30,760
So when you actually for loop through a string you basically loop through every single character in

81
00:05:30,760 --> 00:05:31,400
that string.

82
00:05:31,540 --> 00:05:35,320
So for a list every item or element in that list for strings.

83
00:05:35,320 --> 00:05:37,430
It's every character in that string.

84
00:05:37,570 --> 00:05:38,800
Then for dictionaries.

85
00:05:38,920 --> 00:05:40,300
Let's go ahead to make a dictionary.

86
00:05:41,200 --> 00:05:47,590
We'll say salary's again and we'll say John 10.

87
00:05:47,680 --> 00:05:59,450
Let's have Sally B 20 and let's say Lisa is 30 I'm going to start this again for.

88
00:05:59,600 --> 00:06:03,440
And when you loop through a dictionary what's going to happen is you get a loop through the keys of

89
00:06:03,440 --> 00:06:11,710
the dictionary so you can say for key in salary's print the key.

90
00:06:11,730 --> 00:06:17,330
So when I read this and I loop through a dictionary I see all the keys John Salley and Lisa.

91
00:06:17,430 --> 00:06:20,230
So something to know here is you could technically call this whatever you want.

92
00:06:20,490 --> 00:06:26,780
So maybe it makes more sense to call this four employee in salary's again to really taking advantage

93
00:06:26,840 --> 00:06:29,990
of Python's clean syntax to make code very readable.

94
00:06:30,200 --> 00:06:31,670
So here it's very clear what's happening.

95
00:06:31,670 --> 00:06:36,910
I'm saying for employee in salary's Prince employ and there we go.

96
00:06:36,950 --> 00:06:38,420
John Salley and Lisa.

97
00:06:38,510 --> 00:06:42,320
Now let's imagine actually wanted the values associated with those keys.

98
00:06:42,330 --> 00:06:50,190
Well all we need to do is take my salary's dictionary and then index it for that particular employee.

99
00:06:50,280 --> 00:06:53,310
So I'm going to grab that employee and paste it in there.

100
00:06:54,160 --> 00:06:58,260
And let's print out salary's for that employee.

101
00:07:00,460 --> 00:07:06,910
So now when I run this piece of code I see 10 20 30 and if I want all the information I can just use

102
00:07:07,000 --> 00:07:08,310
multiple print statements.

103
00:07:08,470 --> 00:07:18,710
So Nel's prints employ here and let's do some more prints so I can say the let's say employ

104
00:07:22,230 --> 00:07:25,000
has a salary of.

105
00:07:25,190 --> 00:07:29,870
And then we'll print out that salary and I'm going to print out a blank line there.

106
00:07:30,090 --> 00:07:35,070
So save that and zoom a little bit so we can see the whole picture here and collapse this little bit.

107
00:07:35,310 --> 00:07:36,600
So have a salary's dictionary.

108
00:07:36,600 --> 00:07:39,150
I'm saying for every employee in salaries.

109
00:07:39,150 --> 00:07:43,290
Remember when you loop through a dictionary it's going to default through the Keys that dictionary.

110
00:07:43,590 --> 00:07:48,120
So then in print that the employee has a salary of and they're going to grab that associate a key value

111
00:07:48,150 --> 00:07:50,880
pair and then prints a blank line.

112
00:07:50,880 --> 00:07:53,130
So when I run this I get back.

113
00:07:53,130 --> 00:07:55,080
Lisa has a salary of 30.

114
00:07:55,080 --> 00:07:56,410
Salary a salary of 20.

115
00:07:56,580 --> 00:07:58,340
And John has a salary of ten.

116
00:07:58,350 --> 00:08:03,000
So again just keep in mind when you loop through a dictionary it's going to loop through the Keys by

117
00:08:03,000 --> 00:08:04,170
default.

118
00:08:04,170 --> 00:08:08,130
Now the last special thing I want to mention is something called tuple unpacking.

119
00:08:08,370 --> 00:08:15,690
As you begin to work a lot with Python you'll notice that often functions return a list of tuple pairs.

120
00:08:15,740 --> 00:08:22,530
So you'll see something like this a one.

121
00:08:23,080 --> 00:08:26,930
And then maybe C-3.

122
00:08:26,930 --> 00:08:28,150
So notice what's happening here.

123
00:08:28,160 --> 00:08:30,980
I have a list of tuples.

124
00:08:31,130 --> 00:08:38,470
So if I were to print out the length of my pairs I would expect a length of three because there's three

125
00:08:38,470 --> 00:08:41,460
tuples inside of this my pairs list.

126
00:08:41,470 --> 00:08:42,670
So here you have these tuples.

127
00:08:42,670 --> 00:08:44,200
There's three of them.

128
00:08:44,230 --> 00:08:53,590
So what happens when actually loop through this I'll say for item in my pairs print the item will save

129
00:08:53,600 --> 00:09:00,370
that and run it and I get back each particular tuple which is fine but because this sort of structure

130
00:09:00,400 --> 00:09:07,090
is so common in Python having a list of tuples where you can do something called tuple and packing so

131
00:09:07,120 --> 00:09:12,190
you could do is instead of saying for item and each item being a tuple you can actually have a little

132
00:09:12,190 --> 00:09:15,680
tuple here and then say something like.

133
00:09:15,870 --> 00:09:18,630
Item one item two.

134
00:09:18,980 --> 00:09:25,620
And then you can print out item 1 and print out item 2.

135
00:09:25,620 --> 00:09:32,500
So this is actually going to deconstruct or unpack the tuple as you are going through your for loop.

136
00:09:32,550 --> 00:09:33,720
So now look what you see.

137
00:09:33,720 --> 00:09:40,650
You see one you see a one BE2 and then see three because you're printing out first these letters and

138
00:09:40,650 --> 00:09:42,360
then printing out these numbers.

139
00:09:42,360 --> 00:09:45,960
So you're unpacking the tuple inside of your for loop.

140
00:09:45,960 --> 00:09:47,990
Now you actually don't need these princes here.

141
00:09:48,000 --> 00:09:49,290
Instead you could say Item one.

142
00:09:49,290 --> 00:09:53,210
Item two and my pairs and this by itself will also work just fine.

143
00:09:53,340 --> 00:09:54,950
To get that exact same result.

144
00:09:54,960 --> 00:09:57,890
I just wanted to show you with the princes to make it clear.

145
00:09:57,930 --> 00:09:59,390
The idea of what's happening.

146
00:09:59,790 --> 00:10:05,130
And again we can choose whatever variable names we want for these particular items inside of this list.

147
00:10:05,130 --> 00:10:11,750
So it might make more sense to do something like four letter numb in my pairs.

148
00:10:12,100 --> 00:10:12,660
Prince

149
00:10:16,200 --> 00:10:17,940
and again you don't have to use both of these.

150
00:10:17,940 --> 00:10:21,000
Maybe I only want to print the letters inside of those pairs.

151
00:10:21,030 --> 00:10:24,270
So after you save it you can run it and just get back the letters.

152
00:10:24,270 --> 00:10:25,750
Or maybe you only want the numbers.

153
00:10:25,890 --> 00:10:29,250
In that case just print out them save that.

154
00:10:29,250 --> 00:10:31,760
Run it again and easy one two three.

155
00:10:31,770 --> 00:10:38,370
Again this is called tuple unpacking and it's a pretty important topic because you see this sort of

156
00:10:38,370 --> 00:10:41,860
structure a lot and some more advanced Python functions.

157
00:10:41,970 --> 00:10:44,580
OK so that's the basics of for loops.

158
00:10:44,580 --> 00:10:50,640
Now let's quickly discuss how we can use while loops and we'll also talk about a few other topics such

159
00:10:50,640 --> 00:11:00,280
as the in keyword and the range function so let's say that so remember that while loops will continually

160
00:11:00,280 --> 00:11:04,310
perform action until that condition becomes true.

161
00:11:04,340 --> 00:11:06,770
So I will set a variable.

162
00:11:06,770 --> 00:11:23,820
I equal to 1 and I will say while I is less than 5 go ahead and Prince I is currently and then I'll

163
00:11:23,960 --> 00:11:26,780
go ahead and use an F string literal to Prince.

164
00:11:26,810 --> 00:11:29,640
I so what was happening here him say is equal one.

165
00:11:29,690 --> 00:11:34,550
And then while I was less than five I'll say I is currently in the current value of i.

166
00:11:34,790 --> 00:11:39,860
Now if I were to just run this by itself right now it would go on forever because I'm not changing the

167
00:11:39,860 --> 00:11:42,260
value of I one will always be less than five.

168
00:11:42,470 --> 00:11:44,800
So this would constantly Forever print out eyes.

169
00:11:44,810 --> 00:11:47,710
Currently one is currently one is currently one.

170
00:11:47,720 --> 00:11:53,800
So in order to make sure that we don't get a forever while loop peer analysts a prince actually not

171
00:11:53,840 --> 00:12:00,720
going to print anything I'll just say I is not going to be equal to I plus one.

172
00:12:00,750 --> 00:12:02,110
So what does that mean.

173
00:12:02,130 --> 00:12:04,320
It means I started off as one.

174
00:12:04,440 --> 00:12:10,290
I'm saying while I less than 5 print out the current value of i and then take the current value of i

175
00:12:10,650 --> 00:12:13,510
add 1 to it and reset that to be I.

176
00:12:13,710 --> 00:12:16,140
So then that's going to become two then three.

177
00:12:16,150 --> 00:12:18,520
Then it's four and eventually it'll be five.

178
00:12:18,570 --> 00:12:22,100
And this while loop will no longer execute so it should stop by printing.

179
00:12:22,140 --> 00:12:26,260
I's currently 4 and by the time I get added 1 and it's 5.

180
00:12:26,340 --> 00:12:28,990
This while loop ceases to continue.

181
00:12:29,280 --> 00:12:31,950
So let's run this and see what we get.

182
00:12:31,960 --> 00:12:37,960
So we say eyes currently 1 2 3 4 and on that very last iteration of the while loop we said 4 plus 1

183
00:12:37,960 --> 00:12:40,750
is 5 and 5 is no longer less than 5.

184
00:12:40,780 --> 00:12:42,900
So this while loop terminates.

185
00:12:42,910 --> 00:12:45,160
OK that's the very basic idea of a while loop.

186
00:12:45,310 --> 00:12:49,070
You want to be really careful with this condition because if you do something like.

187
00:12:49,270 --> 00:12:52,920
While one is equal to 1 then it doesn't matter what you put here.

188
00:12:52,930 --> 00:12:59,020
This is always going to be true so it will go on forever and you want to use Control-C to stop permanent

189
00:12:59,020 --> 00:13:03,150
while loops OK we just discussed for loops and while loops.

190
00:13:03,230 --> 00:13:07,700
The last thing I want to do is discuss a few more useful operators.

191
00:13:07,700 --> 00:13:12,310
One is the range function which can quickly generate a list of integers for you.

192
00:13:12,560 --> 00:13:20,870
So if you use Range what you can do is signify a start a stop and a step size so you can say range from

193
00:13:20,870 --> 00:13:28,220
0 to 5 and you can actually loop through this or iterate through it so I can say for x in range 0 to

194
00:13:28,230 --> 00:13:36,860
5 print X and what range does it automatically generates integers for you.

195
00:13:36,860 --> 00:13:38,650
So now I want to run this I get back.

196
00:13:38,930 --> 00:13:40,950
0 1 2 3 4.

197
00:13:41,000 --> 00:13:45,690
So it starts at 0 and then goes up to but not including 5.

198
00:13:46,130 --> 00:13:52,790
So again you can kind of play around with this maybe do 11 so that will go all the way up to but not

199
00:13:52,790 --> 00:13:53,770
including 11.

200
00:13:53,990 --> 00:13:55,110
So we'll see.

201
00:13:55,370 --> 00:14:01,210
0 1 2 3 4 5 6 7 8 9 10 and something else to know is that you can add a third parameter.

202
00:14:01,250 --> 00:14:02,230
That's a step size.

203
00:14:02,360 --> 00:14:07,270
So hopefully this feels really really reminiscent of slicing a list or slicing a string.

204
00:14:07,270 --> 00:14:12,730
So if you want to take jumps and two cause by default it just jumps one integer at a time.

205
00:14:13,840 --> 00:14:16,510
When you run this you get back 0 2 4 6 8 10.

206
00:14:16,510 --> 00:14:17,990
So again that's the step size.

207
00:14:18,070 --> 00:14:21,250
So you have a start in exclusive stop.

208
00:14:21,280 --> 00:14:25,600
So it goes up to and not including a stopping point and then a step size.

209
00:14:25,600 --> 00:14:36,060
Now if you just want the list itself where you need to do is cast this into a list and then say something

210
00:14:36,060 --> 00:14:40,970
like result is equal to that list and then if you aren't you can then print that result.

211
00:14:43,030 --> 00:14:47,140
So if you don't intend on actually iterating through this range instead you actually want the raw result

212
00:14:47,200 --> 00:14:48,010
as a list.

213
00:14:48,010 --> 00:14:51,650
You can just cast it as a list and then get back to that list.

214
00:14:51,700 --> 00:14:55,960
OK the last keyword I want to dive in a little more on is the in keyword.

215
00:14:56,110 --> 00:15:00,410
We already saw as a keyword for 4 loops but I just want to clarify.

216
00:15:00,450 --> 00:15:04,420
You can actually use it to test if something is in an iterable.

217
00:15:04,420 --> 00:15:06,170
So let's say I want to check.

218
00:15:06,190 --> 00:15:09,700
Let's say I have a really long string so I'm just going to type a bunch of code there and I want to

219
00:15:09,700 --> 00:15:13,940
check is the letter S inside of that string.

220
00:15:14,010 --> 00:15:20,280
All I can say s in this string and then I can actually print out the result of this.

221
00:15:20,500 --> 00:15:26,270
So I just print s in right here and this should return back a boolean value.

222
00:15:26,440 --> 00:15:30,080
And in this case it's true because S isn't here.

223
00:15:30,190 --> 00:15:31,380
Now let's put Z.

224
00:15:31,690 --> 00:15:33,660
I can tell that zees not in there.

225
00:15:33,850 --> 00:15:36,390
So I run this they'll give me back false.

226
00:15:36,580 --> 00:15:40,010
So this works both for strings and for a list.

227
00:15:40,150 --> 00:15:43,510
So let's move this and I'll say one two three.

228
00:15:43,630 --> 00:15:48,910
So I'm going to ask is the string Z an element inside of this list.

229
00:15:48,910 --> 00:15:50,230
You run that you get false.

230
00:15:50,260 --> 00:15:52,320
So let's change it to 1.

231
00:15:52,330 --> 00:15:55,030
So now is 1 inside of this list.

232
00:15:55,030 --> 00:15:56,650
Run that and that's true.

233
00:15:56,650 --> 00:15:59,140
So that's the in keyword operator by itself.

234
00:15:59,150 --> 00:16:00,620
Kind of outside of a for loop.

235
00:16:00,640 --> 00:16:03,330
You can just check if an element is inside.

236
00:16:03,340 --> 00:16:04,790
In other iterable element.

237
00:16:05,200 --> 00:16:11,500
OK so we talked about range in the keyword and then while loops and for loops as well as tuple and packing.

238
00:16:11,500 --> 00:16:14,050
Kind of a special condition for for loops.

239
00:16:14,260 --> 00:16:17,240
If you have any questions feel free to post the Kewney forums.

240
00:16:17,290 --> 00:16:18,640
We'll see you at the next lecture.
