WEBVTT

00:00.110 --> 00:04.910
In this lesson we are going to work on loops and let's start with a problem.

00:04.910 --> 00:08.540
So imagine that we want to print something ten times.

00:08.570 --> 00:11.630
Let's say I have my print hello.

00:11.870 --> 00:14.420
And I want to do that ten times.

00:14.420 --> 00:20.240
So the first uh, kind of obvious solution is you would just write this ten times.

00:20.240 --> 00:24.110
So here just copy and paste ten times, okay.

00:24.170 --> 00:25.850
Eight, nine and ten.

00:26.090 --> 00:33.860
If I run this well we expect to have hello printed ten times and that's what we have.

00:33.860 --> 00:35.060
So this works.

00:35.060 --> 00:37.550
But it's very far from being optimal okay.

00:37.580 --> 00:41.150
Writing the same code many times is really not efficient.

00:41.150 --> 00:44.210
And it's quite a bad practice when programming okay.

00:44.240 --> 00:46.970
The best is not to repeat yourself too much.

00:46.970 --> 00:49.490
And also, what about if we want to print hello?

00:49.490 --> 00:53.570
Not ten times, but 100 times or 1000 times?

00:53.600 --> 00:56.570
Okay, you're not going to write this 1000 times.

00:56.570 --> 01:03.720
In this case you are going to use Loops, so a loop will be used to repeat a given block of code.

01:03.750 --> 01:05.040
Here, for example, the print.

01:05.070 --> 01:05.730
Hello.

01:05.760 --> 01:11.760
You only need to write the block of code once, and then it will be repeated as many times as it needs

01:11.760 --> 01:12.000
to.

01:12.030 --> 01:14.850
So I'm going to remove all of this for now.

01:14.850 --> 01:17.760
And there are two main ways to write loops.

01:17.760 --> 01:21.000
You can use a for loop and use a while loop.

01:21.000 --> 01:24.120
I'm going to show you both here in this lesson.

01:24.120 --> 01:25.830
And let's start with the for loop.

01:25.830 --> 01:30.300
So I'm going to write a very simple for loop and then explain to you what it does.

01:30.330 --> 01:32.820
Okay I'm going to start with the for keyword.

01:32.820 --> 01:36.690
You can see we have the syntax highlighting for the for keyword.

01:36.720 --> 01:41.070
And then I'm going to put I in range okay.

01:41.100 --> 01:48.180
Open parentheses between zero comma and ten okay I put the colon I go back to a new line.

01:48.180 --> 01:50.310
We have an indentation and I print.

01:50.790 --> 01:51.660
Hello.

01:52.350 --> 01:55.500
So that's the block of code that I want to execute.

01:55.530 --> 01:58.170
Let's just run this to see.

01:58.350 --> 02:00.330
And we have you can see.

02:00.360 --> 02:00.750
Hello.

02:00.750 --> 02:02.130
Printed ten times.

02:02.160 --> 02:02.310
Okay.

02:02.340 --> 02:05.160
So that's the exact same behavior as the previous one.

02:05.190 --> 02:07.620
Now let me explain what I did here.

02:07.620 --> 02:11.280
So first this line we have the for keyword okay.

02:11.310 --> 02:13.110
That's going to create a for loop.

02:13.230 --> 02:16.680
Then we have I here I is a variable.

02:16.680 --> 02:17.730
It's an index.

02:17.730 --> 02:22.650
So you create a local variable in this for loop usually for basic for loops.

02:22.680 --> 02:25.680
Or when you want to go through an index or a counter.

02:25.680 --> 02:27.840
We just use I as a convention okay.

02:27.840 --> 02:30.990
You could use any uh, variable okay.

02:31.020 --> 02:31.920
It doesn't really matter.

02:31.920 --> 02:35.730
But we use I then you have the in keyword okay.

02:35.730 --> 02:39.810
So for I in and here usually you will have a list.

02:39.840 --> 02:42.300
We're going to see list in the next lesson.

02:42.300 --> 02:46.800
So list or for example here a range that's the same thing.

02:46.800 --> 02:50.460
So this range it just means that it's going to be between zero.

02:50.460 --> 02:54.870
And so this value and then that value minus one okay.

02:54.900 --> 02:58.750
So 0123456789.

02:58.750 --> 02:59.350
And that's it.

02:59.350 --> 03:01.720
So we have ten values between 0 and 9.

03:01.720 --> 03:03.190
Then we have a colon.

03:03.190 --> 03:05.800
And that's it for the first line of the file.

03:05.830 --> 03:06.070
Okay.

03:06.100 --> 03:08.260
So with this we have the definition of the for loop.

03:08.260 --> 03:14.080
And inside you can see with an indentation we have the code that we want to repeat.

03:14.080 --> 03:18.970
So what's going to happen is that this code is going to be repeated as many times as we have.

03:18.970 --> 03:24.070
Well asked here which is basically a range between 0 and 10 minus one.

03:24.070 --> 03:25.540
So we have ten values total.

03:25.570 --> 03:27.820
Just one thing here I printed ten times.

03:27.820 --> 03:30.130
But let's come back to our previous problem.

03:30.130 --> 03:36.250
If I want to print it a thousand times, I just write 1000 here and I'm going to run this.

03:36.280 --> 03:43.660
You can see it's taking a bit more time, because executing this 1000 times is not going to be instant.

03:43.660 --> 03:44.590
And now we are done.

03:44.590 --> 03:50.140
We have hello printed 1000 times okay, so you could count but no need.

03:50.380 --> 03:54.370
Let's go back to ten and I'm going to talk about the indentation here okay.

03:54.400 --> 03:55.760
You can see I have an indentation.

03:55.760 --> 03:58.100
So the indentation is very important.

03:58.130 --> 04:04.070
Now you can see if I go back to a new line I am still with an indentation, which means I am still in

04:04.100 --> 04:04.790
the for loop.

04:04.790 --> 04:09.680
I need to go back here to the previous indentation so that I am out of the for loop.

04:09.710 --> 04:15.860
Let's just add a print end like this and let's run it.

04:15.860 --> 04:20.060
And you can see we have hello ten times and then end.

04:20.450 --> 04:20.780
All right.

04:20.810 --> 04:23.870
So the end is executed after the for loop.

04:23.900 --> 04:24.050
Okay.

04:24.080 --> 04:27.680
So you can see the for loop has a loop going through this block of code.

04:27.680 --> 04:29.780
And after ten times it's going out.

04:29.780 --> 04:31.250
And then we print end.

04:31.280 --> 04:37.610
If I put end like this you can see with an indentation even if we have an empty line here.

04:37.640 --> 04:37.820
Okay.

04:37.850 --> 04:38.570
Empty lines.

04:38.570 --> 04:39.710
They don't do anything.

04:39.740 --> 04:41.150
Look at what's going to happen.

04:41.150 --> 04:45.830
You can see we have hello and hello and printed ten times okay.

04:45.860 --> 04:50.390
Because this print is in the for loop because of the indentation.

04:50.390 --> 04:53.790
If I remove the indentation the print is out of the loop.

04:53.790 --> 04:55.770
So that's very important detail.

04:55.800 --> 04:56.070
Okay.

04:56.100 --> 04:59.550
Now let's come back to this AI variable that I created.

04:59.550 --> 05:05.610
I told you that we create a local variable inside the for loop that's going to go through this range.

05:05.610 --> 05:07.290
But we can actually print this.

05:07.290 --> 05:08.640
So we can say hello.

05:08.640 --> 05:13.200
And then plus and because it's going to be an integer number.

05:13.200 --> 05:17.340
And now you know that we cannot concatenate a string with an integer number.

05:17.340 --> 05:20.490
We need to cast this as a string just like that.

05:20.520 --> 05:23.640
Make sure you add the correct amount of parentheses.

05:23.640 --> 05:25.560
So I'm going to run this.

05:26.100 --> 05:29.730
And now you can see we have hello zero.

05:29.730 --> 05:34.920
And then hello 12345678 and nine.

05:34.950 --> 05:38.760
I can add a space if I want to to make it a bit cleaner.

05:39.510 --> 05:40.020
Okay.

05:40.050 --> 05:43.680
You can see here clearly that we are going between 0 and 9.

05:43.680 --> 05:45.330
So we have ten values total.

05:45.360 --> 05:45.690
All right.

05:45.690 --> 05:48.120
So that's how a for loop works.

05:48.150 --> 05:52.620
Now there is another way to do what we did here using the while loop.

05:52.620 --> 05:53.940
So let's see how that works.

05:53.940 --> 05:57.420
I'm going to comment this code here.

05:58.830 --> 06:01.230
So right click and toggle comment.

06:01.650 --> 06:07.020
And I'm going to write a while loop I will keep this print n here.

06:07.020 --> 06:09.210
And let's do the same with a while loop.

06:09.210 --> 06:12.030
So first let me write the code and then I will explain it to you.

06:12.030 --> 06:15.690
So here I'm going to create a variable I is equal to zero.

06:15.690 --> 06:20.670
And then I will do while I is strictly lower than ten.

06:21.060 --> 06:27.930
And then colon go back to a new line and we execute a block of code, which is this print that I'm just

06:27.930 --> 06:30.090
going to copy here is the same.

06:30.390 --> 06:32.880
So I copy and paste it right there.

06:33.000 --> 06:38.850
And after that I will do I is equal to I plus one.

06:38.850 --> 06:43.560
So while I is lower than ten strictly lower than ten we print hello with the I.

06:43.560 --> 06:45.600
And then we add one to I.

06:45.600 --> 06:48.000
And this is going to be run ten times.

06:48.000 --> 06:50.320
Let's actually see Okay.

06:50.320 --> 06:52.090
We have the exact same, you see.

06:52.120 --> 06:52.360
Hello.

06:52.390 --> 06:53.140
Zero until.

06:53.170 --> 06:53.980
Hello, nine.

06:53.980 --> 06:57.460
So this block of code is doing the same thing as that one.

06:57.460 --> 07:00.820
Now let's see in detail how this is executed.

07:00.850 --> 07:01.090
Okay.

07:01.120 --> 07:04.510
So the while loop is actually going to test here.

07:04.510 --> 07:07.420
You can see this is a condition okay.

07:07.420 --> 07:11.380
So you can see the while actually has an if.

07:11.380 --> 07:13.150
So it's an if with a loop.

07:13.180 --> 07:17.170
Here you can read this as if I is strictly lower than ten.

07:17.170 --> 07:20.050
So of course here we test a condition with a variable.

07:20.050 --> 07:22.660
So that's why we have created the variable before okay.

07:22.690 --> 07:26.710
In the for loop here you can directly create the variable inside the definition of the for.

07:26.710 --> 07:31.330
But for the while we're going to create the variable before are we going to use another variable from

07:31.330 --> 07:31.930
another place.

07:31.930 --> 07:34.120
But here we create this variable here okay.

07:34.150 --> 07:38.620
So if I is strictly lower than ten we're going to execute this block of code.

07:38.710 --> 07:44.020
Then the difference is that with the if you just execute this once or not and then you go on the next

07:44.020 --> 07:49.040
line here line number eight with a while after we have executed this.

07:49.040 --> 07:54.560
So if this condition is true, we go back to line five and we test the condition again.

07:54.560 --> 07:59.900
And as long as this condition is true, then we keep executing this block of code.

07:59.900 --> 08:02.990
If the condition is false, then we go out of the while.

08:03.020 --> 08:03.260
Okay.

08:03.290 --> 08:06.950
So what's happening exactly we have I is equal to zero.

08:06.980 --> 08:08.450
Then we test is I.

08:08.480 --> 08:10.880
So is zero strictly lower than ten.

08:10.910 --> 08:11.750
Yes.

08:11.750 --> 08:16.610
So we go in the while we print hello zero.

08:16.610 --> 08:21.800
And then we say that I is equal to its current value plus one okay.

08:21.830 --> 08:23.600
So I becomes one.

08:23.630 --> 08:24.650
Then we test again.

08:24.680 --> 08:25.130
Here we go.

08:25.130 --> 08:26.210
Back to line five.

08:26.240 --> 08:28.280
Is one strictly lower than ten.

08:28.310 --> 08:30.290
Yes we do the same thing.

08:30.290 --> 08:32.120
And then I is equal to two.

08:32.150 --> 08:34.430
We go all the way up to nine.

08:34.700 --> 08:37.040
Then we print with nine and then nine.

08:37.040 --> 08:38.570
So I becomes ten.

08:38.600 --> 08:41.720
We go back here is ten strictly lower than ten.

08:41.720 --> 08:42.290
No.

08:42.290 --> 08:46.490
So we go out of the loop and then we print end okay.

08:46.490 --> 08:47.640
So that's how it works.

08:47.640 --> 08:53.610
And a very important thing here is you can see we have to uh, in this case we have to increment the

08:53.610 --> 08:55.110
value ourself, okay.

08:55.140 --> 08:59.970
Because if we don't do this well we're going to have what we call an infinite loop.

08:59.970 --> 09:05.160
And that can be a problem because if you don't let's say comment this line, you see if you don't increment

09:05.190 --> 09:07.950
I then I is always going to be zero.

09:07.950 --> 09:10.350
And so this condition is always going to be true.

09:10.380 --> 09:14.280
And so we are going to execute this an infinite amount of time.

09:14.280 --> 09:16.770
And we are never going to go out of the while loop.

09:16.770 --> 09:21.720
So that's why we add one to I here and here to write this.

09:21.750 --> 09:24.690
Well there is an easier way to write that in Python.

09:24.900 --> 09:31.080
If you just want to add a number to a variable instead of doing variable is equal to variable plus number,

09:31.080 --> 09:35.640
you can just use plus equal like that okay.

09:35.640 --> 09:40.410
If you do I plus equal one just means that you add one to I.

09:40.440 --> 09:40.740
Okay.

09:40.740 --> 09:42.300
So it's a bit better like this.

09:42.330 --> 09:42.720
Great.

09:42.720 --> 09:48.270
And now you have seen the for loop and the while loop, and the question is when to use a while loop

09:48.270 --> 09:50.250
and when to use a for loop.

09:50.280 --> 09:50.430
Okay.

09:50.460 --> 09:55.770
Basically, you will use a while loop when you don't know how many times you need to execute the block

09:55.770 --> 09:56.490
of code.

09:56.520 --> 10:02.070
For example, if you read the temperature from a sensor and you need to keep executing action while

10:02.070 --> 10:07.050
the temperature is below ten degrees, then you will need a while loop because you don't know how long

10:07.050 --> 10:10.110
it will take for the temperature to rise above ten degrees.

10:10.110 --> 10:15.870
So you need to continue until the condition that the temperature is greater than ten is met.

10:15.900 --> 10:17.490
Now another example.

10:17.490 --> 10:23.100
If you need to read the temperature a thousand times and then to compute the average temperature, then

10:23.100 --> 10:27.870
you can use a for loop because you know exactly how many times you need to do the action.

10:27.900 --> 10:29.190
Here it's 1000 times.

10:29.190 --> 10:34.440
And so for the example we used here, because we know exactly how many times we need to print.

10:34.470 --> 10:34.710
Hello.

10:34.740 --> 10:36.180
We want to print hello ten times.

10:36.180 --> 10:39.720
Then you can see that a for loop is a more appropriate choice.

10:39.750 --> 10:44.400
And later in this course we will use both for and while loops for different purposes.
