In this tutorial, you’ll learn how to create inline if statements in Python. This is often known as the Python ternary operator, which allows you to execute conditional if statements in a single line, allowing statements to take up less space and often be written in my easy-to-understand syntax! Let’s take a look at what you’ll learn.
The Quick Answer: Use the Python Ternary Operator
Table of Contents
What is the Python Ternary Operator?
A ternary operator is an inline statement that evaluates a condition and returns one of two outputs. It’s an operator that’s often used in many programming languages, including Python, as well as math. The Python ternary operator has been around since Python 2.5, despite being delayed multiple times.
The syntax of the Python ternary operator is a little different than that of other languages. Let’s take a look at what it looks like:
result = x if a > b else y
Now let’s take a look at how you can actually write an inline if statement in Python.
How Do you Write an Inline If Statement in Python?
Before we dive into writing an inline if statement in Python, let’s take a look at how if statements actually work in Python. With an if statement you must include an
if, but you can also choose to include an
else statement, as well as one more of else-ifs, which in Python are written as
The traditional Python if statement looks like this:
x = True if x is True: y=10 else: y=20 print(y) # Returns 10
This can be a little cumbersome to write, especially if you conditions are very simple. Because of this, inline if statements in Python can be really helpful to help you write your code faster.
Let’s take a look at how we can accomplish this in Python:
x = True y = 10 if x else 20 print(y) # Returns 10
This is significantly easier to write. Let’s break this down a little bit:
- We assign a value to
x, which will be evaluated
- We declare a variable,
y, which we assign to the value of 10, if
xis True. Otherwise, we assign it a value of 20.
We can see how this is written out in a much more plain language than a for-loop that may require multiple lines, thereby wasting space.
Tip! This is quite similar to how you’d written a list comprehension. If you want to learn more about Python List Comprehensions, check out my in-depth tutorial here. If you want to learn more about Python for-loops, check out my in-depth guide here.
Now that you know how to write a basic inline if statement in Python, let’s see how you can simplify it even further by omitting the
How To Write an Inline If Statement Without an Else Statement
Now that you know how to write an inline if statement in Python with an
else clause, let’s take a look at how we can do this in Python.
Before we do this, let’s see how we can do this with a traditional if statement in Python
x = True if x is True: y=10 print(y) # Returns 10
You can see that this still requires you to write two lines. But we know better – we can easily cut this down to a single line. Let’s get started!
x = True if x: y = 10 print(y) # Returns 10
We can see here that really what this accomplishes is remove the line break between the
if line and the code it executes.
Now let’s take a look at how we can even include an
elif clause in our inline if statements in Python!
How to Write an Inline If Statement With an Elif Statement
Including an else-if, or
elif, in your Python inline if statement is a little less intuitive. But it’s definitely doable! So let’s get started. Let’s imagine we want to write this if-statement:
x = 3 if x == 1: y = 10 elif x == 2: y = 20 else: y = 30 print(y) # Returns 30
Let’s see how we can easily turn this into an inline if statement in Python:
x = 3 y = 10 if x == 1 else (20 if x == 20 else 30) print(y) # Returns 10
This is a bit different than what we’ve seen so far, so let’s break it down a bit:
- First, we evaluate is x == 1. If that’s true, the conditions end and y = 10.
- Otherwise, we create another condition in brackets
- First we check if x == 20, and if that’s true, then y = 20. Note that we did not repeated
- Finally, if neither of the other decisions are true, we assign 30 to y
This is definitely a bit more complex to read, so you may be better off creating a traditional if statement.
In this post, you learned how to create inline if statement in Python! You learned about the Python ternary operator and how it works. You also learned how to create inline if statements with else statements, without else statements, as well as with else if statements.
To learn more about Python ternary operators, check out the official documentation here.