Python’s ternary operator is a one-line conditional expression. It returns one value when a condition is true and another value when the condition is false:
value_if_true if condition else value_if_false
Python’s official reference calls this a conditional expression. Many developers still say ternary operator—both names refer to the same syntax.
Tested on: Python 3.13.3; kernel 6.14.0-37-generic.
Python ternary operator quick reference
| Task | Example |
|---|---|
| Basic syntax | value_if_true if condition else value_if_false |
| Assign value conditionally | status = "adult" if age >= 18 else "minor" |
| Return value from function | return "even" if n % 2 == 0 else "odd" |
| Use in f-string value | label = "pass" if score >= 40 else "fail" |
| Use in list comprehension | ["even" if n % 2 == 0 else "odd" for n in numbers] |
| Use nested ternary | "positive" if n > 0 else "zero" if n == 0 else "negative" |
| Prefer normal if-else | When logic is long, nested, or has multiple actions |
Python ternary operator syntax
Syntax:
value_if_true if condition else value_if_falseHow it works:
- Python evaluates
conditionfirst. - If the condition is true, it evaluates and returns
value_if_true. - If the condition is false, it evaluates and returns
value_if_false. - The
elsepart is required—you cannot omit it.
number = 8
result = "even" if number % 2 == 0 else "odd"
print(result)The variable result becomes "even".
Python ternary operator example
Start with a normal if-else, then rewrite it as a ternary expression.
if-else:
age = 20
if age >= 18:
status = "adult"
else:
status = "minor"
print(status)Equivalent ternary:
age = 20
status = "adult" if age >= 18 else "minor"
print(status)Both assign "adult". The ternary version fits on one line because you are choosing between two values.
def parity(n):
return "even" if n % 2 == 0 else "odd"
age = 20
status = "adult" if age >= 18 else "minor"
score = 35
label = "pass" if score >= 40 else "fail"
numbers = [1, 2, 3, 4, 5]
labels = ["even" if n % 2 == 0 else "odd" for n in numbers]
print(parity(7))
print(status)
print(label)
print(labels)
print(max(10, 25) if 10 < 25 else 10)The output is odd, adult, fail, a list of even/odd labels, and 25.
Ternary operator vs if-else in Python
| Feature | Ternary operator | if-else statement |
|---|---|---|
| Main use | Choose between two values | Run one or more statements |
| Length | One expression | Multiple lines |
| Returns a value | Yes | Not directly as an expression |
| Best for | Simple conditional assignment | Complex logic |
| Readability | Good for short cases | Better for long conditions |
Use the ternary operator for simple value selection. Use normal if / else when the logic needs multiple steps, side effects, or becomes hard to read. For multi-branch choices, see Python switch-case patterns and if-elif-else.
How the Python ternary operator is evaluated
Python checks the condition first. Only the selected branch is evaluated—the other branch is skipped. That short-circuit behavior can prevent errors:
x = 10
y = 0
result = x / y if y != 0 else "cannot divide by zero"
print(result)When y is 0, Python never evaluates x / y, so no ZeroDivisionError occurs.
This is different from putting both branches in a tuple trick or calling both sides eagerly—stick to the official if condition else form for clarity.
Use ternary operator in return statements
Small helper functions often return a ternary result directly:
def parity(n):
return "even" if n % 2 == 0 else "odd"
print(parity(4))
print(parity(9))Keep the condition simple so the return line stays readable.
Use ternary operator with print() and function arguments
A ternary expression can be passed as a function argument when you need to pick a message or value inline:
score = 88
print("pass" if score >= 40 else "fail")
a, b = 3, 9
print(max(a, b) if a < b else a)Avoid long or nested ternaries inside function calls—they become hard to scan. Extract a variable or use if-else when the logic grows.
Use ternary operator in list comprehension
Place the ternary before the for part—the same shape appears in a for loop over any iterable:
numbers = [1, 2, 3, 4, 5]
labels = ["even" if n % 2 == 0 else "odd" for n in numbers]
print(labels)Important: x if condition else y chooses a value for each item. A trailing if filters items:
numbers = [1, 2, 3, 4, 5]
# Choose a value per item
mapped = ["even" if n % 2 == 0 else "odd" for n in numbers]
# Keep only even numbers
evens = [n for n in numbers if n % 2 == 0]
print(mapped)
print(evens)mapped is ['odd', 'even', 'odd', 'even', 'odd']; evens is [2, 4]. See list comprehension examples for more patterns.
Nested ternary operator in Python
Python allows nested conditional expressions:
n = 0
sign = "positive" if n > 0 else "zero" if n == 0 else "negative"
print(sign)Reading order: evaluate left to right as nested conditions. Works for three-way labels, but readability drops fast.
If you need more than one nested condition, prefer normal if-elif-else:
n = -4
if n > 0:
sign = "positive"
elif n == 0:
sign = "zero"
else:
sign = "negative"
print(sign)Python ternary operator with None/default values
Ternary expressions are handy for default labels when a value may be empty:
username = ""
display_name = username if username else "Guest"
print(display_name)This relies on Python’s truthiness: empty strings are falsy, so "Guest" is chosen. Be explicit (username if username is not None else "Guest") when 0 or False are valid values you must keep.
Python ternary operator with lambda
Lambda functions may contain only a single expression, so ternary syntax fits well:
parity = lambda n: "even" if n % 2 == 0 else "odd"
print(parity(6))
print(parity(3))Keep lambdas short. Complex lambda + nested ternary logic is usually clearer as a normal def with if-else.
Common mistakes to avoid
- Using C/Java syntax —
condition ? true_value : false_valueis invalid in Python. - Forgetting
else—x if conditionalone is a syntax error. - Reversing the order — the true value comes before
if, not after the condition in? :style. - Using ternary for multiple actions — you cannot run two statements; use
if-elsefor side effects. - Overusing nested ternary — prefer
if-elif-elsewhen readability suffers. - Mixing unrelated types —
5 if ok else "five"can confuse later code; keep branches consistent when possible. - Choosing ternary only for brevity — shorter is not better if teammates struggle to read it.
Summary
Python’s ternary operator syntax is value_if_true if condition else value_if_false. It is officially a conditional expression and works well for simple conditional values in assignments, returns, function arguments, list comprehensions, and small lambdas. Use normal if-else for complex logic. Nested ternary expressions are allowed but should stay rare.
Useful references

