How to Use Python If-Else Statements

You make decisions every day. When you wake, you decide what to eat for breakfast, which then might lead you to choose between coffee or orange juice. Which beverage you decide on could be predicated on whether you choose granola, oatmeal, or yogurt as your meal.
Those types of decisions carry on throughout the day. What to wear, which path to take to work, what application to use for what task, what to have for lunch, who to talk to… ad infinitum.
Here’s a perfect example of a real-life if-else statement:
If it’s raining, I will take an umbrella with me.
Simple.
Most programming languages have conditional statements that help guide the direction of how a program flow will continue. For Python, that conditional statement is if-else and it’s a very handy tool to have at your disposal. If-else statements should be considered a must-know to be a successful Python programmer.
Essentially, the if-else statement is used to execute the true and false portion of any given condition. Continuing with my example above, the true portion could be “I will take an umbrella” and the false portion could be “If it’s not raining.” In other words, if it’s raining I will take my umbrella else (if it’s not raining) I won’t.”
Now, how do you use if-else statements in Python?
Quite easily.
Let me show you.
How the If-Else Statement Works
In Python, the if-else statement is structured like so:
if condition:
# If the statement is true, execute this
The flow of the if-else statement looks like that shown in Figure 1.
Figure 1

The if-else statement as a flow chart.
When Python enters an if-else statement, it moves from the condition to the execution if the result of the condition is true, otherwise, it skips the execution and exits the statement.
A Simple Example
Let’s create an if-else statement that will print a message based on the statement. Here’s what this sample application will do:
If i is less than 10, it will print out “i is less than 10”, otherwise it will print out “i does not meet the condition”. Simple enough.
Because the if-else statement is built into Python, we don’t have to worry about importing any libraries, so we can go straight to the script itself. Our example script will look like this:
1 2 3 4 5 |
i = 10 if (i > 10): print("i is less than 10") print("i does not meet the condition") |
Create the Python script with the command:
1 |
nano ifelse.py |
Paste the above script into the file and save/close the new file.
Run the new Python app with:
1 |
python3 ifelse.py |
The output will be:
1 |
i does not meet the condition |
Let’s make this a bit more useable. What we’ll do is allow input from the user. For this, we’ll define i using the float() method, which returns a floating point number from a string. This is required because users could input a decimal point value. Without float(), should a user input a decimal, the app would fail.
The float() line looks like this:
1 |
<em>i = float(input ("Enter a number: "))</em> |
Next, we’re going to employ the full if-else statement, so we’ll first have an if statement and then an else statement, each of which will use the print() function to output a different string of text. The full script looks like this:
1 2 3 4 5 6 |
<em>i = float(input ("Enter a number: ")) if i < 10: print("i is less than 10") else: print("i does not meet the condition")</em> |
Create the script with:
1 |
<em>nano ifelse2.py</em> |
Paste the above script and then save the file.
Run the script with the command:
1 |
<em>python3 ifelse2.py</em> |
You will be required to input a number. So long as the number (even with a decimal point value) is less than 10, the output will be:
1 |
<em>i is less than 10</em> |
If the input is more than 10 (else), the output will be:
1 |
<em>i does not meet the condition</em> |
Let’s create a script with a compound test. Our test will be for age and height to meet the requirements to ride a roller coaster (which requires riders to be over 12 years old and be at least 48 inches tall).
First, we’ll define the age and height variables like so:
1 2 |
<em>age = int(input("Enter your age: ")) height = int(input("Enter your height in inches: "))</em> |
Next, comes the if-else statement that will use our variables. The trick is that our if statement will use a compound test which looks like this:
1 |
<em>if age > 12 and height > 48:</em> |
After the if statement, we use the print() function and print out Enjoy the ride. Our else statement will have the sad job of printing out Sorry, but you cannot enjoy this ride.
The full script looks like this:
1 2 3 4 5 6 7 |
<em>age = int(input("Enter your age: ")) height = int(input("Enter your height in inches: ")) if age > 12 and height > 48: print("Enjoy the ride.") else: print("Sorry, but you cannot enjoy this ride.")</em> |
Create a new file with:
1 |
<em>nano ride.py</em> |
Paste the script and save/close the new file.
Run the script with:
1 |
<em>python3 ride.py</em> |
Enter your age and then enter your height. If you meet both requirements, you’ll see Enjoy the ride, otherwise (else) you’ll see Sorry, but you cannot enjoy this ride.
If you feel you’ve mastered Python, this is the end of your studies.
Else… come back next week for more Python goodness.