Next / Previous / Contents / TCC Help System / NM Tech homepage

14.2. The if construct: choice

For conditional branching:

if E0:
    B0
elif E1:
    B1
elif ...
else:
    Bf

This is the most general form, and means: if expression E0 is true, execute block B0; otherwise, if E1 is true, execute block B1; and so forth. If all the conditions are false, execute Bf.

The elif clause is optional, and there can be any number of them. The else clause is also optional.

When evaluating whether an expression is true or false, false values include None, a number equal to zero, an empty sequence, or an empty dictionary. All other values are true.

Here's an example:

if x < 0:
    print "But it's negative!"
else:
    print "The square root of", x, "is", x**0.5