VOOZH about

URL: https://thenewstack.io/how-to-use-python-if-else-statements/

⇱ How to Use Python If-Else Statements - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2023-11-30 05:00:45
How to Use Python If-Else Statements
Python / Software Development

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 Jack Wallen
👁 Featued image for: How to Use Python If-Else Statements
Featured image via Unsplash.
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
👁 Image

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:
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
Paste the above script into the file and save/close the new file. Run the new Python app with:
python3 ifelse.py
The output will be:
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:
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.py
Paste the above script and then save the file. Run the script with the command:
python3 ifelse2.py
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:
i is less than 10
If the input is more than 10 (else), the output will be:
i does not meet the condition
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:
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.py
Paste the script and save/close the new file. Run the script with:
python3 ride.py
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.
TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: Statement.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.