WEBVTT

1
00:00:00.370 --> 00:00:02.290
<v ->Hi guys and welcome back,</v>

2
00:00:02.290 --> 00:00:05.210
in this video we're going to learn about for loops.

3
00:00:05.210 --> 00:00:07.860
A for loop is used to repeat something

4
00:00:07.860 --> 00:00:10.170
a defined number of times.

5
00:00:10.170 --> 00:00:11.003
That is,

6
00:00:11.003 --> 00:00:13.180
when you know how many times you wanna repeat it.

7
00:00:14.650 --> 00:00:17.930
But a for loop in Python has a slightly different way

8
00:00:17.930 --> 00:00:20.140
about it that in other programming languages.

9
00:00:20.140 --> 00:00:22.440
So if you're coming from a different programming language,

10
00:00:22.440 --> 00:00:25.060
this is more like a foreach loop.

11
00:00:25.060 --> 00:00:26.640
And if you don't know what I'm talking about,

12
00:00:26.640 --> 00:00:29.000
don't worry let's learn about it.

13
00:00:29.000 --> 00:00:32.050
First of all I will start with a friends list,

14
00:00:32.050 --> 00:00:33.750
which has the names of my friends.

15
00:00:36.480 --> 00:00:38.710
And then we would create our for loop.

16
00:00:38.710 --> 00:00:41.640
What we want to do is we want to print the names

17
00:00:41.640 --> 00:00:45.650
of each friend separately into its own lines.

18
00:00:45.650 --> 00:00:48.596
So what we'll do is we'll do for,

19
00:00:48.596 --> 00:00:50.380
that is the key word that signals

20
00:00:50.380 --> 00:00:52.292
that we want to start a for loop.

21
00:00:52.292 --> 00:00:56.073
And then we will do friend in friends.

22
00:00:56.980 --> 00:00:58.240
Print friend.

23
00:00:58.240 --> 00:01:01.660
So a few familiar things going on in here.

24
00:01:01.660 --> 00:01:04.310
We've got the colon,

25
00:01:04.310 --> 00:01:08.210
we've got the indented body of the loop,

26
00:01:08.210 --> 00:01:12.690
but we do have something a bit weird going on in this line.

27
00:01:12.690 --> 00:01:14.740
Friend in friends,

28
00:01:14.740 --> 00:01:17.750
we learned that this was Boolean comparison,

29
00:01:17.750 --> 00:01:20.400
earlier on right?

30
00:01:20.400 --> 00:01:22.460
But what is friend?

31
00:01:22.460 --> 00:01:24.423
We've not defined this variable anyway.

32
00:01:25.470 --> 00:01:26.500
And here's the thing,

33
00:01:26.500 --> 00:01:29.270
this is not a Boolean comparison.

34
00:01:29.270 --> 00:01:33.200
What's going on here is you're telling Python to give you a

35
00:01:33.200 --> 00:01:38.200
new variable called friend once for every friend.

36
00:01:38.320 --> 00:01:39.520
So we're saying,

37
00:01:39.520 --> 00:01:44.140
friend will be the first element in friends.

38
00:01:44.140 --> 00:01:45.990
Then when the loop repeats again,

39
00:01:45.990 --> 00:01:49.490
friend will now be the second element in friends.

40
00:01:49.490 --> 00:01:53.460
Then friend will be the third element in friends and so on.

41
00:01:53.460 --> 00:01:57.030
This is why it uses also the in key word,

42
00:01:57.030 --> 00:01:58.470
but it means something different

43
00:01:58.470 --> 00:02:00.610
than if you use it anywhere else.

44
00:02:00.610 --> 00:02:05.610
In the for loop you've got for, new variable, in, list.

45
00:02:08.010 --> 00:02:11.240
And then you get one iteration of the loop,

46
00:02:11.240 --> 00:02:16.120
or one repeat of the loop for every value in that list.

47
00:02:16.120 --> 00:02:18.430
Note that instead of lists you can use tuples,

48
00:02:18.430 --> 00:02:20.570
you can use sets or dictionaries here,

49
00:02:20.570 --> 00:02:21.620
and that's totally fine.

50
00:02:21.620 --> 00:02:24.700
Those are all known as iterables,

51
00:02:24.700 --> 00:02:26.810
because you can iterate over them,

52
00:02:26.810 --> 00:02:29.223
you can use them for exactly this.

53
00:02:30.120 --> 00:02:32.880
So this is well and good when you've got a few values,

54
00:02:32.880 --> 00:02:35.840
and you wanna do something to each of them.

55
00:02:35.840 --> 00:02:39.460
But if you wanna repeat something say 10 times,

56
00:02:39.460 --> 00:02:43.590
you can do something like elements equal zero to 10.

57
00:02:43.590 --> 00:02:45.870
And then you can do for index,

58
00:02:45.870 --> 00:02:46.930
which is a new variable,

59
00:02:46.930 --> 00:02:48.970
you can call this whatever you want by the way.

60
00:02:48.970 --> 00:02:52.820
In elements and we will print index.

61
00:02:52.820 --> 00:02:55.100
What this is doing is now we're going to

62
00:02:55.100 --> 00:02:59.260
repeat this loop 10 times because there are 10 numbers here.

63
00:02:59.260 --> 00:03:01.440
So by running this we will see

64
00:03:01.440 --> 00:03:04.563
that we get the numbers zero to nine printed out.

65
00:03:06.050 --> 00:03:09.830
So we are getting the numbers zero to nine,

66
00:03:09.830 --> 00:03:12.530
but we don't have to print them out.

67
00:03:12.530 --> 00:03:15.993
We can print something else like hello world.

68
00:03:16.860 --> 00:03:17.693
And we know,

69
00:03:17.693 --> 00:03:20.240
that because there are 10 elements here,

70
00:03:20.240 --> 00:03:22.930
this loop will run 10 times and therefore,

71
00:03:22.930 --> 00:03:25.190
we will get 10 hello worlds.

72
00:03:25.190 --> 00:03:28.350
So what I'm trying to say here is that just because

73
00:03:28.350 --> 00:03:32.240
you are getting a variable for each element,

74
00:03:32.240 --> 00:03:34.730
you don't have to use it for anything.

75
00:03:34.730 --> 00:03:37.970
And usually when you do something like this where you

76
00:03:37.970 --> 00:03:40.910
create a variable but you don't use it inside a loop,

77
00:03:40.910 --> 00:03:44.940
we often in Python give it the name of underscore.

78
00:03:44.940 --> 00:03:46.470
So we just call it underscore,

79
00:03:46.470 --> 00:03:49.040
and that usually signals to other Python programmers

80
00:03:49.040 --> 00:03:52.370
that you don't intend on using this variable for anything.

81
00:03:52.370 --> 00:03:53.800
You could if you wanted to,

82
00:03:53.800 --> 00:03:55.920
because that's a valid variable name,

83
00:03:55.920 --> 00:03:58.810
but this signals that you don't want to.

84
00:03:58.810 --> 00:04:00.600
Of course you can give it any other name you want,

85
00:04:00.600 --> 00:04:02.260
and that's fine.

86
00:04:02.260 --> 00:04:05.800
I will go back to for index in elements just because

87
00:04:05.800 --> 00:04:08.710
I think it's a little bit more readable for you guys.

88
00:04:08.710 --> 00:04:12.360
But instead of creating our own list of 10 elements,

89
00:04:12.360 --> 00:04:14.993
you can actually just do range 10.

90
00:04:16.250 --> 00:04:18.040
And this is pretty much the same thing

91
00:04:18.040 --> 00:04:20.493
in that it will also run 10 times.

92
00:04:22.090 --> 00:04:23.830
What range is doing behind the scenes,

93
00:04:23.830 --> 00:04:28.390
it is creating something similar to that 10 element list.

94
00:04:28.390 --> 00:04:31.370
But it comes with a few more powerful things,

95
00:04:31.370 --> 00:04:35.040
such as you can do five comma 10 inside range,

96
00:04:35.040 --> 00:04:39.660
and this will give you the numbers five to 10.

97
00:04:39.660 --> 00:04:41.370
So five six seven eight and nine.

98
00:04:41.370 --> 00:04:46.040
Or you can do something like two twenty and three.

99
00:04:46.040 --> 00:04:46.873
And what this does,

100
00:04:46.873 --> 00:04:49.410
is it gives you the numbers two to 19,

101
00:04:49.410 --> 00:04:51.860
but only every three numbers,

102
00:04:51.860 --> 00:04:55.113
so two five eight 11 14 17.

103
00:04:56.120 --> 00:04:58.510
Now these are just some of the things you can do with range

104
00:04:58.510 --> 00:05:02.050
and I don't expect you to get much use out of them just now.

105
00:05:02.050 --> 00:05:04.421
But it is one of the reasons to use range over

106
00:05:04.421 --> 00:05:07.140
creating your own list of 10 elements.

107
00:05:07.140 --> 00:05:07.973
It's a lot shorter,

108
00:05:07.973 --> 00:05:10.253
it also more convenient and more powerful.

109
00:05:11.620 --> 00:05:13.920
Imagine you've got a list of dictionaries,

110
00:05:13.920 --> 00:05:16.030
where each dictionary contains

111
00:05:16.030 --> 00:05:18.453
the name and the grade of a student.

112
00:05:19.750 --> 00:05:23.280
You could do for student in students,

113
00:05:23.280 --> 00:05:24.940
and remember what we've got here is

114
00:05:24.940 --> 00:05:27.450
the for key word the student,

115
00:05:27.450 --> 00:05:31.530
the new variable we are creating every iteration,

116
00:05:31.530 --> 00:05:36.530
and in students which is our list or our iterable.

117
00:05:36.590 --> 00:05:38.880
We can then get the name of each

118
00:05:38.880 --> 00:05:41.815
student by doing student name.

119
00:05:41.815 --> 00:05:46.230
Now remember student is each of the values in the list,

120
00:05:46.230 --> 00:05:48.410
so that's the first time the loop runs,

121
00:05:48.410 --> 00:05:51.270
student will be this dictionary here.

122
00:05:51.270 --> 00:05:52.460
The second time it runs,

123
00:05:52.460 --> 00:05:54.650
student will be this dictionary here,

124
00:05:54.650 --> 00:05:56.200
and then it will be this one,

125
00:05:56.200 --> 00:05:57.810
and then this one.

126
00:05:57.810 --> 00:05:59.760
So by doing student name,

127
00:05:59.760 --> 00:06:03.690
what you're going to do is you're going to get the name key

128
00:06:03.690 --> 00:06:06.290
of this dictionary the first time the loop runs,

129
00:06:06.290 --> 00:06:09.170
and you're going to put that inside a variable name.

130
00:06:09.170 --> 00:06:10.310
The second time the loop runs,

131
00:06:10.310 --> 00:06:13.980
you're gonna get the name property of this dictionary,

132
00:06:13.980 --> 00:06:16.260
and you're gonna put that inside this variable.

133
00:06:16.260 --> 00:06:17.400
And then you're gonna repeat that for

134
00:06:17.400 --> 00:06:19.213
this dictionary and this one too.

135
00:06:20.330 --> 00:06:22.113
Lets do the same with grade.

136
00:06:25.170 --> 00:06:27.170
And finally lets use them for something.

137
00:06:29.680 --> 00:06:31.790
Lets run this and you'll see that we can print this

138
00:06:31.790 --> 00:06:34.160
information much more nicely rather than

139
00:06:34.160 --> 00:06:35.640
just printing out a dictionary,

140
00:06:35.640 --> 00:06:37.210
by using this for loop and saying

141
00:06:37.210 --> 00:06:40.653
Rolf has a grade of 90, 78, 100, and 80.

142
00:06:42.160 --> 00:06:44.810
So when do you want to use a for loop?

143
00:06:44.810 --> 00:06:47.590
When you want to repeat something a certain number of times

144
00:06:47.590 --> 00:06:49.600
and you know how many that is,

145
00:06:49.600 --> 00:06:53.660
or when you want to use each of the values of an iterable,

146
00:06:53.660 --> 00:06:56.806
such as a list, tuple, or set, or dictionary,

147
00:06:56.806 --> 00:07:00.583
and you want to do something once for each of the values.

148
00:07:02.200 --> 00:07:03.810
I hope you've learned something in this video.

149
00:07:03.810 --> 00:07:04.990
I know that we're looking at a

150
00:07:04.990 --> 00:07:07.010
lot of different bits of Python,

151
00:07:07.010 --> 00:07:08.720
and maybe right now you don't quite

152
00:07:08.720 --> 00:07:11.490
understand why they're all important.

153
00:07:11.490 --> 00:07:13.530
But very soon everything's going to come together

154
00:07:13.530 --> 00:07:17.030
and we're going to be able to develop very useful programmes

155
00:07:17.030 --> 00:07:19.320
just with the knowledge we've got so far.

156
00:07:19.320 --> 00:07:20.820
So thanks for sticking with me,

157
00:07:20.820 --> 00:07:21.780
hope you've enjoyed it,

158
00:07:21.780 --> 00:07:23.530
and I'll see you in the next video.

