Let's talk about Python's version of the ternary operator.
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.
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
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 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.
Sign up for my free 5 day email course and learn essential concepts that introductory courses often overlook: iterables, callables, pointers, duck typing, and namespaces.
Sign up for my 5 day email course and learn essential concepts that introductory courses often overlook!
Sign in to your Python Morsels account to track your progress.
Don't have an account yet? Sign up here.
Sign up for my free 5 day email course and learn essential concepts that introductory courses often overlook: iterables, callables, pointers, duck typing, and namespaces. Learn to avoid beginner pitfalls, in less than a week!
Ready to level up? Sign up now to begin your Python journey the right way!