WEBVTT

0
00:00.110 --> 00:04.400
The first topic I want to talk about today is the concept of Loops.

1
00:04.400 --> 00:07.670
Things that have to happen over and over and over again.

2
00:08.000 --> 00:13.550
The first type of loop I want to introduce you to is something called the for loop, and it can be used

3
00:13.550 --> 00:20.330
really easily in combination with something we learned in yesterday's lessons, which is Lists.

4
00:20.540 --> 00:26.810
By using a for loop like this, we can go through each item in a list and perform some action with each

5
00:26.840 --> 00:28.220
individual item.

6
00:28.250 --> 00:30.770
So let's try it out in practice.

7
00:30.800 --> 00:39.380
First, let's say we had a list called fruits and it contained three items apple, peach, and pear.

8
00:39.830 --> 00:46.040
If we wanted to access each item in this list individually and print it out one by one, then we would

9
00:46.040 --> 00:47.510
use a for loop.

10
00:47.780 --> 00:54.650
So we start out with the keyword "for" and then we give a name to a single item,

11
00:54.950 --> 00:58.220
so in this case we might call it, fruit.

12
00:58.370 --> 01:01.010
And then we use the "in" keyword.

13
01:01.010 --> 01:07.010
And finally, the list that we want to loop through, which is our fruits here.

14
01:07.070 --> 01:14.810
Now we cap it off with a colon, and we go on to the next line and notice how it's indented there.

15
01:15.320 --> 01:23.000
Now I'm going to go ahead and print the value of each of the fruit variables.

16
01:23.510 --> 01:28.790
And if I run this code you'll see that it loops through my list of fruits,

17
01:28.790 --> 01:35.450
and for each of the fruits inside the list, it prints it out into the console.

18
01:35.480 --> 01:35.960
Apple,

19
01:35.960 --> 01:36.290
Peach,

20
01:36.290 --> 01:38.330
Pear in that order.

21
01:38.360 --> 01:44.930
Now, the important thing to realize here is that basically, you can imagine that behind the scenes,

22
01:44.930 --> 01:52.850
what this code is doing is it's taking this list of fruits and it's assigning a variable name fruit

23
01:52.850 --> 01:53.960
to each of them.

24
01:53.960 --> 01:57.890
So the first time this runs fruit is equal to Apple.

25
01:57.920 --> 02:00.980
The second time this runs fruit is equal to Peach.

26
02:01.010 --> 02:07.560
And we can see this even more clearly if you run it through the Thonny IDE which I told you about

27
02:07.560 --> 02:09.450
at the beginning of the course.

28
02:09.480 --> 02:15.450
Now, if I go ahead and click on the debug icon and I step into each of the steps, you can see,

29
02:15.480 --> 02:21.060
first, it establishes that we have a list of three strings Apple, Peach, Pear.

30
02:21.060 --> 02:23.670
And then we go into the for loop.

31
02:23.700 --> 02:29.010
Now notice on the right here I've got all the variables being accounted for.

32
02:29.010 --> 02:34.950
And the first one it's noticed is the variable fruits which holds a list of strings.

33
02:34.980 --> 02:42.570
Now as I continue into the for loop, notice how it's going to look through this list of fruits.

34
02:42.570 --> 02:50.910
And it's going to assign the variable name fruit to each of the items starting from the first one, Apple.

35
02:50.940 --> 02:57.900
So now by the time it's reached line three, we've already got this variable called fruit that's been

36
02:57.900 --> 03:00.060
assigned to the value of Apple.

37
03:00.090 --> 03:07.200
And so at this point printing out the value of this fruit, it's obviously going to print out Apple

38
03:07.230 --> 03:09.390
as you'll see in the next step.

39
03:10.530 --> 03:11.490
There we go.

40
03:11.520 --> 03:17.490
Now, once I'm done here, then it's going to loop back to the start of the for loop,

41
03:17.490 --> 03:24.300
and now this variable fruit is going to be assigned to the next value inside the list of fruits.

42
03:24.330 --> 03:29.040
So now notice how the variable fruit is attached to the value Peach.

43
03:29.070 --> 03:36.540
And then it continues this and so on and so forth until it prints out each one of the fruits.

44
03:37.800 --> 03:41.970
And this really emphasizes the most important aspect of loops.

45
03:41.970 --> 03:46.980
The loop allows us to execute the same line of code multiple times.

46
03:46.980 --> 03:50.520
In this case we're executing the print statement three times.

47
03:50.520 --> 03:54.570
But our for loop isn't limited to just executing a single statement.

48
03:54.600 --> 03:57.840
We don't just have to print out the name of an item in the list.

49
03:57.870 --> 04:03.780
We can execute a whole block of statements multiple times, and we can do many things inside this for

50
04:03.780 --> 04:04.200
loop,

51
04:04.200 --> 04:06.840
and by inside I mean indented.

52
04:07.530 --> 04:13.440
So let's say that in addition to printing out the fruit, I'm going to write another print statement,

53
04:13.440 --> 04:20.970
and I'm going to not only print out the name of the fruit, but I'm also going to say fruit + space

54
04:20.970 --> 04:22.350
plus pie.

55
04:22.380 --> 04:26.040
So when I run this code, what do you think will happen?

56
04:28.290 --> 04:33.720
Well, it prints out Apple, and then it prints out Apple Plus pie.

57
04:33.720 --> 04:38.640
And then it goes back to the start and it assigns the variable fruit to the next item.

58
04:38.670 --> 04:39.210
Peach.

59
04:39.240 --> 04:39.960
Peach pie.

60
04:39.990 --> 04:40.410
Pear.

61
04:40.440 --> 04:41.190
Pear pie.

62
04:41.610 --> 04:49.530
So this is how we can implement a simple for loop that loops through a list and assigns a variable name

63
04:49.530 --> 04:55.410
to each of the items in the list in order, and then inside the for loop after the colon,

64
04:55.410 --> 05:02.760
after some indentation, we can do something with that temporary variable for each of the items.

65
05:02.970 --> 05:10.950
Now, I've been talking a lot about the concept of being inside a for loop versus being outside a for

66
05:10.950 --> 05:17.880
loop, and this is really, really important. Whenever you see a colon, say in our if statements that

67
05:17.880 --> 05:25.140
we saw previously, or the for loop that we're using here, the indentation is really, really important

68
05:25.140 --> 05:30.210
because if it's indented then it means that it's inside the for loop

69
05:30.210 --> 05:36.390
and these instructions will get carried out for as many times as the for loop will need to repeat.

70
05:36.420 --> 05:43.050
Now, if I decided that I wanted to say print my fruits,

71
05:43.050 --> 05:50.610
so my list of fruits up here, and I put it inside my for loop, so indented after the for loop.

72
05:50.640 --> 05:56.760
Then, as you can imagine, it's going to print that for as many times as the loop runs, which is going

73
05:56.760 --> 06:00.390
to be three because there's three items in our list.

74
06:00.420 --> 06:08.520
Now, if I had indented that back to the beginning, so now it's no longer inside the for loop, then

75
06:08.520 --> 06:14.850
it's only going to print once and it's going to print it after the for loop is done.

76
06:14.850 --> 06:21.520
So notice how it's doing the whole Apple Apple pie, etc. and then once the loop is finished, then

77
06:21.520 --> 06:25.810
it jumps to the next line and carries out this instruction.

78
06:25.810 --> 06:28.270
So the indentation is really, really important,

79
06:28.270 --> 06:30.400
and you have to be really careful with this.

80
06:31.300 --> 06:33.460
Have a play around with this code.

81
06:33.460 --> 06:39.190
And if you're still struggling to see how it works, then move the code over to Thonny and step through

82
06:39.190 --> 06:42.130
each step, one at a time and see how it works.

83
06:42.130 --> 06:47.680
But I hope you're starting to see how loops are really handy at executing an instruction over and over

84
06:47.680 --> 06:51.520
and over again, getting the computer to save us time and energy.

85
06:51.880 --> 06:58.210
A really good example of when loops would come in really handy is, you know, how Bart Simpson gets

86
06:58.240 --> 07:02.590
punished and has to write out a sentence over and over again on the blackboard?

87
07:02.590 --> 07:08.740
Well, if only Bart was a programmer, then he would be able to use loops to do this and he would be

88
07:08.740 --> 07:10.150
able to chill in the corner.

89
07:10.510 --> 07:16.360
Now, once you're happy with this type of for loop, then head over to the next lesson where I've got

90
07:16.360 --> 07:19.630
a coding exercise that's going to put your knowledge to the test.

91
07:19.660 --> 07:21.220
All right, so I'll see you there.