WEBVTT

00:00.080 --> 00:06.020
So for now, you have seen that all the instructions you write in a Python program will be executed

00:06.020 --> 00:07.880
one by one.

00:07.880 --> 00:12.770
But as you progress, you will want to make your programs more dynamic, and you will want to be able

00:12.770 --> 00:17.930
to execute only some instructions depending on some specific conditions.

00:17.930 --> 00:24.440
For example, you might do a certain action if a button is pressed, and if it's not pressed, then

00:24.440 --> 00:26.420
you will do another action okay.

00:26.450 --> 00:33.770
So in your code some instructions might be executed and some other instructions might not be executed.

00:33.770 --> 00:39.350
And let's write our first condition in Python I will start by creating a variable.

00:39.350 --> 00:45.770
Let's let's name it temperature is equal to for example 21.

00:45.770 --> 00:50.930
So even though float numbers would be better for temperatures, I'm just going to use an integer here

00:50.930 --> 00:53.240
to keep it simple for this example okay.

00:53.270 --> 00:54.830
So we have our variable.

00:54.830 --> 00:58.610
And I'm going to write a condition here how to write a condition.

00:58.610 --> 01:01.220
Well you have the if keyword.

01:01.220 --> 01:03.860
As you can see we have the syntax Highlighting on.

01:03.890 --> 01:10.910
If so, in Python, you start with if then we are going to test a condition and this condition is going

01:10.940 --> 01:13.580
to be evaluated to true or false.

01:13.580 --> 01:16.430
So let me write something first and then I will explain it to you.

01:16.430 --> 01:24.770
So if temperature I use this angle bracket like this which means greater than let's say 20.

01:24.800 --> 01:28.220
Then I add a colon and I go back to a new line.

01:28.220 --> 01:32.210
And you can see we have a new indentation of four spaces.

01:32.300 --> 01:33.800
So what does that mean?

01:33.800 --> 01:36.830
What will the if keyword we enter a condition.

01:36.830 --> 01:42.350
This here is going to be evaluated to true or to false.

01:42.350 --> 01:46.340
If this is evaluated to true then well you can see we end.

01:46.370 --> 01:53.810
If with a colon then we are going to execute whatever code is here after the if with an indentation.

01:53.810 --> 01:59.600
But if it is false, we are not going to execute this block of code here.

01:59.630 --> 01:59.960
Okay.

01:59.990 --> 02:07.770
So here we test if the temperature is greater than 20 and if the temperature is greater than 20, I'm

02:07.770 --> 02:09.120
going to write print.

02:09.150 --> 02:12.750
For example, it's warm.

02:12.780 --> 02:13.080
Okay.

02:13.110 --> 02:15.090
Here I'm using the metric system.

02:15.090 --> 02:17.370
I use the Celsius degrees okay.

02:17.370 --> 02:20.820
But it's not really going to be a problem for comprehension of conditions.

02:20.850 --> 02:21.270
Okay.

02:21.270 --> 02:23.760
So we have written our first condition.

02:23.790 --> 02:25.560
Now let's test it.

02:25.800 --> 02:27.990
And you can see we have it's warm.

02:28.020 --> 02:33.300
So what happened is that when we first create the variable temperature we assign a value to it.

02:33.330 --> 02:34.290
21.

02:34.320 --> 02:36.990
Then we test if temperature greater.

02:36.990 --> 02:39.450
And actually this is strictly greater.

02:39.480 --> 02:43.260
And I'm going to come back to conditional operators in the next lesson.

02:43.290 --> 02:43.500
Okay.

02:43.530 --> 02:46.020
For now we just focus on the condition itself.

02:46.020 --> 02:51.000
So if the temperature is strictly greater than 20 which means at least 21, then we print.

02:51.000 --> 02:53.040
It's warm because it's 21.

02:53.070 --> 02:55.260
Then we print it warm okay.

02:55.290 --> 02:59.310
If I put 41 we are also going to print.

02:59.340 --> 03:00.060
It's warm.

03:00.090 --> 03:03.180
Now let's say I put 18 degrees.

03:03.990 --> 03:08.340
Let's run the script and you can see that we don't print anything.

03:08.340 --> 03:13.050
So this line is only executed if this condition here is true.

03:13.080 --> 03:13.380
All right.

03:13.410 --> 03:16.500
So that's already making our program more dynamic.

03:16.530 --> 03:18.690
I'm going to add a new line after that.

03:18.690 --> 03:23.040
So you can see that after I press enter I'm still in this in notation.

03:23.040 --> 03:27.060
So I'm still inside the if and I can add more stuff inside the if.

03:27.090 --> 03:27.600
Okay.

03:27.630 --> 03:31.680
If I go back here I am back to the first indentation.

03:31.680 --> 03:33.960
So we are out of the if structure.

03:33.990 --> 03:35.880
I'm just going to add a space for readability.

03:35.880 --> 03:40.920
And let's print for example end of program.

03:40.920 --> 03:42.540
So let's print that.

03:43.410 --> 03:45.150
You can see we have end of program.

03:45.150 --> 03:52.800
So even though we didn't execute that line because it's in the if and this is false, we still execute

03:52.800 --> 03:56.340
this because we are after the if and this is outside of the if.

03:56.370 --> 04:02.220
Now if I for example, add an indentation here, you can see we will still be in the.

04:02.250 --> 04:05.610
If so we will not print end of program.

04:05.640 --> 04:06.240
Okay.

04:06.240 --> 04:10.290
And in this case it will only be printed if for example I put 25.

04:11.730 --> 04:12.270
then.

04:12.300 --> 04:15.270
End of program is going to be printed because it's inside the if.

04:15.300 --> 04:15.690
Okay.

04:15.720 --> 04:18.120
So pay attention to the indentation.

04:18.120 --> 04:19.470
It's very important.

04:19.500 --> 04:19.860
Great.

04:19.860 --> 04:22.680
So a condition you can see it's not very complicated.

04:22.710 --> 04:29.970
If and then condition to test colon and then a block of code to execute if it is true.

04:30.000 --> 04:31.770
Now you can add after the.

04:31.800 --> 04:36.990
If so you go back to the same indentation as the if you can add an else keyword.

04:37.140 --> 04:39.300
So you put else colon.

04:39.300 --> 04:40.470
Go back to a new line.

04:40.470 --> 04:41.850
You have an indentation.

04:41.850 --> 04:47.130
And let's say for example print not so one.

04:48.090 --> 04:49.710
What does the else do.

04:49.740 --> 04:55.050
Well if this condition is true you're going to execute this block of code and that's it.

04:55.050 --> 05:02.520
But if this condition is false you will execute what's inside the else keyword the else block here.

05:02.550 --> 05:03.150
Okay.

05:03.150 --> 05:08.010
So if the temperature is 25 you can just print it's one.

05:08.010 --> 05:16.960
And you can see this one was not executed because we only execute it It's warm, but if I put 15 in

05:16.960 --> 05:21.550
this case, you will see we have not so warm.

05:21.940 --> 05:25.090
Okay, so what happened here is that we go to the.

05:25.120 --> 05:30.070
If we evaluate this condition this is going to be false.

05:30.070 --> 05:34.750
So if this is false we go to the else block and we execute the else block.

05:34.750 --> 05:40.780
And then of course we also execute end of program because it's out of the if and on top of if and else.

05:40.780 --> 05:51.160
You can also add in between if you want to else if and actually in Python it's not else if it's been

05:51.160 --> 05:54.010
condensed into Elif okay.

05:54.010 --> 05:55.540
So Elif means else.

05:55.570 --> 05:58.990
If so you have the if Elif and else keywords.

05:58.990 --> 06:01.600
With Elif, you can test another condition.

06:01.600 --> 06:05.110
So, for example, if temperature is greater than 20, it's one.

06:05.140 --> 06:06.580
But then you can test.

06:06.580 --> 06:12.820
So if this is false, we're going to test if temperature is greater than ten.

06:14.020 --> 06:15.770
And you can see we have a new edition.

06:15.770 --> 06:16.790
Let's print.

06:16.820 --> 06:17.750
Well, let's print.

06:17.780 --> 06:19.310
Not so warm here.

06:22.610 --> 06:24.080
Then I'm going to go back to a new line.

06:24.080 --> 06:27.350
And you can see I removed the indentation and I can add another Elif.

06:27.380 --> 06:29.840
Okay, I can add as many Elif as I want.

06:29.870 --> 06:33.560
Let's say temperature greater than zero.

06:34.880 --> 06:35.960
Print.

06:36.380 --> 06:40.760
It's cold and I'm going to change the else to.

06:40.790 --> 06:46.580
It's freezing, so it's freezing.

06:46.580 --> 06:48.200
So what's happening now?

06:48.200 --> 06:54.530
Well, let's make a test with 15, actually, and let's see what's happening and understand this flow

06:54.530 --> 06:55.340
here.

06:55.340 --> 06:56.960
So we have not so warm.

06:56.960 --> 06:57.980
So what happened.

06:57.980 --> 07:00.470
We have this entire if structure.

07:00.500 --> 07:02.210
We start at the first.

07:02.240 --> 07:06.530
If we check if this is true then we're going to execute.

07:06.530 --> 07:07.790
It's warm and that's it.

07:07.820 --> 07:09.650
We don't execute any of the other.

07:09.650 --> 07:13.010
We don't even test the other else if other else okay.

07:13.040 --> 07:16.190
But in this case with temperature 15 well this is false.

07:16.190 --> 07:17.930
So if this is false we're going to check.

07:17.950 --> 07:19.210
Do we have another leaf?

07:19.240 --> 07:19.900
Yes.

07:19.900 --> 07:24.190
So we're going to test this one if temperature is greater than ten.

07:24.190 --> 07:25.210
And this is true.

07:25.210 --> 07:27.640
So we are going to print not so warm.

07:27.640 --> 07:31.840
And then we go out of the if we don't even test the next ones.

07:31.840 --> 07:34.660
So now I can make another example with five.

07:36.010 --> 07:40.210
And you can see now we have it's cold because this condition was false.

07:40.210 --> 07:41.770
So then we try the next one.

07:41.770 --> 07:43.690
This one is false.

07:43.690 --> 07:44.830
We try the next one.

07:44.830 --> 07:45.970
This one is true.

07:45.970 --> 07:47.770
So we print it's cold.

07:47.770 --> 07:50.170
And finally let's say minus five.

07:51.550 --> 07:54.970
Then we're going to have it's freezing okay.

07:55.000 --> 07:56.860
Because this one is going to be false.

07:56.860 --> 07:58.510
And then this one is going to be false.

07:58.510 --> 07:59.770
This one is going to be false.

07:59.770 --> 08:01.330
Finally we go to the else.

08:01.360 --> 08:01.720
All right.

08:01.750 --> 08:06.820
So if you have any temperature that's greater than 20 we're going to get here.

08:07.060 --> 08:11.440
But then if the temperature is greater than ten here actually you know that temperature is greater than

08:11.440 --> 08:16.240
ten, but also that the temperature is lower than 20.

08:16.270 --> 08:21.110
Because if it was greater than 20, you would have stayed here.

08:21.140 --> 08:25.820
Okay, so when you arrive in this leaf, actually, you know that the temperature is kind of between

08:25.820 --> 08:26.900
10 and 20.

08:26.930 --> 08:27.860
Same thing here.

08:27.860 --> 08:33.290
You know, the temperature is going to be between 0 and 10, because if it was greater than ten you

08:33.290 --> 08:36.260
would be in this block okay.

08:36.290 --> 08:41.060
And finally with the else you know that it's freezing because well, negative temperature is freezing.

08:41.060 --> 08:45.680
And you know that if the temperature is not greater than zero, then it's freezing.

08:45.680 --> 08:52.160
So you can see with this the order of the conditions is quite important.

08:52.160 --> 08:59.210
And you can create if blocks as simple as just one, if you can add as many Elif as you want and only

08:59.210 --> 09:01.790
one else if you want to add an else.

09:01.790 --> 09:03.410
But everything is optional.

09:03.440 --> 09:10.340
Okay, you can just the minimum code for a if structure for condition is basically this okay if condition

09:10.340 --> 09:16.040
colon and then a block of code with one line or as many lines as you want, and then you just need to

09:16.070 --> 09:23.000
go out of the indentation here to go back to the main execution of the code that will be line by line

09:23.000 --> 09:23.780
once again.
