The if-elif-else statement

Our central tool for conditional processing is the if statement. This is a compound statement which is built from a number of clauses. The initial clause starts with the if keyword. Any number of elif (short for "else if") clauses can be used. Each of these clauses has a conditional expression and an indented suite of statements. We can also add a single catch-all else clause at the end; this doesn't have a condition, but does have a suite of statements.

The minimal if statement, with a single clause, might look like this:

if abs(a-b) < ε:
    print("{a} \N{ALMOST EQUAL TO} {b}".format(a=a, b=b))

The if statement contains a single expression. If the expression is True, the suite of statements is executed. In this case, the ...

Get Python Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.