Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,280 --> 00:00:04,300
The first topic I want to talk about today is the concept of loops,
2
00:00:04,660 --> 00:00:07,450
things that have to happen over and over and over again.
3
00:00:08,020 --> 00:00:11,560
The first type of loop I want to introduce you to is something called the For
4
00:00:11,560 --> 00:00:16,560
loop. And it can be used really easily in combination with something we learned
5
00:00:16,960 --> 00:00:21,520
in yesterday's lessons, which is lists. By using a
6
00:00:21,520 --> 00:00:22,600
for loop like this
7
00:00:22,660 --> 00:00:26,860
we can go through each item in a list and perform some action with each
8
00:00:26,890 --> 00:00:27,880
individual item.
9
00:00:28,600 --> 00:00:32,290
So let's try it out in practice. In order to follow along
10
00:00:32,590 --> 00:00:36,730
go ahead and fork the day 5 start rebel.
11
00:00:37,210 --> 00:00:41,200
And once I've explained the concept, then I want you to try it out yourself.
12
00:00:41,920 --> 00:00:46,920
But first, let's say we had a list called fruits and it contained three items,
13
00:00:48,430 --> 00:00:50,320
apple, peach, and pear.
14
00:00:51,370 --> 00:00:55,660
If we wanted to access each item in this list individually and print it out one
15
00:00:55,660 --> 00:00:58,390
by one, then we would use a for loop.
16
00:00:59,290 --> 00:01:01,270
So if we start out with the keyword for,
17
00:01:01,720 --> 00:01:05,620
and then we give a name to a single item.
18
00:01:06,250 --> 00:01:09,160
So in this case, we might call it fruit.
19
00:01:09,880 --> 00:01:12,130
And then we use the in keyword
20
00:01:12,430 --> 00:01:15,640
and finally the list that we want to loop through,
21
00:01:16,090 --> 00:01:17,860
which is our fruits here.
22
00:01:18,580 --> 00:01:23,580
Now we cap it off with a colon and we go on to the next line and notice how it's
23
00:01:24,730 --> 00:01:25,690
indented there.
24
00:01:26,770 --> 00:01:31,770
Now I'm going to go ahead and print the value of each of the fruit variables.
25
00:01:35,080 --> 00:01:36,520
And if I run this code,
26
00:01:36,580 --> 00:01:41,580
you'll see that it loops through my list of fruits and for each of the fruits
27
00:01:42,370 --> 00:01:46,540
inside the list, it prints it out into the console,
28
00:01:46,780 --> 00:01:50,260
apple, peach, pear, in that order. Now,
29
00:01:50,290 --> 00:01:54,670
the important thing to realize here is that basically you can imagine that
30
00:01:54,670 --> 00:01:58,170
behind the scenes, what this code is doing is
31
00:01:58,170 --> 00:02:00,670
it's taking this list of fruits
32
00:02:01,060 --> 00:02:05,140
and it's a signing a variable name, fruit, to each of them.
33
00:02:05,260 --> 00:02:09,040
So the first time this runs fruit is equal to apple,
34
00:02:09,190 --> 00:02:12,100
the second time this runs fruit is equal to peach,
35
00:02:12,610 --> 00:02:15,160
and we can see this even more clearly
36
00:02:15,220 --> 00:02:17,650
if you run it through the Thonny IDE,
37
00:02:17,680 --> 00:02:21,250
which I told you about at the beginning of the course. Now,
38
00:02:21,280 --> 00:02:25,870
if I go ahead and click on the debug icon and I step into each of the steps,
39
00:02:26,170 --> 00:02:31,060
you can see first it establishes that we have a list of three strings,
40
00:02:31,090 --> 00:02:34,750
apple, peach, pear, and then we go into the for loop.
41
00:02:35,170 --> 00:02:40,120
Now notice on the right here I've got all the variables being accounted for.
42
00:02:40,420 --> 00:02:43,300
And the first one it's noticed is the variable fruits,
43
00:02:43,540 --> 00:02:46,870
which holds a list of strings. Now,
44
00:02:46,930 --> 00:02:49,090
as I continue into the for loop,
45
00:02:49,510 --> 00:02:53,530
notice how it's going to look through this list of fruits
46
00:02:54,160 --> 00:02:59,160
and it's going to assign the variable name fruit to each of the items starting
47
00:03:00,460 --> 00:03:01,930
from the first one apple.
48
00:03:02,410 --> 00:03:05,950
So now by the time it's reached line 3,
49
00:03:06,130 --> 00:03:08,260
we've already got this variable called fruit
50
00:03:08,620 --> 00:03:12,790
that's been assigned to the value of apple. And so at this point,
51
00:03:12,820 --> 00:03:15,640
printing out the value of this fruit
52
00:03:15,880 --> 00:03:20,200
it's obviously going to print out apple and you'll see in the next step.
53
00:03:21,880 --> 00:03:25,000
There we go. Now, once I'm done here,
54
00:03:25,120 --> 00:03:28,510
then it's going to loop back to the start of the for loop
55
00:03:28,960 --> 00:03:33,960
and now this variable fruit is going to be assigned to the next value inside the
56
00:03:34,540 --> 00:03:35,410
list of fruits.
57
00:03:35,800 --> 00:03:40,180
So now notice how the variable fruit is attached to the value peach,
58
00:03:40,570 --> 00:03:45,570
and then it continues this and so on and so forth until it prints out each one
59
00:03:46,720 --> 00:03:47,553
of the fruits.
60
00:03:49,390 --> 00:03:53,020
And this really emphasizes the most important aspect of loops.
61
00:03:53,410 --> 00:03:57,940
The loop allows us to execute the same line of code multiple times.
62
00:03:58,390 --> 00:04:01,600
In this case, we're executing the print statement three times,
63
00:04:01,960 --> 00:04:05,590
but our for loop isn't limited to just executing a single statement.
64
00:04:06,010 --> 00:04:08,950
We don't just have to print out the name of an item in the list.
65
00:04:09,310 --> 00:04:12,460
We can execute a whole block of statements multiple times,
66
00:04:12,910 --> 00:04:17,770
and we can do many things inside this for loop and by inside, I mean indented.
67
00:04:18,790 --> 00:04:21,820
So let's say that in addition to printing out the fruit,
68
00:04:22,240 --> 00:04:26,980
I'm going to write another print statement and I'm going to not only print out
69
00:04:26,980 --> 00:04:27,940
the name of the fruit,
70
00:04:28,240 --> 00:04:33,240
but I'm also going to say fruit + + "Pie."
71
00:04:33,820 --> 00:04:36,940
So when I run this code, what do you think will happen?
72
00:04:39,730 --> 00:04:44,680
Well, it prints out apple, and then it prints out Apple Pie
73
00:04:45,220 --> 00:04:49,270
And then it goes back to the start and it assigns the variable fruit to the next
74
00:04:49,270 --> 00:04:52,360
item, peach peach pie, pear pear pie.
75
00:04:53,080 --> 00:04:58,080
So this is how we can implement a simple for loop that loops through a list
76
00:04:58,930 --> 00:05:03,370
and assigns a variable name to each of the items in the list in order,
77
00:05:03,760 --> 00:05:07,870
and then inside the for loop after the colon, after some indentation,
78
00:05:08,140 --> 00:05:13,140
we can do something with that temporary variable for each of the items.
79
00:05:14,470 --> 00:05:19,470
Now I've been talking a lot about the concept of being inside a for loop versus
80
00:05:20,530 --> 00:05:25,210
being outside a for loop. And this is really, really important.
81
00:05:25,510 --> 00:05:30,130
Whenever you see a colon say in our if statement that we saw previously,
82
00:05:30,190 --> 00:05:32,350
or the for loop that we're using here,
83
00:05:32,860 --> 00:05:35,380
the indentation is really,
84
00:05:35,380 --> 00:05:38,470
really important because if it's indented,
85
00:05:38,770 --> 00:05:42,760
then it means that it's inside the for loop and these instructions
86
00:05:42,820 --> 00:05:47,320
all get carried out for as many times as the for-loop will need to repeat.
87
00:05:47,920 --> 00:05:52,540
Now, if I decided that I wanted to say print my,
88
00:05:52,600 --> 00:05:54,250
uh, fruits,
89
00:05:54,430 --> 00:05:59,430
so my list of fruits up here, and I put it inside my for loop,
90
00:05:59,510 --> 00:06:03,410
so indented after the for loop, then as you can imagine,
91
00:06:03,410 --> 00:06:07,160
it's going to print that for as many times as the loop runs,
92
00:06:07,430 --> 00:06:12,290
which is going to be three because there's three items in our list. Now,
93
00:06:12,320 --> 00:06:15,800
if I had indented that back to the beginning,
94
00:06:16,160 --> 00:06:18,860
so now it's no longer inside the for loop,
95
00:06:19,340 --> 00:06:24,340
then it's only going to print once and it's going to print it after the for
96
00:06:24,800 --> 00:06:29,480
loop is done. So notice how it's doing the whole Apple, Apple pie,
97
00:06:29,480 --> 00:06:32,090
et cetera. And then once the loop is finished,
98
00:06:32,150 --> 00:06:36,770
then it jumps to the next line and carries out this instruction.
99
00:06:37,280 --> 00:06:39,500
So the indentation is really, really important,
100
00:06:39,500 --> 00:06:41,450
and you have to be really careful with this.
101
00:06:42,620 --> 00:06:44,630
Have a play around with this code
102
00:06:44,870 --> 00:06:47,300
and if you're still struggling to see how it works
103
00:06:47,300 --> 00:06:52,160
then move the code over to Thonny and step through each step one at a time and
104
00:06:52,160 --> 00:06:53,120
see how it works.
105
00:06:53,600 --> 00:06:57,260
But I hope you're starting to see how loops are really handy at executing an
106
00:06:57,260 --> 00:06:59,600
instruction over and over and over again,
107
00:06:59,990 --> 00:07:02,510
getting the computer to save us time and energy.
108
00:07:03,110 --> 00:07:07,970
A really good example of when loops would come in really handy is,
109
00:07:08,000 --> 00:07:09,920
you know, how Bart Simpson gets punished
110
00:07:09,920 --> 00:07:14,450
and it has to write out a sentence over and over again on the Blackboard. Well,
111
00:07:14,600 --> 00:07:19,070
if only Bart was a programmer, then he would be able to use loops to do this
112
00:07:19,310 --> 00:07:22,520
and he would be able to chill in the corner. Now,
113
00:07:22,760 --> 00:07:24,890
once you're happy with this type of for-loop,
114
00:07:25,190 --> 00:07:28,730
then head over to the next lesson where I've got a coding exercise
115
00:07:28,940 --> 00:07:31,370
that's going to put your knowledge to the test. All right.
116
00:07:31,370 --> 00:07:32,150
So I'll see you there.
11225
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.