TNS
VOXPOP
Where are you using WebAssembly?
Wasm promises to let developers build once and run anywhere. Are you using it yet?
At work, for production apps
0%
At work, but not for production apps
0%
I don’t use WebAssembly but expect to when the technology matures
0%
I have no plans to use WebAssembly
0%
No plans and I get mad whenever I see the buzzword
0%
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
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

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:


Create the Python script with the command:


Paste the above script into the file and save/close the new file.

Run the new Python app with:


The output will be:


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:


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:


Create the script with:


Paste the above script and then save the file.

Run the script with the command:


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:


If the input is more than 10 (else), the output will be:


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:


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:


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:


Create a new file with:


Paste the script and save/close the new file.

Run the script with:


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.

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.