WEBVTT

00:00.710 --> 00:01.460
Hello.

00:01.670 --> 00:04.340
Welcome to another lecture of our python course.

00:04.340 --> 00:05.850
And my name is Stephan.

00:05.870 --> 00:12.950
In this lecture you will learn about conditions, but you will also learn if Elif and else chain.

00:12.950 --> 00:17.420
So often you will need to test more than just two possible situations.

00:17.420 --> 00:23.240
And to evaluate these you can use python's if Elif else syntax.

00:23.240 --> 00:31.760
So Python executes only one block in an if Elif else chain, so it runs each conditional test in order

00:31.760 --> 00:33.590
until one passes.

00:33.590 --> 00:40.280
So when a test passes, the code following that test is executed and Python skips the rest of the tests.

00:40.280 --> 00:44.180
So many real world situations involve more than two possible conditions.

00:44.180 --> 00:51.350
For example, consider a bus ticket, a mechanism bus ticket technology that charges different rates

00:51.350 --> 00:53.180
for different age groups.

00:53.180 --> 00:59.930
For example, bus tickets for anyone under age eight is free bus ticket for anyone between the ages

00:59.930 --> 01:02.880
of eight and 18 is $50.

01:02.880 --> 01:08.730
Bus ticket from anyone age 18 or older is $90.

01:08.730 --> 01:15.240
So how can we use an If statement to determine a person's bus ticket rate?

01:15.240 --> 01:18.270
So this code we will write here.

01:18.270 --> 01:24.060
For example, let's make the my age here, for example.

01:24.060 --> 01:31.710
My age is 25 for and we will change this age to test different conditions.

01:32.250 --> 01:40.320
If our age is here, let's actually let's actually write the first statement here.

01:41.400 --> 01:46.920
So if a ticket for anyone, a bus ticket for anyone under age is free.

01:47.040 --> 01:54.240
And so if age is less than eight, then we will print.

01:55.020 --> 01:55.950
Your.

01:58.470 --> 02:00.750
You need to pay.

02:02.320 --> 02:03.370
$0.

02:05.360 --> 02:05.600
Or.

02:05.600 --> 02:11.930
Yeah, let's actually, instead of writing that your ticket is free here.

02:12.460 --> 02:13.210
And.

02:13.830 --> 02:17.090
Here of my age.

02:17.100 --> 02:27.300
And after that we will add else if I will explain all of this here else if Elif here, Elif my age and

02:27.300 --> 02:29.970
here, let's let's actually take this.

02:31.810 --> 02:32.170
Here.

02:32.290 --> 02:34.090
So we did this.

02:34.090 --> 02:34.600
Now.

02:34.600 --> 02:35.500
Now we are.

02:35.920 --> 02:37.570
We need to do the second here.

02:37.570 --> 02:44.140
So bus ticket for anyone between the ages of eight and 18 is $50.

02:44.170 --> 02:45.550
Now we will write that.

02:45.550 --> 02:50.620
So if my age is less than.

02:56.720 --> 02:57.260
Here.

02:57.290 --> 03:03.350
Anyone between the ages of four and 18 is $50.

03:08.340 --> 03:11.410
And here we will write 18.

03:12.660 --> 03:13.320
My age.

03:13.320 --> 03:13.680
18.

03:13.680 --> 03:15.450
So you will explain here.

03:15.450 --> 03:20.610
You might be wondering why we didn't included eight here, which I will explain now after writing this

03:20.610 --> 03:29.010
code and you will understand because here print we will use your ticket.

03:29.130 --> 03:30.870
Ticket is.

03:33.700 --> 03:36.850
Bicycle, $50, $50.

03:37.270 --> 03:42.700
And after that, we will also add clouds here and here.

03:43.580 --> 03:44.210
Print.

03:45.130 --> 03:49.750
So your ticket is $100.

03:52.940 --> 03:55.500
So also we did this section.

03:55.520 --> 03:56.990
Mark This one.

03:58.600 --> 03:59.160
That's it.

03:59.170 --> 04:07.720
So firstly, the if test checks whether a person is under eight years old.

04:07.720 --> 04:15.820
So when test passes, an appropriate message is printed and Python skips the rest of the tests.

04:15.820 --> 04:19.780
And here, let's try this for example, if my age was five.

04:20.300 --> 04:28.280
And here, as you can see, your ticket is free and the life line is really another if test so which

04:28.280 --> 04:32.630
runs only if this previous test fails.

04:32.630 --> 04:36.980
So in this case it didn't fail here and this code didn't run.

04:37.990 --> 04:44.200
So at this point in the chain, we know that the person is at least eight years old because the first

04:44.200 --> 04:44.890
test.

04:45.960 --> 04:48.840
Uh, is let's actually, uh, make here.

04:50.750 --> 04:56.810
Now, in this case, we know the person is at least eight years old because the first test didn't run

04:56.810 --> 04:57.380
right.

04:57.770 --> 05:08.780
So if the person is under 18, an appropriate message is printed and Python will not execute the else

05:08.780 --> 05:09.140
block.

05:09.140 --> 05:19.910
Now, Python will now execute because our Elif here, our Elif is executed, but also our if didn't

05:19.910 --> 05:26.630
execute because it didn't meet meet the conditions here because my age was ten.

05:27.440 --> 05:28.160
And.

05:29.080 --> 05:36.790
Here if my age under eight, then it didn't print because my age is not under eight.

05:36.940 --> 05:43.990
You learned that in expressions previous lectures, and here we have Elif, which is my age, is under

05:43.990 --> 05:47.740
18 and here we have ten here.

05:47.830 --> 05:56.260
So in this example here, the if test, let's actually yes, here in this example, the if test evaluates

05:56.260 --> 05:59.740
false so it's code block is not executed.

05:59.740 --> 06:06.400
However, the Elif test evaluates true which is the ten is less than 18.

06:06.400 --> 06:06.640
Right.

06:06.640 --> 06:08.920
So its code is executed.

06:08.920 --> 06:16.420
So the output is one sentence here informing the user that admission cost is $50.

06:16.420 --> 06:18.280
So your ticket is $50.

06:18.280 --> 06:27.700
Any age greater than 17 would cause the first the if and Elif tests to fail in this situations here,

06:27.700 --> 06:35.030
let's actually make it 20 In this situations the else block will be executed here and as you can see

06:35.030 --> 06:37.010
here else block is executed.

06:37.010 --> 06:44.030
This block is in the if and Elif didn't execute because they didn't meet the condition here and here

06:44.030 --> 06:47.180
we have the else block executed here.

06:48.010 --> 06:51.250
And we printed this message here.

06:52.760 --> 06:59.570
So rather than the printing the ticket price with the if else block, it would be more concise to just

06:59.570 --> 07:04.160
set the price inside the if else chain.

07:04.160 --> 07:09.300
And then we have a single print call that runs after the chain has been evaluated.

07:09.320 --> 07:14.320
So now we will let's delete this and we will do another example with that.

07:14.330 --> 07:20.390
So here, if my age is 20, remember that the.

07:21.910 --> 07:26.610
For under a under age of eight, it will be free for bus ticket.

07:26.620 --> 07:33.850
So my age will be or and we will also add price here.

07:34.650 --> 07:36.240
Price is going to.

07:37.360 --> 07:42.950
Zero and my age eight while my age is other than 18.

07:42.970 --> 07:45.610
Then the price is going to be price.

07:46.300 --> 07:57.730
Price is going to be 25, the 50 and else if our age is greater than 18, we will praise we will use

07:57.730 --> 08:00.400
the price 19 here.

08:01.100 --> 08:06.770
And after that we will print here we will use format.

08:06.800 --> 08:14.690
Your bus ticket cost is here and we will use this price.

08:14.690 --> 08:15.590
That's it.

08:15.740 --> 08:18.110
So here, let's run this.

08:18.110 --> 08:19.570
And here we are.

08:19.570 --> 08:24.320
Our age is 20 and our else block is executed here.

08:24.320 --> 08:32.930
So the indented lines here you see here, these are the indented lines set the value of price according

08:32.960 --> 08:36.230
to the person's age, as in the previous example.

08:36.410 --> 08:48.470
So after the price is set by the if here, if Elif and else conditions as separate on indented print

08:48.470 --> 08:52.250
call uses this value the price value.

08:52.970 --> 08:54.290
Um, two.

08:54.920 --> 08:56.870
So price value.

08:58.110 --> 08:59.850
To display this message, right?

09:02.250 --> 09:08.690
And so this here, this code produces the same output as the previous example.

09:08.700 --> 09:14.550
But the purpose of the if Elif else chain is narrower.

09:14.550 --> 09:21.870
So instead of determining a price and displaying a message, it simply determines the ticket price.

09:22.020 --> 09:29.220
And in addition to being more efficient, the reveals this code here is easier to modify than the original

09:29.220 --> 09:33.620
approach that we did in earlier in this lecture.

09:33.630 --> 09:39.960
So to change the text of the ultimate message, you will need to change only this print call rather

09:39.960 --> 09:44.760
than three separate print calls that was here before.
