1
00:00:00,009 --> 00:00:04,539
Let's now go back to our trusty replit. And I'm gonna give you two examples.

2
00:00:04,550 --> 00:00:06,829
One for the for loop

3
00:00:07,199 --> 00:00:09,789
and another for the while loop.

4
00:00:10,050 --> 00:00:10,590
So

5
00:00:11,149 --> 00:00:15,920
in order to create our for loop, let's first of all, create the list in

6
00:00:16,370 --> 00:00:22,430
this case, right now, I'm going to provide the name of the list as names. OK?

7
00:00:22,889 --> 00:00:23,889
Equals

8
00:00:24,409 --> 00:00:27,950
and then I'm gonna have my brackets in here and let's add

9
00:00:28,559 --> 00:00:31,340
some names. So I've got uh Alex,

10
00:00:32,319 --> 00:00:35,259
I've got uh Nancy

11
00:00:36,860 --> 00:00:40,259
and I've got a James

12
00:00:41,130 --> 00:00:44,430
and let's say last, but not least

13
00:00:45,270 --> 00:00:46,060
I

14
00:00:46,189 --> 00:00:47,220
have got

15
00:00:48,069 --> 00:00:50,009
uh Violetta.

16
00:00:50,830 --> 00:00:52,369
Honestly, I just came up with that name,

17
00:00:53,139 --> 00:00:54,500
Violetta. OK.

18
00:00:54,750 --> 00:00:59,189
So we have our list right here of four different names, right? Alex, Nancy James.

19
00:00:59,200 --> 00:01:00,610
And then Violetta,

20
00:01:01,090 --> 00:01:05,589
one thing I can do right now is we can create a variable

21
00:01:05,849 --> 00:01:10,010
that will represent each item in the list.

22
00:01:10,019 --> 00:01:14,129
Kind of like how we've been assigning values to variables like X equals four,

23
00:01:14,139 --> 00:01:15,650
Y equals seven.

24
00:01:15,989 --> 00:01:19,129
I can now come in here and then simply say

25
00:01:19,360 --> 00:01:20,589
four,

26
00:01:21,319 --> 00:01:24,970
OK? And now I will say four X

27
00:01:26,940 --> 00:01:27,989
in

28
00:01:28,160 --> 00:01:29,389
names.

29
00:01:30,430 --> 00:01:31,029
OK?

30
00:01:31,559 --> 00:01:34,519
What this does right here and let me just add the colon right? There.

31
00:01:34,779 --> 00:01:38,620
So what we've done with this statement here is that we're saying, OK,

32
00:01:38,680 --> 00:01:42,849
we are going to use X to represent each

33
00:01:43,180 --> 00:01:50,430
name in this list. So X will be equal to Alex. That will be the first loop, that

34
00:01:50,599 --> 00:01:50,970
one,

35
00:01:51,250 --> 00:01:54,559
the second loop X will not be equal to Nancy. It will

36
00:01:54,660 --> 00:01:57,430
run, the third loop will not be X equals to James.

37
00:01:57,440 --> 00:02:00,519
And then finally, the very last loop will be X

38
00:02:00,650 --> 00:02:03,650
is equal to violet. And that's basically what this does in here.

39
00:02:03,660 --> 00:02:05,529
So we're saying for X in names

40
00:02:05,790 --> 00:02:10,949
and now we have to tell the program. OK. What should we do? Well,

41
00:02:11,940 --> 00:02:13,070
we should print out

42
00:02:14,350 --> 00:02:14,899
the name

43
00:02:15,919 --> 00:02:16,550
X.

44
00:02:18,690 --> 00:02:19,110
OK.

45
00:02:19,830 --> 00:02:21,570
I am going to run

46
00:02:22,020 --> 00:02:22,770
and there you go,

47
00:02:23,210 --> 00:02:24,449
Alex,

48
00:02:24,820 --> 00:02:27,710
Nancy, James and

49
00:02:28,169 --> 00:02:29,110
Violetta.

50
00:02:29,559 --> 00:02:32,789
But what if I wanted to spice this program up

51
00:02:32,800 --> 00:02:35,020
just a little bit and let's say something like,

52
00:02:35,029 --> 00:02:38,779
you know, good morning and then the name of that person.

53
00:02:38,789 --> 00:02:42,830
So we'll have Good morning, Alex, good morning, Nancy, good morning James.

54
00:02:42,839 --> 00:02:44,149
And of course, good morning

55
00:02:44,289 --> 00:02:45,149
Violetta.

56
00:02:45,419 --> 00:02:47,990
How are we going to do this? Well,

57
00:02:48,169 --> 00:02:50,710
inside of the print function,

58
00:02:51,210 --> 00:02:54,309
I am going to add a string that will first of all say

59
00:02:54,800 --> 00:02:56,509
good

60
00:02:56,639 --> 00:02:57,869
morning,

61
00:03:00,009 --> 00:03:00,550
okay.

62
00:03:01,330 --> 00:03:04,509
And then we're going to have the comma which by the

63
00:03:04,520 --> 00:03:07,830
way is just part of the actual print out itself.

64
00:03:07,839 --> 00:03:10,779
It's not part of the actual program.

65
00:03:10,789 --> 00:03:11,910
If you know what I mean, it's not,

66
00:03:11,919 --> 00:03:14,050
it's part of the string basically is what I'm trying to say, we can

67
00:03:14,160 --> 00:03:16,289
do so without the comma as well.

68
00:03:16,300 --> 00:03:19,470
In fact, you know what, let me remove the comma just so I don't confuse you.

69
00:03:19,559 --> 00:03:21,419
So I'm gonna say good morning.

70
00:03:21,660 --> 00:03:22,270
Ok?

71
00:03:22,639 --> 00:03:23,630
And then

72
00:03:24,110 --> 00:03:26,789
I am going to add a space

73
00:03:27,210 --> 00:03:30,449
because we want to be space between the actual string.

74
00:03:30,460 --> 00:03:32,130
Good morning and then the name of the person.

75
00:03:32,139 --> 00:03:34,110
That's why I've added the space right there.

76
00:03:34,449 --> 00:03:37,110
Ok? And then right here,

77
00:03:37,520 --> 00:03:39,830
I am going to add the plus sign

78
00:03:40,320 --> 00:03:41,500
and then X

79
00:03:42,169 --> 00:03:43,669
so basically attaching

80
00:03:43,839 --> 00:03:45,110
the string. Good morning

81
00:03:45,360 --> 00:03:47,509
to the name of the person.

82
00:03:47,800 --> 00:03:49,380
Let's run the program

83
00:03:49,869 --> 00:03:50,729
and there you go.

84
00:03:51,110 --> 00:03:54,580
Good morning, Alex. Good morning and good morning, James. Good morning, Violetta.

85
00:03:55,490 --> 00:03:58,330
Now, if I remove this space because I want you to understand this very well.

86
00:03:58,339 --> 00:04:01,710
If I remove this space right now and I run the program again,

87
00:04:01,960 --> 00:04:05,619
you can see right now that it's all jumbled up,

88
00:04:05,710 --> 00:04:07,759
there's no space between morning and then the name of the person.

89
00:04:07,770 --> 00:04:08,910
That's why once again,

90
00:04:09,139 --> 00:04:13,509
I added the space right here between the G and the closing quotation marks.

91
00:04:13,880 --> 00:04:14,520
So

92
00:04:14,759 --> 00:04:15,869
one more time

93
00:04:16,230 --> 00:04:17,048
and there you go.

94
00:04:17,290 --> 00:04:21,380
This is an example of a very simple for loop.

95
00:04:21,390 --> 00:04:26,750
First of all, we created a list of names, we assigned this entire

96
00:04:27,440 --> 00:04:29,950
list right here to the variable names.

97
00:04:30,170 --> 00:04:34,600
And then we now need to create another variable that will represent each item,

98
00:04:34,609 --> 00:04:36,220
each name in this list.

99
00:04:36,230 --> 00:04:39,410
So we said for X in

100
00:04:39,579 --> 00:04:41,350
the list called names.

101
00:04:41,570 --> 00:04:43,500
And now the function print good morning

102
00:04:43,670 --> 00:04:48,829
plus the name of the person in that list.

103
00:04:48,970 --> 00:04:52,529
This is an example of your for loop.

104
00:04:53,040 --> 00:04:53,940
Now,

105
00:04:54,130 --> 00:04:55,839
what I'm gonna do is

106
00:04:55,959 --> 00:05:01,299
we're going to create a while loop now to do this.

107
00:05:01,920 --> 00:05:04,700
Let's come down here and yes, we can run

108
00:05:04,920 --> 00:05:08,820
multiple programs and functions within the exact same file. All right.

109
00:05:09,489 --> 00:05:09,910
So

110
00:05:10,640 --> 00:05:15,799
what I'm gonna do right now is I'm going to create something called a counter.

111
00:05:16,109 --> 00:05:17,299
OK? So counter

112
00:05:17,559 --> 00:05:19,019
equals zero.

113
00:05:19,480 --> 00:05:20,950
Now, what exactly am

114
00:05:21,070 --> 00:05:22,839
I doing here? What is this counter?

115
00:05:23,190 --> 00:05:26,519
Remember that with while loops,

116
00:05:26,959 --> 00:05:30,579
they will continue to run as long as a condition

117
00:05:30,730 --> 00:05:33,260
continues to be satisfied.

118
00:05:33,500 --> 00:05:37,179
That's the difference between a for loop and a while loop with the full loop.

119
00:05:37,320 --> 00:05:39,720
We have a list of items, right? So

120
00:05:39,850 --> 00:05:42,609
the loop will simply run through all the items in that list.

121
00:05:42,779 --> 00:05:46,950
But a wide loop, there has to be some sort of a condition

122
00:05:47,100 --> 00:05:48,600
that will be presented.

123
00:05:48,790 --> 00:05:50,929
And then we're gonna tell Python that, hey,

124
00:05:50,940 --> 00:05:56,279
as long as this particular condition continues to be satisfied, execute the loop.

125
00:05:56,579 --> 00:05:57,119
So

126
00:05:57,390 --> 00:06:02,239
the condition here right now is that while a counter

127
00:06:02,529 --> 00:06:04,720
is less than, let's say three,

128
00:06:06,130 --> 00:06:07,420
do you now see

129
00:06:07,679 --> 00:06:11,709
we're saying, hey, as long as our counter,

130
00:06:11,720 --> 00:06:15,040
as long as the number in here is less than three,

131
00:06:15,420 --> 00:06:20,630
continue to do something. And you can see Python kind of already given us a hint

132
00:06:20,859 --> 00:06:22,790
of where to go next. Well,

133
00:06:23,279 --> 00:06:24,890
I'm gonna say print,

134
00:06:25,410 --> 00:06:26,670
print and then

135
00:06:27,589 --> 00:06:29,559
I'm going to add a text right there, the string.

136
00:06:30,190 --> 00:06:31,570
Uh hello.

137
00:06:32,239 --> 00:06:32,739
Ok.

138
00:06:34,130 --> 00:06:34,820
And there you go.

139
00:06:35,519 --> 00:06:39,619
Now what we're saying here is, start from zero.

140
00:06:40,119 --> 00:06:45,600
Ok, start from zero and then check to see if zero is less than three.

141
00:06:45,609 --> 00:06:49,440
If it's less than three, then print. Hello.

142
00:06:49,929 --> 00:06:50,980
However,

143
00:06:51,329 --> 00:06:53,820
we're not actually done.

144
00:06:54,049 --> 00:06:54,980
Why?

145
00:06:55,369 --> 00:07:00,140
Well, because we haven't specifically told Python

146
00:07:00,519 --> 00:07:01,190
to

147
00:07:01,359 --> 00:07:02,070
add

148
00:07:02,440 --> 00:07:04,829
a new number to the counter. So

149
00:07:05,010 --> 00:07:07,470
instead of zero, it will not become one

150
00:07:07,630 --> 00:07:11,070
check to see if one is less than three and then print out. Hello.

151
00:07:11,380 --> 00:07:16,130
We haven't told Python that yet because a loop can go either way,

152
00:07:16,350 --> 00:07:18,070
we can either continue to add

153
00:07:18,600 --> 00:07:26,339
or 123 to 0 or we could go the opposite direction and then instead of zero going to one,

154
00:07:26,350 --> 00:07:30,910
it could be zero, going to minus one minus two minus three minus four.

155
00:07:31,119 --> 00:07:32,440
And in that kind of scenario,

156
00:07:33,279 --> 00:07:35,320
those numbers will always be less than

157
00:07:35,329 --> 00:07:39,299
three and the condition will never be satisfied

158
00:07:39,630 --> 00:07:42,679
or that it will always be satisfied rather.

159
00:07:42,880 --> 00:07:44,579
And then that will crush the program.

160
00:07:44,760 --> 00:07:48,059
So we need to tell Python explicitly that, hey,

161
00:07:48,359 --> 00:07:51,940
once you've run the very first loop where zero is less than three,

162
00:07:51,950 --> 00:07:54,230
add one to the counter.

163
00:07:54,390 --> 00:07:55,029
So

164
00:07:55,190 --> 00:07:57,579
I'm gonna come in right now and I'm gonna say counter

165
00:07:58,829 --> 00:07:59,589
and then

166
00:07:59,920 --> 00:08:02,720
plus equals one.

167
00:08:03,980 --> 00:08:06,920
This is our way of saying OK, Python

168
00:08:07,040 --> 00:08:10,369
the very first, the very beginning of the loop will be zero,

169
00:08:10,480 --> 00:08:14,399
check to see if zero is less than three, if it's less than three print. Hello.

170
00:08:14,519 --> 00:08:17,660
And then now add one to the counter. So it will not be one

171
00:08:18,179 --> 00:08:21,940
at less than three. Python says. Yes, one is indeed less than three.

172
00:08:21,950 --> 00:08:25,779
It's gonna print out. Hello. It's gonna add one again. So one will not become two.

173
00:08:25,790 --> 00:08:27,850
Python is gonna check. Ok. It's two, less than three.

174
00:08:27,859 --> 00:08:31,109
Yes, it's less than three print. Hello. And then it's gonna add one again.

175
00:08:31,119 --> 00:08:32,400
So to now becomes three.

176
00:08:32,780 --> 00:08:34,780
And then finally, Python will say that OK,

177
00:08:34,789 --> 00:08:38,849
three isn't less than three because three equals three and then it will end

178
00:08:39,000 --> 00:08:39,558
the loop.

179
00:08:40,760 --> 00:08:41,450
OK?

180
00:08:41,650 --> 00:08:45,159
Let's go ahead right now and run

181
00:08:45,590 --> 00:08:46,440
and there you go.

182
00:08:46,880 --> 00:08:48,729
Hello. Hello? Hello.

183
00:08:48,849 --> 00:08:51,099
You can see right there that it stopped at three

184
00:08:51,109 --> 00:08:53,200
loops because the very first loop will be for zero,

185
00:08:53,440 --> 00:08:57,080
second loop will be for one third loop will be for two

186
00:08:57,900 --> 00:08:59,130
if I came here.

187
00:08:59,309 --> 00:09:01,770
And I said, OK, less than seven

188
00:09:02,340 --> 00:09:03,400
run.

189
00:09:03,750 --> 00:09:11,659
Now, you can see we have again, 40123456 hellos.

190
00:09:12,130 --> 00:09:12,679
So

191
00:09:13,140 --> 00:09:14,419
that's the difference between

192
00:09:14,530 --> 00:09:16,599
the for loop and

193
00:09:16,710 --> 00:09:18,559
the while loop.

194
00:09:18,570 --> 00:09:23,109
You can see right now with the full loop, we have our list or our, our way

195
00:09:23,539 --> 00:09:25,960
and then the loop will go through each particular

196
00:09:25,969 --> 00:09:28,559
item in that way and it will do something.

197
00:09:28,880 --> 00:09:33,640
But for the while loop, we need to first of all specify what's the beginning point

198
00:09:34,109 --> 00:09:37,349
and then we will not provide the condition as long as this

199
00:09:37,359 --> 00:09:40,080
equals this or this is less than this or this is this,

200
00:09:40,830 --> 00:09:42,080
do something

201
00:09:42,369 --> 00:09:47,169
and then either add one or remove one or just do something

202
00:09:47,179 --> 00:09:50,390
to make sure that the loop will now actually begin to run.

203
00:09:50,679 --> 00:09:53,549
That's the difference between the for loop and the while loop.

204
00:09:53,559 --> 00:09:55,320
Of course, we're going to be working a lot more

205
00:09:55,440 --> 00:09:58,690
with four and wide loops in this course. But thank you for watching.

206
00:09:58,700 --> 00:10:00,510
I will see you in the next class.

