- Read Tutorial
- Watch Guide Video
- Complete the Exercise
In this guide, we're going to focus on the basic syntax for implementing conditionals. So I'm going to start off with just a single condition and so I'm going to create a variable here called age and set it equal to the number 25.
age = 25
And now the syntax for being able to build a conditional here is to say if and that is a reserved keyword in Python so I'm gonna say if age is less than 25 and then I follow this up with a colon and then when I come to a new line I need to have an indentation and we've already talked about this for our for-in loops and are while loops and it's a good rule of thumb in Python. Anytime you see a colon the next line you're going to have to have indented and so now that we've implemented our basic conditional.
Now we can say what we want to happen if this condition is true. So, in this case, I'm just going to print something out and I'm going to say f, and then for this one, we don't really need formatting but I'm going to show you later on how we're going to use it. So I'm gonna say I'm sorry you need to be at least 25 years old and that is going to be everything that we need in order to get this working. So if I hit run you can see nothing happens.
So, why is that? Well, it's because this condition right here was not true. Our age was set at twenty-five. Now if we change age and we say 15 and now we run it you can see that it now prints out exactly like how we typed. So it says I'm sorry you need to be at least 25 years old.
This can work for any number, so if I say 1500 years old and run this you can see nothing is going to be printed out because this condition is not true.
So when it comes to understanding conditional logic you just have to think of setting up your conditions and assuming they're going to be either true or false. So, in this case, it is false that age is less than 25 and so this is one of the most basic kinds of conditions that you can implement.
Now let's see how we can add a little bit more dynamic behavior to this by adding a second condition. So right now we're saying if age is less than 25 then do this but as we saw whenever we have a point in time where this is not true then nothing happens in the program and there are times where you may want to implement a single condition like this. However, a very common practice is to also have an else condition. So what this means is if this condition here is true then it's going to run the code inside of this code block. But if this condition is false then it is going to come down here into this else code block and it's going to do whatever we have in here.
And so I'm going to just copy this and I'm going to keep the print statement and the format and then I'm just going to replace everything inside of it and so I'm going to say something like "You're good to go, and then inside of this I'll call age inside our curly brackets and say fits in the range to rent a car and so that is 25 years old in the US is the age where you can rent a car and so this is what our program is testing for.
age = 1500 if age < 25: print(f"I'm sorry, you need to be at least 25 years old") else: print(f"You're good to go, {age} fits in the range to rent a car.")
So now if I run this you can see that now even though it's not true. The age is less than 25 because we have this else statement. Now we're going to be guaranteed that we're always going to get some kind of print out. We're either going to have an I'm sorry you need to be at least 25 years old or what we have in this case where it says "You're good to go, 1500 years old fits in the range to rent a car."
So this is an if-else statement. So this is where you set up one condition and if it's true it runs and if not then anything else is going to run inside of this else block. And so this takes care of two out of the three basic conditional statements that you're usually going to build.
Now what we're going to do is implement another construct which is called the else if and it has a little bit of a weird name in Python it's called elif
but it's short for else if and what it does is it allows us to chain on additional conditions. So, for example, say that you are building this program out as an age verification tool for a rental company and the rules are that someone has to be at least 25 years or older and they cannot be over a hundred years old. So someone who's 1500 years old like we have right here should not be able to rent a car.
So let's come down and I'm going to keep our else block here. But what I'm going to do is I'm going to add an else if block and it is spelled out like this elif
and then I'll say if age is greater than a hundred then I'm going to simply take this and I'm going to just add a format here and say something like "I'm sorry and then curly braces whatever this age is, is too old to rent a car." Now if we run this we should fall into the second category. Let's hit run and that works.
I'm sorry, 1500 is too old to rent a car. If we change this up and we put something in the middle so we say 50 years old and run this. "You're good to go."
So as you can see we are checking for two conditions. So this is looking to see first if this first condition is true and if not it looks at the second condition. And then finally if both of these are false then it goes to the final statement here where it just says OK these items are both false. We're going to do whatever is inside of this code block.
In review, that is how you can implement three types of conditions inside a python.
Code
# Single condition age = 25 if age < 25: print(f"I'm sorry, you need to be at least 25 years old") # if/else age = 55 if age < 25: print(f"I'm sorry, {age} is under 25 years old") else: print(f"You're good to go, {age} fits in the range to rent a car") # if/elif/else age = 55 if age < 25: print(f"I'm sorry, {age} is under 25 years old") elif age > 100: print(f"I'm sorry, {age} is over 100 years old") else: print(f"You're good to go, {age} fits in the range to rent a car")