2.11. if Statement

The standard if conditional statement follows this syntax:

						if
						expression:
						if_suite
					

If the expression is non-zero or true, then the statement suite is executed; otherwise, execution continues on the first statement after. “Suite” is the term used in Python to refer to a sub-block of code and can consist of single or multiple statements.

>>> if counter > 5:
…       print 'stopping after 5 iterations'
…       break
					

Python supports an else statement that is used with if in the following manner:

						if expression:
      if_suite
						else:
						else_suite
					

Python has an “else-if” statement named elif which has the following syntax:

						if
						expression1:
						if_suite
						elif
						expression2:
						elif_suite
						else:
						else_suite
					

Another surprise: There is no switch or case statement ...

Get Core Python Programming 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.