Python's ternary operator

Share
Copied to clipboard.
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
2 min. read Watch as video Python 3.8—3.12
Python Morsels
Watch as video
01:57

Let's talk about Python's version of the ternary operator.

Python doesn't have typical ternary operators

Here we have an if-else statement:

if amount == 1:
    noun = "item"
else:
    noun = "items"

Many programming languages allow you to take code like this, that assigns a variable to one of two values based on a condition, and turn that into a single line of code:

noun = amount == 1 ? "item" : "items"

That strange ?...: syntax is often called a ternary operator.

Python doesn't support that syntax: that isn't valid Python code. Instead, we have something called a conditional expression.

Python's conditional expressions (a.k.a. inline if)

Python's conditional expression looks like this:

noun = "item" if amount == 1 else "items"

Though, we often call this an inline if because it looks sort of like an if-else statement all in one line of code. You can think of this as Python's version of a ternary operator.

Notice that in a conditional expression, the condition is in the middle. That's actually the first part of the expression that Python runs. If the condition returns something truthy, then the expression at the beginning (before the if) is evaluated. Otherwise, the expression that's at the end (after the else) is evaluated.

So in our case, noun will be set to "items" if amount is not equal to 1:

>>> amount = 2
>>> noun = "item" if amount == 1 else "items"
>>> print(f"{amount} {noun}")
2 items

But if amount is equal to 1, then noun will be set to "item" instead:

>>> amount = 1
>>> noun = "item" if amount == 1 else "items"
>>> print(f"{amount} {noun}")
1 item

Readability of conditional expressions

I find short conditional expressions pretty readable:

noun = "item" if amount == 1 else "items"

But long conditional expressions can be pretty hard to read sometimes.

Here we have a relatively long conditional expression:

key = data["id"] if data["type"] != "customer" else data["customer_id"]

I usually prefer to break up a long conditional expression over multiple lines of code by using parentheses, so that my human brain can parse the different parts of the conditional expression more easily (see breaking up long lines of code in Python for more on that syntax):

key = (
    data["id"]
    if data["type"] != "customer"
    else data["customer_id"]
)

Though if you're writing a very complex conditional expression, you might ask yourself whether a regular if-else statement might be more readable instead:

if data["type"] != "customer":
    key = data["id"]
else:
    key = data["customer_id"]

I do have a slightly higher affinity for Python's very of the ternary operator than the ?...: ternary operator in most languages, mostly because Python's ternary operator is fairly readable even for developers who have never encountered it before.

Python's ternary-equivalent is inside-out

Python doesn't have the ?:-style ternary operator that many programming languages do.

Instead, we have conditional expressions which read about a little bit more like English and look kind of like an if-else statement all on one line of code.

Series: Conditionals

Conditionals statements (if statements) are useful for making a branch in our Python code. If a particular condition is met, we run one block of code, and if not then we run another block.

To track your progress on this Python Morsels topic trail, sign in or sign up.

0%
Concepts Beyond Intro to Python

Intro to Python courses often skip over some fundamental Python concepts.

Sign up below and I'll share ideas new Pythonistas often overlook.

Python Morsels
Watch as video
01:57