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

The if statement

The if statement is used for conditional execution:


if expression :
[elif expression :
    suite ]…
    suite ]

It selects exactly one of the suites by evaluating the expressions one by one until one is found to be true (see below); then that suite is executed (and no other part of the if statement is executed or evaluated). If all expressions are false, the suite of the else clause, if present, is executed.

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.

The if clause

The if construct is also used, together with the for clause, in list comprehensions and generator expressions.


if expression

The if operator

The if construct is also used, together with an else clause, as an operator:


expression if expression else expression

See operator-if.