WEBVTT

1
00:00:00.130 --> 00:00:01.890
<v Instructor>Hi guys and welcome back!</v>

2
00:00:01.890 --> 00:00:04.970
In this video we're going to talk about Loops in Python.

3
00:00:04.970 --> 00:00:06.220
There are two types of Loop.

4
00:00:06.220 --> 00:00:08.460
One that allows us to repeat some

5
00:00:08.460 --> 00:00:10.590
code a certain number of times.

6
00:00:10.590 --> 00:00:12.400
And one that allows us to repeat

7
00:00:12.400 --> 00:00:15.910
code for as long as a condition is true.

8
00:00:15.910 --> 00:00:18.636
Let's start with that one in this video.

9
00:00:18.636 --> 00:00:21.797
So, we've got our code from the last video here

10
00:00:21.797 --> 00:00:24.850
where we ask the user where they want to play.

11
00:00:24.850 --> 00:00:27.860
And then, if the user's input was y,

12
00:00:27.860 --> 00:00:29.390
we ask them to get our number.

13
00:00:29.390 --> 00:00:31.210
And we told them whether the number was correct.

14
00:00:31.210 --> 00:00:33.258
The number being defined up here.

15
00:00:33.258 --> 00:00:35.140
Now, what we're going to do is,

16
00:00:35.140 --> 00:00:37.940
we're going to repeat the game

17
00:00:37.940 --> 00:00:41.700
over and over until the user asks us to stop.

18
00:00:41.700 --> 00:00:43.360
So, what I'm going to do is, I'm going to change

19
00:00:43.360 --> 00:00:46.680
this prompt a little bit before we begin.

20
00:00:46.680 --> 00:00:48.960
And I will say, "Would you like to play?"

21
00:00:48.960 --> 00:00:50.650
And then, I would give them the

22
00:00:50.650 --> 00:00:55.650
options of y in capital, or n in lowercase.

23
00:00:56.020 --> 00:00:59.250
This is a common convention in terminal based programmes

24
00:00:59.250 --> 00:01:00.740
where you use one of the options

25
00:01:00.740 --> 00:01:02.800
as uppercase and one of them as lowercase.

26
00:01:02.800 --> 00:01:04.921
And that means that if they type anything

27
00:01:04.921 --> 00:01:09.510
other than n, y would be the default.

28
00:01:09.510 --> 00:01:12.210
So they have to specifically type n if they want to stop.

29
00:01:12.210 --> 00:01:13.570
This is a common convention,

30
00:01:13.570 --> 00:01:15.893
so you can use it in your own programmes as well.

31
00:01:15.893 --> 00:01:18.880
What that means is that now we want to repeat

32
00:01:18.880 --> 00:01:22.180
this code here, allowing the user to guess a number,

33
00:01:22.180 --> 00:01:24.280
and then telling them whether it's wrong or not,

34
00:01:24.280 --> 00:01:28.030
for as long as the user didn't type n.

35
00:01:28.030 --> 00:01:30.800
So, if the user types anything except n,

36
00:01:30.800 --> 00:01:32.590
we wanna run this code.

37
00:01:32.590 --> 00:01:35.800
So, we no longer need this if statement there.

38
00:01:35.800 --> 00:01:36.680
But what we want to do is,

39
00:01:36.680 --> 00:01:38.210
we want to use a while loop.

40
00:01:38.210 --> 00:01:41.501
So we will say, while the user input is not n,

41
00:01:41.501 --> 00:01:45.020
so while user input is not n,

42
00:01:45.020 --> 00:01:47.833
then we will repeat this code over and over.

43
00:01:49.580 --> 00:01:50.413
What happens here is that we've got

44
00:01:50.413 --> 00:01:52.696
the while keyword, then we've got a space,

45
00:01:52.696 --> 00:01:54.650
and then we have our condition.

46
00:01:54.650 --> 00:01:57.200
More or less likely if statement we have earlier on

47
00:01:57.200 --> 00:02:01.950
but now this code here, makes the indented code

48
00:02:01.950 --> 00:02:04.430
run over and over.

49
00:02:04.430 --> 00:02:06.350
And what happens is, it starts up at the top

50
00:02:06.350 --> 00:02:07.580
and it goes to the bottom,

51
00:02:07.580 --> 00:02:08.720
and then when it reaches the end

52
00:02:08.720 --> 00:02:10.560
of this block that's indented,

53
00:02:10.560 --> 00:02:14.410
it goes back to the top and evaluates this Boolean again.

54
00:02:14.410 --> 00:02:16.730
So, if this Boolean is true,

55
00:02:16.730 --> 00:02:18.030
then it will run this again,

56
00:02:18.030 --> 00:02:19.300
and then it will go back to the top,

57
00:02:19.300 --> 00:02:21.400
and evaluate the Boolean again, and so on.

58
00:02:22.340 --> 00:02:25.220
Of course, the first time that Python reaches line four,

59
00:02:25.220 --> 00:02:27.040
this Boolean is also evaluated.

60
00:02:27.040 --> 00:02:30.620
So, potentially the user could enter n to begin with

61
00:02:30.620 --> 00:02:33.440
and then this loop would not run at all.

62
00:02:33.440 --> 00:02:36.023
So, that is how the y loop works.

63
00:02:37.620 --> 00:02:40.800
Now, of course if the user enters y,

64
00:02:40.800 --> 00:02:42.676
then y would not be equal to n,

65
00:02:42.676 --> 00:02:45.280
that's true, so this would run.

66
00:02:45.280 --> 00:02:47.080
And then we we'll go back to the top,

67
00:02:47.080 --> 00:02:49.090
and this would still be y.

68
00:02:49.090 --> 00:02:50.450
And it still wouldn't be equal to n,

69
00:02:50.450 --> 00:02:51.700
so this would run again.

70
00:02:51.700 --> 00:02:53.520
And so on, forever.

71
00:02:53.520 --> 00:02:58.040
If you don't change the condition here, then you're gonna

72
00:02:58.040 --> 00:02:59.730
end up in an infinite loop.

73
00:02:59.730 --> 00:03:01.800
So, what we have to do to begin with is we have to ask

74
00:03:01.800 --> 00:03:04.330
the user whether they wanna play again.

75
00:03:04.330 --> 00:03:05.300
So, what we'll do is, will we copy

76
00:03:05.300 --> 00:03:07.363
this line here just for now.

77
00:03:08.590 --> 00:03:10.980
And we will put it inside the loop,

78
00:03:10.980 --> 00:03:13.640
but not inside the else.

79
00:03:13.640 --> 00:03:15.930
So, just down here in the same indentation level

80
00:03:15.930 --> 00:03:17.080
as all the other lines,

81
00:03:17.080 --> 00:03:18.370
we ask the user whether they want to play again.

82
00:03:18.370 --> 00:03:20.980
And what that means is that when we go back

83
00:03:20.980 --> 00:03:22.855
to reevaluate this condition,

84
00:03:22.855 --> 00:03:25.116
the user can potentially affect that

85
00:03:25.116 --> 00:03:27.493
and potentially terminate the loop.

86
00:03:28.660 --> 00:03:32.000
However, there is a small issue in here,

87
00:03:32.000 --> 00:03:34.540
which is that you have these two lines duplicated.

88
00:03:34.540 --> 00:03:36.130
So, if you ever wanna change your prompt

89
00:03:36.130 --> 00:03:37.870
for example you're gonna have to change it in both

90
00:03:37.870 --> 00:03:40.713
places and that can be a little bit cumbersome.

91
00:03:41.670 --> 00:03:43.230
So, instead what we're gonna do is,

92
00:03:43.230 --> 00:03:45.349
we're going to get rid of it and introduce

93
00:03:45.349 --> 00:03:48.972
another Python keyword that allows us to do that.

94
00:03:48.972 --> 00:03:52.230
Here, I'm going to change this so it no longer

95
00:03:52.230 --> 00:03:54.030
uses the condition.

96
00:03:54.030 --> 00:03:56.595
Instead I'm going to do while true.

97
00:03:56.595 --> 00:04:00.200
This always creates an infinite loop.

98
00:04:00.200 --> 00:04:02.260
So, now this code will run and then we will

99
00:04:02.260 --> 00:04:03.150
go back to the top.

100
00:04:03.150 --> 00:04:05.230
This is still true, so it runs again.

101
00:04:05.230 --> 00:04:06.971
And so on, forever.

102
00:04:06.971 --> 00:04:09.980
If you use while true,

103
00:04:09.980 --> 00:04:13.160
then you must be able to terminate the loop

104
00:04:13.160 --> 00:04:16.500
from within the loop's code.

105
00:04:16.500 --> 00:04:17.730
So what I'm going to do,

106
00:04:17.730 --> 00:04:19.770
is I am going to move this line

107
00:04:20.910 --> 00:04:22.453
into the loop.

108
00:04:23.310 --> 00:04:25.400
And then after the line,

109
00:04:25.400 --> 00:04:26.450
I'm going to say,

110
00:04:26.450 --> 00:04:30.180
if user input is equal to n,

111
00:04:30.180 --> 00:04:32.490
and here comes the break keyword.

112
00:04:32.490 --> 00:04:35.940
The break keyword allows us to exit a loop.

113
00:04:35.940 --> 00:04:37.919
So, because we're inside the loop here

114
00:04:37.919 --> 00:04:41.990
we can break out of it and that terminates it and continues

115
00:04:41.990 --> 00:04:44.490
our Python code from the next line after the loop.

116
00:04:45.340 --> 00:04:46.470
So what we've done here is,

117
00:04:46.470 --> 00:04:48.190
within the loop, we ask the user whether

118
00:04:48.190 --> 00:04:51.600
they'd like to play, and if they type no, then we terminate.

119
00:04:51.600 --> 00:04:53.080
And if they type anything else,

120
00:04:53.080 --> 00:04:54.910
then we continue with the loop.

121
00:04:54.910 --> 00:04:56.860
And that happens over and over.

122
00:04:56.860 --> 00:04:59.490
This is an alternative way of doing the loop

123
00:04:59.490 --> 00:05:01.080
that you saw just a moment ago.

124
00:05:01.080 --> 00:05:04.128
You can do either this one does reduce duplication

125
00:05:04.128 --> 00:05:07.280
but it adds an extra if statement inside your loop.

126
00:05:07.280 --> 00:05:09.110
So, its up to you which one you wanna do.

127
00:05:09.110 --> 00:05:12.650
Usually though, I would go for something like this.

128
00:05:12.650 --> 00:05:13.650
That's everything I wanted to show

129
00:05:13.650 --> 00:05:15.530
you regarding the y loop.

130
00:05:15.530 --> 00:05:17.963
So, let's take a look at the For loop.

131
00:05:19.160 --> 00:05:21.700
Let's say you've got a list of your friends

132
00:05:21.700 --> 00:05:23.260
Rolf, Jen, Bob, and Anne.

133
00:05:23.260 --> 00:05:27.050
And you want to print out a simple stream for each friend.

134
00:05:27.050 --> 00:05:29.560
For example, you want to print out Rolf is my friend.

135
00:05:29.560 --> 00:05:30.393
Jen is my friend.

136
00:05:30.393 --> 00:05:31.226
Bob is my friend.

137
00:05:31.226 --> 00:05:32.061
Anne is my friend.

138
00:05:32.061 --> 00:05:35.510
The simple way to do that without loops,

139
00:05:35.510 --> 00:05:37.510
is of course to do something like print,

140
00:05:38.790 --> 00:05:41.633
friend zero is my friend.

141
00:05:42.567 --> 00:05:46.120
And then repeat that for one, two, and three.

142
00:05:46.120 --> 00:05:49.050
Obviously, as the list of friends grows,

143
00:05:49.050 --> 00:05:51.090
these get increasingly difficult.

144
00:05:51.090 --> 00:05:54.320
Plus, if you have this list as part of a programme,

145
00:05:54.320 --> 00:05:56.640
and you wanna be able to print out that all

146
00:05:56.640 --> 00:05:58.265
your friends, are your friends,

147
00:05:58.265 --> 00:06:00.210
you're not gonna be able to do that without

148
00:06:00.210 --> 00:06:01.640
changing the code.

149
00:06:01.640 --> 00:06:04.220
So, here's where the loop comes in handy.

150
00:06:04.220 --> 00:06:06.856
We're going to repeat this structure

151
00:06:06.856 --> 00:06:09.640
for each of your friends.

152
00:06:09.640 --> 00:06:11.000
So, what we'll do is we will say,

153
00:06:11.000 --> 00:06:13.915
for friend in friends.

154
00:06:13.915 --> 00:06:16.907
And then we will indent this piece of code.

155
00:06:16.907 --> 00:06:20.398
And here we will use the variable of friend.

156
00:06:20.398 --> 00:06:22.220
How the For loop works is,

157
00:06:22.220 --> 00:06:24.130
you have the for keyword,

158
00:06:24.130 --> 00:06:25.580
then you have your space,

159
00:06:25.580 --> 00:06:28.540
then you define a variable

160
00:06:28.540 --> 00:06:33.170
that will take the first value of this list.

161
00:06:33.170 --> 00:06:34.950
So, Rolf.

162
00:06:34.950 --> 00:06:37.440
And then you have in friends.

163
00:06:37.440 --> 00:06:39.600
That's the list that you wanna go over.

164
00:06:39.600 --> 00:06:43.500
And what this allows you to do, is to use this variable

165
00:06:43.500 --> 00:06:47.273
and make it equal to the first value in this list,

166
00:06:47.273 --> 00:06:49.417
and then run this code.

167
00:06:49.417 --> 00:06:53.040
When you reach the end of the indented code, it goes

168
00:06:53.040 --> 00:06:57.180
back to the top, and friend takes the second value.

169
00:06:57.180 --> 00:06:58.930
And then it runs this code and it then it goes

170
00:06:58.930 --> 00:07:00.100
right to the top, and friend takes

171
00:07:00.100 --> 00:07:01.630
the third value and so on.

172
00:07:01.630 --> 00:07:03.270
When you reach the end,

173
00:07:03.270 --> 00:07:06.300
the For loop structure knows that you are done.

174
00:07:06.300 --> 00:07:07.770
And then it continues running the Python

175
00:07:07.770 --> 00:07:10.200
code from the next line onwards.

176
00:07:10.200 --> 00:07:13.010
So, this is great when you wanna do something for each

177
00:07:13.010 --> 00:07:16.610
element in a list, or a tuple, or a set and so on.

178
00:07:16.610 --> 00:07:18.610
And also when you wanna repeat something

179
00:07:18.610 --> 00:07:20.373
a certain number of times.

180
00:07:21.700 --> 00:07:23.380
For example, here you know you have four

181
00:07:23.380 --> 00:07:25.110
elements in this list.

182
00:07:25.110 --> 00:07:28.480
So therefore this loop will run four times.

183
00:07:28.480 --> 00:07:31.100
If you wanted to run a loop four times,

184
00:07:31.100 --> 00:07:34.550
and you didn't care to create a list of four elements,

185
00:07:34.550 --> 00:07:37.670
you can actually do for variable

186
00:07:37.670 --> 00:07:40.730
in range four.

187
00:07:40.730 --> 00:07:43.990
And that is also going to allow you to repeat

188
00:07:43.990 --> 00:07:45.780
this code four times.

189
00:07:45.780 --> 00:07:49.504
Of course the friend variable still is used.

190
00:07:49.504 --> 00:07:51.340
So, if you run this code, you'll actually

191
00:07:51.340 --> 00:07:53.303
see that you'd get back.

192
00:07:54.170 --> 00:07:55.210
Zero is my friend.

193
00:07:55.210 --> 00:07:56.043
One is my friend.

194
00:07:56.043 --> 00:07:56.876
Two is my friend

195
00:07:56.876 --> 00:07:57.709
Three is my friend.

196
00:07:57.709 --> 00:07:59.740
Because when you do range four, that creates

197
00:07:59.740 --> 00:08:01.400
something like a list

198
00:08:01.400 --> 00:08:03.290
where the first element is zero,

199
00:08:03.290 --> 00:08:04.123
the second one is one,

200
00:08:04.123 --> 00:08:05.000
and so on.

201
00:08:05.000 --> 00:08:07.210
This is a construct that allows you to iterate

202
00:08:07.210 --> 00:08:11.560
over a list of numbers so that you can repeat the code

203
00:08:11.560 --> 00:08:13.550
a certain number of times.

204
00:08:13.550 --> 00:08:14.870
Usually though for a for loop,

205
00:08:14.870 --> 00:08:18.830
you'll be using something like an existing list and do

206
00:08:18.830 --> 00:08:20.230
something with each element.

207
00:08:21.880 --> 00:08:24.640
Let's use a For loop to calculate the average

208
00:08:24.640 --> 00:08:27.770
grade of this class.

209
00:08:27.770 --> 00:08:29.490
So, let's say we've got a list of grades

210
00:08:29.490 --> 00:08:30.323
where the numbers are

211
00:08:30.323 --> 00:08:33.370
35, 67, 98, and two 100s.

212
00:08:33.370 --> 00:08:34.970
And we have a variable total

213
00:08:34.970 --> 00:08:36.330
which is equal to zero.

214
00:08:36.330 --> 00:08:38.980
Then we also have an amount of grades,

215
00:08:38.980 --> 00:08:40.930
which is the length of the grades list

216
00:08:40.930 --> 00:08:42.860
which gives us five.

217
00:08:42.860 --> 00:08:44.750
What we wanna do is use a For loop

218
00:08:44.750 --> 00:08:46.700
to modify this total variable

219
00:08:46.700 --> 00:08:50.260
and add to it each of these grades.

220
00:08:50.260 --> 00:08:51.150
How we do that is we say,

221
00:08:51.150 --> 00:08:55.640
for grade for example in grades, and that creates

222
00:08:55.640 --> 00:08:57.820
a new variable that we're calling grade

223
00:08:57.820 --> 00:08:59.210
although you can call it whatever you want,

224
00:08:59.210 --> 00:09:02.890
and is going to help us go through each of the values

225
00:09:02.890 --> 00:09:04.600
in the grades list.

226
00:09:04.600 --> 00:09:05.433
Then we will say,

227
00:09:05.433 --> 00:09:08.060
total plus equal grade.

228
00:09:08.060 --> 00:09:09.950
And this by the way, I think we've not see it before

229
00:09:09.950 --> 00:09:13.186
but it's the same as doing total equal total plus grade.

230
00:09:13.186 --> 00:09:17.240
So, it adds up the existing total plus the new grade,

231
00:09:17.240 --> 00:09:18.640
and sets it back to total.

232
00:09:18.640 --> 00:09:22.406
You can do that or you can do plus equal is the same thing.

233
00:09:22.406 --> 00:09:25.420
And then, after the For loop,

234
00:09:25.420 --> 00:09:27.620
once we've added them all together,

235
00:09:27.620 --> 00:09:31.610
we can print out the total divided by the amount.

236
00:09:31.610 --> 00:09:33.560
And that's gonna give us the average.

237
00:09:33.560 --> 00:09:35.420
So if we run this piece of code here,

238
00:09:35.420 --> 00:09:37.873
you'll see that the average is 80.0.

239
00:09:40.500 --> 00:09:41.401
Now, say at this point that you don't actually

240
00:09:41.401 --> 00:09:42.967
need a For loop

241
00:09:42.967 --> 00:09:44.580
for this specific piece of code.

242
00:09:44.580 --> 00:09:46.940
And this'll help me introduce another function in Python

243
00:09:46.940 --> 00:09:48.460
which is the Sum function.

244
00:09:48.460 --> 00:09:51.690
Instead of doing all of this,

245
00:09:51.690 --> 00:09:55.860
you can actually say that the total is the sum of grades.

246
00:09:55.860 --> 00:09:59.600
Just as we have Len to calculate the length of a list,

247
00:09:59.600 --> 00:10:01.700
Sum calculates the addition of all

248
00:10:01.700 --> 00:10:03.400
the elements inside a list,

249
00:10:03.400 --> 00:10:05.362
or a tuple, or a set, and so on.

250
00:10:05.362 --> 00:10:07.637
So, here we have the total and the amount

251
00:10:07.637 --> 00:10:09.330
and that's exactly the same thing

252
00:10:09.330 --> 00:10:10.836
without the need for a For loop.

253
00:10:10.836 --> 00:10:14.470
So, you just kind of have to know that this exists

254
00:10:14.470 --> 00:10:15.360
if you wanna use it.

255
00:10:15.360 --> 00:10:17.510
But, my advice would be to pretty

256
00:10:17.510 --> 00:10:20.520
much search for everything in Google or your search

257
00:10:20.520 --> 00:10:21.488
engine of choice.

258
00:10:21.488 --> 00:10:24.110
I would search when I was starting,

259
00:10:24.110 --> 00:10:28.130
for how to add a list of numbers in Python.

260
00:10:28.130 --> 00:10:30.930
And that will give you many answers.

261
00:10:30.930 --> 00:10:33.696
And one of them will teach you about this Sum function.

262
00:10:33.696 --> 00:10:36.230
When you're doing anything else in Python,

263
00:10:36.230 --> 00:10:39.150
usually I would also search for everything.

264
00:10:39.150 --> 00:10:41.150
And that way, you're going to be constantly

265
00:10:41.150 --> 00:10:44.010
absorbing new information and learning

266
00:10:44.010 --> 00:10:45.920
different ways of doing things.

267
00:10:45.920 --> 00:10:48.330
It's very often that programmers will try

268
00:10:48.330 --> 00:10:51.040
a very complicated solution at first

269
00:10:51.040 --> 00:10:53.040
because that's what we can build

270
00:10:53.040 --> 00:10:55.360
based on the building blocks that we already know.

271
00:10:55.360 --> 00:10:57.730
But, there's usually better solutions out there.

272
00:10:57.730 --> 00:10:59.410
And by searching for them,

273
00:10:59.410 --> 00:11:03.050
you're going to be able to absorb that knowledge as I said.

274
00:11:03.050 --> 00:11:04.160
That's it for this video though.

275
00:11:04.160 --> 00:11:05.300
Thank you for joining me.

276
00:11:05.300 --> 00:11:06.950
And I'll see you in the next one.

