So for now, you have seen that all the instructions you write in a Python program will be executed
one by one.
But as you progress, you will want to make your programs more dynamic, and you will want to be able
to execute only some instructions depending on some specific conditions.
For example, you might do a certain action if a button is pressed, and if it's not pressed, then
you will do another action okay.
So in your code some instructions might be executed and some other instructions might not be executed.
And let's write our first condition in Python I will start by creating a variable.
Let's let's name it temperature is equal to for example 21.
So even though float numbers would be better for temperatures, I'm just going to use an integer here
to keep it simple for this example okay.
So we have our variable.
And I'm going to write a condition here how to write a condition.
Well you have the if keyword.
As you can see we have the syntax Highlighting on.
If so, in Python, you start with if then we are going to test a condition and this condition is going
to be evaluated to true or false.
So let me write something first and then I will explain it to you.
So if temperature I use this angle bracket like this which means greater than let's say 20.
Then I add a colon and I go back to a new line.
And you can see we have a new indentation of four spaces.
So what does that mean?
What will the if keyword we enter a condition.
This here is going to be evaluated to true or to false.
If this is evaluated to true then well you can see we end.
If with a colon then we are going to execute whatever code is here after the if with an indentation.
But if it is false, we are not going to execute this block of code here.
Okay.
So here we test if the temperature is greater than 20 and if the temperature is greater than 20, I'm
going to write print.
For example, it's warm.
Okay.
Here I'm using the metric system.
I use the Celsius degrees okay.
But it's not really going to be a problem for comprehension of conditions.
Okay.
So we have written our first condition.
Now let's test it.
And you can see we have it's warm.
So what happened is that when we first create the variable temperature we assign a value to it.
21.
Then we test if temperature greater.
And actually this is strictly greater.
And I'm going to come back to conditional operators in the next lesson.
Okay.
For now we just focus on the condition itself.
So if the temperature is strictly greater than 20 which means at least 21, then we print.
It's warm because it's 21.
Then we print it warm okay.
If I put 41 we are also going to print.
It's warm.
Now let's say I put 18 degrees.
Let's run the script and you can see that we don't print anything.
So this line is only executed if this condition here is true.
All right.
So that's already making our program more dynamic.
I'm going to add a new line after that.
So you can see that after I press enter I'm still in this in notation.
So I'm still inside the if and I can add more stuff inside the if.
Okay.
If I go back here I am back to the first indentation.
So we are out of the if structure.
I'm just going to add a space for readability.
And let's print for example end of program.
So let's print that.
You can see we have end of program.
So even though we didn't execute that line because it's in the if and this is false, we still execute
this because we are after the if and this is outside of the if.
Now if I for example, add an indentation here, you can see we will still be in the.
If so we will not print end of program.
Okay.
And in this case it will only be printed if for example I put 25.
then.
End of program is going to be printed because it's inside the if.
Okay.
So pay attention to the indentation.
It's very important.
Great.
So a condition you can see it's not very complicated.
If and then condition to test colon and then a block of code to execute if it is true.
Now you can add after the.
If so you go back to the same indentation as the if you can add an else keyword.
So you put else colon.
Go back to a new line.
You have an indentation.
And let's say for example print not so one.
What does the else do.
Well if this condition is true you're going to execute this block of code and that's it.
But if this condition is false you will execute what's inside the else keyword the else block here.
Okay.
So if the temperature is 25 you can just print it's one.
And you can see this one was not executed because we only execute it It's warm, but if I put 15 in
this case, you will see we have not so warm.
Okay, so what happened here is that we go to the.
If we evaluate this condition this is going to be false.
So if this is false we go to the else block and we execute the else block.
And then of course we also execute end of program because it's out of the if and on top of if and else.
You can also add in between if you want to else if and actually in Python it's not else if it's been
condensed into Elif okay.
So Elif means else.
If so you have the if Elif and else keywords.
With Elif, you can test another condition.
So, for example, if temperature is greater than 20, it's one.
But then you can test.
So if this is false, we're going to test if temperature is greater than ten.
And you can see we have a new edition.
Let's print.
Well, let's print.
Not so warm here.
Then I'm going to go back to a new line.
And you can see I removed the indentation and I can add another Elif.
Okay, I can add as many Elif as I want.
Let's say temperature greater than zero.
Print.
It's cold and I'm going to change the else to.
It's freezing, so it's freezing.
So what's happening now?
Well, let's make a test with 15, actually, and let's see what's happening and understand this flow
here.
So we have not so warm.
So what happened.
We have this entire if structure.
We start at the first.
If we check if this is true then we're going to execute.
It's warm and that's it.
We don't execute any of the other.
We don't even test the other else if other else okay.
But in this case with temperature 15 well this is false.
So if this is false we're going to check.
Do we have another leaf?
Yes.
So we're going to test this one if temperature is greater than ten.
And this is true.
So we are going to print not so warm.
And then we go out of the if we don't even test the next ones.
So now I can make another example with five.
And you can see now we have it's cold because this condition was false.
So then we try the next one.
This one is false.
We try the next one.
This one is true.
So we print it's cold.
And finally let's say minus five.
Then we're going to have it's freezing okay.
Because this one is going to be false.
And then this one is going to be false.
This one is going to be false.
Finally we go to the else.
All right.
So if you have any temperature that's greater than 20 we're going to get here.
But then if the temperature is greater than ten here actually you know that temperature is greater than
ten, but also that the temperature is lower than 20.
Because if it was greater than 20, you would have stayed here.
Okay, so when you arrive in this leaf, actually, you know that the temperature is kind of between
10 and 20.
Same thing here.
You know, the temperature is going to be between 0 and 10, because if it was greater than ten you
would be in this block okay.
And finally with the else you know that it's freezing because well, negative temperature is freezing.
And you know that if the temperature is not greater than zero, then it's freezing.
So you can see with this the order of the conditions is quite important.
And you can create if blocks as simple as just one, if you can add as many Elif as you want and only
one else if you want to add an else.
But everything is optional.
Okay, you can just the minimum code for a if structure for condition is basically this okay if condition
colon and then a block of code with one line or as many lines as you want, and then you just need to
go out of the indentation here to go back to the main execution of the code that will be line by line
once again.