Hello.
Welcome to another lecture of our python course.
And my name is Stephan.
In this lecture you will learn about conditions, but you will also learn if Elif and else chain.
So often you will need to test more than just two possible situations.
And to evaluate these you can use python's if Elif else syntax.
So Python executes only one block in an if Elif else chain, so it runs each conditional test in order
until one passes.
So when a test passes, the code following that test is executed and Python skips the rest of the tests.
So many real world situations involve more than two possible conditions.
For example, consider a bus ticket, a mechanism bus ticket technology that charges different rates
for different age groups.
For example, bus tickets for anyone under age eight is free bus ticket for anyone between the ages
of eight and 18 is $50.
Bus ticket from anyone age 18 or older is $90.
So how can we use an If statement to determine a person's bus ticket rate?
So this code we will write here.
For example, let's make the my age here, for example.
My age is 25 for and we will change this age to test different conditions.
If our age is here, let's actually let's actually write the first statement here.
So if a ticket for anyone, a bus ticket for anyone under age is free.
And so if age is less than eight, then we will print.
Your.
You need to pay.
$0.
Or.
Yeah, let's actually, instead of writing that your ticket is free here.
And.
Here of my age.
And after that we will add else if I will explain all of this here else if Elif here, Elif my age and
here, let's let's actually take this.
Here.
So we did this.
Now.
Now we are.
We need to do the second here.
So bus ticket for anyone between the ages of eight and 18 is $50.
Now we will write that.
So if my age is less than.
Here.
Anyone between the ages of four and 18 is $50.
And here we will write 18.
My age.
18.
So you will explain here.
You might be wondering why we didn't included eight here, which I will explain now after writing this
code and you will understand because here print we will use your ticket.
Ticket is.
Bicycle, $50, $50.
And after that, we will also add clouds here and here.
Print.
So your ticket is $100.
So also we did this section.
Mark This one.
That's it.
So firstly, the if test checks whether a person is under eight years old.
So when test passes, an appropriate message is printed and Python skips the rest of the tests.
And here, let's try this for example, if my age was five.
And here, as you can see, your ticket is free and the life line is really another if test so which
runs only if this previous test fails.
So in this case it didn't fail here and this code didn't run.
So at this point in the chain, we know that the person is at least eight years old because the first
test.
Uh, is let's actually, uh, make here.
Now, in this case, we know the person is at least eight years old because the first test didn't run
right.
So if the person is under 18, an appropriate message is printed and Python will not execute the else
block.
Now, Python will now execute because our Elif here, our Elif is executed, but also our if didn't
execute because it didn't meet meet the conditions here because my age was ten.
And.
Here if my age under eight, then it didn't print because my age is not under eight.
You learned that in expressions previous lectures, and here we have Elif, which is my age, is under
18 and here we have ten here.
So in this example here, the if test, let's actually yes, here in this example, the if test evaluates
false so it's code block is not executed.
However, the Elif test evaluates true which is the ten is less than 18.
Right.
So its code is executed.
So the output is one sentence here informing the user that admission cost is $50.
So your ticket is $50.
Any age greater than 17 would cause the first the if and Elif tests to fail in this situations here,
let's actually make it 20 In this situations the else block will be executed here and as you can see
here else block is executed.
This block is in the if and Elif didn't execute because they didn't meet the condition here and here
we have the else block executed here.
And we printed this message here.
So rather than the printing the ticket price with the if else block, it would be more concise to just
set the price inside the if else chain.
And then we have a single print call that runs after the chain has been evaluated.
So now we will let's delete this and we will do another example with that.
So here, if my age is 20, remember that the.
For under a under age of eight, it will be free for bus ticket.
So my age will be or and we will also add price here.
Price is going to.
Zero and my age eight while my age is other than 18.
Then the price is going to be price.
Price is going to be 25, the 50 and else if our age is greater than 18, we will praise we will use
the price 19 here.
And after that we will print here we will use format.
Your bus ticket cost is here and we will use this price.
That's it.
So here, let's run this.
And here we are.
Our age is 20 and our else block is executed here.
So the indented lines here you see here, these are the indented lines set the value of price according
to the person's age, as in the previous example.
So after the price is set by the if here, if Elif and else conditions as separate on indented print
call uses this value the price value.
Um, two.
So price value.
To display this message, right?
And so this here, this code produces the same output as the previous example.
But the purpose of the if Elif else chain is narrower.
So instead of determining a price and displaying a message, it simply determines the ticket price.
And in addition to being more efficient, the reveals this code here is easier to modify than the original
approach that we did in earlier in this lecture.
So to change the text of the ultimate message, you will need to change only this print call rather
than three separate print calls that was here before.