We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

operator-if

New in version 2.5.

The if operator provides an inline if-then-else expression, similar to the ternary ?: operator used in C and similar languages.

Syntax:

expression if expression else expression

The expression x if C else y first evaluates C (not x); if C is true, x is evaluated and its value is returned; otherwise, y is evaluated and its value is returned.

In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true.