WEBVTT

00:00.410 --> 00:06.350
Often you will want to take one action when a conditional test passes and different action in other

00:06.350 --> 00:07.070
cases.

00:07.990 --> 00:11.710
Python's if else syntax makes this possible.

00:11.740 --> 00:17.710
The if else block is similar to a simple if statement, but the else statement allows you to define

00:17.710 --> 00:26.200
an action or a set of actions that are executed and run when the conditional test fails.

00:26.560 --> 00:30.820
When the if test fails, and in this case, we will execute else.

00:30.820 --> 00:33.220
So we will display some message here.

00:33.250 --> 00:43.210
Now let's let's write something that, for example, we have a car, a car here.

00:44.590 --> 00:47.680
And of sexual stroke here.

00:53.350 --> 01:00.550
And we don't this we don't want this car to, for example, older than ten years.

01:00.550 --> 01:01.270
Right.

01:05.350 --> 01:07.020
So let's make this.

01:07.910 --> 01:08.540
An.

01:09.710 --> 01:10.580
Years.

01:11.680 --> 01:15.820
So we want this car to less than ten years.

01:15.820 --> 01:18.820
So we will want a new car, Right.

01:18.850 --> 01:25.180
And that's why we're going to do here that let's make, for example, car model.

01:25.930 --> 01:27.970
Car model here.

01:27.970 --> 01:33.160
It's in this case, let's make it BMW and car here.

01:33.310 --> 01:35.380
In this case, let's make it.

01:39.420 --> 01:41.310
In 2015.

01:41.610 --> 01:46.020
And here we will add if our.

01:48.180 --> 01:49.070
Car model.

01:49.080 --> 01:54.000
So car year -2023in this case.

01:54.000 --> 01:57.690
Of course, today's year is 2023.

01:57.900 --> 01:59.820
And actually, let's delete this for now.

02:04.520 --> 02:05.660
So delete this.

02:07.220 --> 02:08.600
And now.

02:11.910 --> 02:15.720
Career -2020 23.

02:17.870 --> 02:18.710
Is.

02:22.930 --> 02:31.090
So our career, for example, in this case, our career is going to be eight, while eight is less than

02:31.090 --> 02:33.210
ten, then it will evaluate three.

02:33.220 --> 02:33.700
Right?

02:33.700 --> 02:39.490
So in this case, print your car is.

02:42.820 --> 02:44.800
And we will also use format here.

02:44.830 --> 02:51.130
Format your your car is a car here.

02:51.760 --> 02:59.770
The -273 years old and it's almost new.

02:59.770 --> 03:00.430
Right.

03:00.880 --> 03:03.580
And here we will also add else statement.

03:03.580 --> 03:12.850
If our car is has age of more than ten, we will write this here else print.

03:13.980 --> 03:15.090
Sorry.

03:15.240 --> 03:18.120
I don't want or sorry.

03:19.160 --> 03:20.600
Our company.

03:21.730 --> 03:22.450
Doesn't.

03:24.520 --> 03:26.470
Buys cars.

03:26.890 --> 03:28.960
Buy cars.

03:29.230 --> 03:32.650
That more that.

03:33.800 --> 03:36.950
Age is more than ten.

03:36.950 --> 03:43.420
And after I will print again and your your cars.

03:43.430 --> 03:44.690
Your car.

03:44.780 --> 03:46.220
Age is.

03:49.900 --> 03:54.760
Of course we need to add F here format per year.

03:54.970 --> 03:58.780
Per year -2023.

03:58.840 --> 04:02.680
Your car or let's actually instead of car we can also write BMW.

04:02.680 --> 04:03.130
Right.

04:03.130 --> 04:04.990
So or car model.

04:05.020 --> 04:10.450
Car model is years old and it's almost new.

04:11.670 --> 04:14.280
And we can buy it.

04:14.280 --> 04:18.870
And here also our company doesn't buy BMW.

04:18.960 --> 04:21.450
Let's actually add another format here.

04:21.690 --> 04:25.260
So we will add F here and format.

04:26.370 --> 04:26.970
Car.

04:28.510 --> 04:32.570
Model that age is more than ten and career here.

04:32.590 --> 04:33.940
Now let's run this.

04:34.060 --> 04:37.320
And as you can see here, your BMW oops.

04:37.330 --> 04:40.660
Actually, we need to do this opposite here.

04:42.820 --> 04:44.110
Sorry for this.

04:45.260 --> 04:49.880
We got right numbers, but we got negative here, so we need to do this.

04:50.150 --> 04:55.100
We need to subtract our career from 2023.

04:56.740 --> 04:58.050
Let's do the same here.

04:58.060 --> 04:59.260
And now.

04:59.260 --> 05:03.130
Your BMW is eight years old and it's almost new.

05:03.160 --> 05:06.340
We can buy it, but in LCR.

05:08.290 --> 05:15.430
A else comment here is didn't executed because if here evaluated true.

05:15.460 --> 05:25.270
If we make our car here for example 2005 then it will give your BMW is 18 years old and it almost no

05:25.870 --> 05:27.040
car here.

05:27.070 --> 05:29.380
Of course we need to change it again.

05:29.530 --> 05:32.050
2023 minus car year.

05:32.050 --> 05:34.030
And here we will get else here.

05:34.060 --> 05:37.330
Sorry, our company doesn't buy that buy BMW.

05:37.330 --> 05:41.470
That age is more than ten and your car age is 18.

05:41.470 --> 05:42.280
So.

05:43.340 --> 05:47.480
First in first example, actually, let's try again 2015.

05:48.020 --> 05:58.820
So in this example, our if conditional test passes and the first block of this print here call is executed.

05:58.820 --> 06:06.680
So if the test evaluates to false, then the else block is executed, for example, 2005.

06:06.680 --> 06:13.580
And in this case, our false will not be executed because it is here because.

06:15.490 --> 06:17.170
It is false, right?

06:17.170 --> 06:18.770
So it will not execute.

06:18.790 --> 06:22.630
Instead, our LS command here will execute here.

06:23.290 --> 06:29.260
So this code works because it has only two possible situations to evaluate.

06:29.740 --> 06:33.620
A car is either old enough or not.

06:33.640 --> 06:34.060
Right.

06:34.060 --> 06:42.570
So the if else structures works well in situations in which you want Python to always execute one of

06:42.580 --> 06:44.470
possible situations or actions.

06:44.470 --> 06:52.570
So in simple if else chain like this, one of two actions will always be executed.

06:52.570 --> 06:56.850
No matter if our car here is 1000 or so here, as you can see here.

06:56.860 --> 07:01.360
So our company owned by BMW age is more than ten.
