Conditionals

The if, else, and elif statements control conditional code execution. The general format of a conditional statement is as follows:

if expression: 
    statements 
elif expression: 
    statements 
elif expression: 

    statements 
... 
else: 
    statements
				

If no action is to be taken, you can omit both the else and elif clauses of a conditional. Use the pass statement if no statements exist for a particular clause:

if expression: 
    pass            # Do nothing 
else: 
    statements
				

Get Python Essential Reference, Second Edition 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.