How to Use Python If-Else Statements
This tutorial delves into how to use if-else statements in Python.
Nov 30th, 2023 5:00am by

Featured image via Unsplash.
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.
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: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:
nano ifelse.py
python3 ifelse.pyThe output will be:
i does not meet the conditionLet’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:
i = float(input ("Enter a number: "))
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:
i = float(input ("Enter a number: "))
if i < 10:
print("i is less than 10")
else:
print("i does not meet the condition")
Create the script with:
nano ifelse2.pyPaste the above script and then save the file. Run the script with the command:
python3 ifelse2.pyYou 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:
i is less than 10If the input is more than 10 (else), the output will be:
i does not meet the conditionLet’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:
age = int(input("Enter your age: "))
height = int(input("Enter your height in inches: "))
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:
if age > 12 and height > 48: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:
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.")
Create a new file with:
nano ride.pyPaste the script and save/close the new file. Run the script with:
python3 ride.pyEnter 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.
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.
