Often you will want to take one action when a conditional test passes and different action in other
cases.
Python's if else syntax makes this possible.
The if else block is similar to a simple if statement, but the else statement allows you to define
an action or a set of actions that are executed and run when the conditional test fails.
When the if test fails, and in this case, we will execute else.
So we will display some message here.
Now let's let's write something that, for example, we have a car, a car here.
And of sexual stroke here.
And we don't this we don't want this car to, for example, older than ten years.
Right.
So let's make this.
An.
Years.
So we want this car to less than ten years.
So we will want a new car, Right.
And that's why we're going to do here that let's make, for example, car model.
Car model here.
It's in this case, let's make it BMW and car here.
In this case, let's make it.
In 2015.
And here we will add if our.
Car model.
So car year -2023in this case.
Of course, today's year is 2023.
And actually, let's delete this for now.
So delete this.
And now.
Career -2020 23.
Is.
So our career, for example, in this case, our career is going to be eight, while eight is less than
ten, then it will evaluate three.
Right?
So in this case, print your car is.
And we will also use format here.
Format your your car is a car here.
The -273 years old and it's almost new.
Right.
And here we will also add else statement.
If our car is has age of more than ten, we will write this here else print.
Sorry.
I don't want or sorry.
Our company.
Doesn't.
Buys cars.
Buy cars.
That more that.
Age is more than ten.
And after I will print again and your your cars.
Your car.
Age is.
Of course we need to add F here format per year.
Per year -2023.
Your car or let's actually instead of car we can also write BMW.
Right.
So or car model.
Car model is years old and it's almost new.
And we can buy it.
And here also our company doesn't buy BMW.
Let's actually add another format here.
So we will add F here and format.
Car.
Model that age is more than ten and career here.
Now let's run this.
And as you can see here, your BMW oops.
Actually, we need to do this opposite here.
Sorry for this.
We got right numbers, but we got negative here, so we need to do this.
We need to subtract our career from 2023.
Let's do the same here.
And now.
Your BMW is eight years old and it's almost new.
We can buy it, but in LCR.
A else comment here is didn't executed because if here evaluated true.
If we make our car here for example 2005 then it will give your BMW is 18 years old and it almost no
car here.
Of course we need to change it again.
2023 minus car year.
And here we will get else here.
Sorry, our company doesn't buy that buy BMW.
That age is more than ten and your car age is 18.
So.
First in first example, actually, let's try again 2015.
So in this example, our if conditional test passes and the first block of this print here call is executed.
So if the test evaluates to false, then the else block is executed, for example, 2005.
And in this case, our false will not be executed because it is here because.
It is false, right?
So it will not execute.
Instead, our LS command here will execute here.
So this code works because it has only two possible situations to evaluate.
A car is either old enough or not.
Right.
So the if else structures works well in situations in which you want Python to always execute one of
possible situations or actions.
So in simple if else chain like this, one of two actions will always be executed.
No matter if our car here is 1000 or so here, as you can see here.
So our company owned by BMW age is more than ten.