Conditional Expressions

Choosing between two values in a single expression, and the point at which the one-liner stops being clearer than the four lines.

Overview

The order takes a moment

verdict = "pass" if score >= 50 else "fail"

The value comes first, then the condition, then the alternative. Most languages put the condition first, so the shape reads backwards at first. It becomes natural quickly, and the reason for it is that the common case reads as a sentence: "pass, if the score is at least fifty, otherwise fail".

ternary.py

ternary.py Python 3
Output

                    

ternary_limits.py

ternary_limits.py Python 3
Output

                    

Worth knowing

Order is value, condition, other value: a if cond else b.
It is an expression, so it works inside f-strings, comprehensions and argument lists.
The else is mandatory - an expression must produce a value on every path.
x or default looks similar and is not: it also replaces 0 and empty strings.

Conditional Expressions: A Practical Guide

A conditional expression picks between two values in one line. It is not a shorter if statement — it is an expression, which means it produces a value and can appear anywhere a value can.

Where it earns its place

Because it is an expression, it fits where a statement cannot:

f"You {'passed' if ok else 'failed'}"
["even" if n % 2 == 0 else "odd" for n in nums]
func(timeout if timeout else 30)

Inside an f-string, inside a comprehension, as an argument. A four-line if cannot go in any of those places, so the choice is not style — it is the only form that fits.

The else is not optional

An expression has to produce a value on every path, so there is no one-armed version. x = 1 if cond is a syntax error. If you want "set it only sometimes", that is a statement, and the statement form is what you want.

Where it stops helping

Chaining is legal:

"A" if s >= 90 else "B" if s >= 80 else "C" if s >= 70 else "F"

and it is the point where the form has outlived its usefulness. The reader has to scan to the end to find the default, and inserting a new band means editing the middle of a long line. The page prints the chained version beside a plain sequence of if statements; they agree on every input, and only one of them can be read at a glance.

The rule that holds up: one condition, short values, one line. Two conditions, write the statement.

The `or` lookalike

This is the most common way the idea goes wrong:

return value or "default"

It looks like "use the default when value is missing", and it is really "use the default when value is falsy" — which includes 0, "", [] and False. If 0 is a legitimate value, or silently discards it.

return value if value is not None else "default"

says what was meant. The page runs both over "set", "", 0 and None so the divergence is visible rather than theoretical.

Check yourself

0 of 3

Answer without scrolling back up.

  1. What does `x = 5 if False` do?

  2. `value or 'default'` differs from a conditional expression how?

  3. Why can a conditional expression go inside an f-string?

Cheat sheet

Conditional Expressions

A conditional expression picks between two values in one line. It is not a shorter if statement — it is an expression, which means it produces a value and can appear anywhere a value can.

PYTHON · vizlearn.in/python/conditional_expressions.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.