Python's ternary operator

Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
2 min. read Watch as video Python 3.7—3.11
Copied to clipboard.
Python Morsels
Watch as video

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"
    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.

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 = (
    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"]
    key = data["customer_id"]

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.

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