Python has various kinds of simple statements, such as assignments and print statements, and several kinds of compound statements, like if and for statements. Formally, function definitions and import statements are also statements, and there are no restrictions on the ordering of statements or their nesting: import may be used inside a function, functions may be defined conditionally using an if statement, etc. The effect of a declaration-like statement takes place only when it is executed.
All statements except assignments and expression statements begin with a keyword: this makes the language easy to parse. An overview of the most common statement forms in Python follows.
An assignment has the general form
variable = variable = ... = variable = expression
It assigns the value of the expression to all listed variables. (As shown in the section on tuples, variables and expressions can in fact be comma-separated lists.) The assignment operator is not an expression operator; there are no horrible things in Python like
while (p = p->next) { ... }Expression syntax is mostly straightforward and will not be explained in detail here.
An expression statement is just an expression on a line by itself. This writes the value of the expression to standard output, in a suitably unambiguous way, unless it is a `procedure call' (a function call that returns no value). Writing the value is useful when Python is used in `calculator mode', and reminds the programmer not to ignore function results.
The if statement allows conditional execution. It has optional elif and else parts; a construct like if...elif...elif...elif...else can be used to compensate for the absence of a switch or case statement.
Looping is done with while and for statements. The latter (demonstrated in the `ls' example earlier) iterates over the elements of a `sequence' (see the discussion of data types below). It is possible to terminate a loop with a break statement or to start the next iteration with continue. Both looping statements have an optional else clause which is executed after the loop is terminated normally, but skipped when it is terminated by break. This can be handy for searches, to handle the case that the item is not found.
Python's exception mechanism is modelled after that of Modula-3.
Exceptions are raised by the interpreter when an illegal operation is
tried. It is also possible to explicitly raise an exception with the
raise statement:
raise expression, expression
The first expression identifies which exception should be raised; there are several built-in exceptions and the user may define additional ones. The second, optional expression is passed to the handler, e.g. as a detailed error message.
Exceptions may be handled (caught) with the try statement, which
has the following general form:
try: block |
except expression, variable: block |
except expression, variable: block |
... |
except: block |
When an exception is raised during execution of the first block, a search for an exception handler starts. The first except clause whose expression matches the exception is executed. The expression may specify a list of exceptions to match against. A handler without an expression serves as a `catch-all'. If there is no match, the search for a handler continues with outer try statements; if no match is found on the entire invocation stack, an error message and stack trace are printed, and the program is terminated (interactively, the interpreter returns to its main loop).
Note that the form of the except clauses encourages a style of programming whereby only selected exceptions are caught, passing unanticipated exceptions on to the caller and ultimately to the user. This is preferable over a simpler `catch-all' error handling mechanism, where a simplistic handler intended to catch a single type of error like `file not found' can easily mask genuine programming errors — especially in a language like Python which relies strongly on run-time checking and allows the catching of almost any type of error.
Other common statement forms, which we have already encountered, are function definitions, import statements and print statements. There is also a del statement to delete one or more variables, a return statement to return from a function, and a global statement to allow assignments to global variables. Finally, the pass statement is a no-op.