Compound Statements
Compound statements contain (groups of) other statements; they affect or control the execution of those other statements in some way. In general, compound statements span multiple lines, although in simple incarnations a whole compound statement may be contained in one line. Theif, while and for statements implement traditional control flow constructs. try specifies exception handlers and/or cleanup code for a group of statements, while the with statement allows the execution of initialization and finalization code around a block of code. Function and class definitions are also syntactically compound statements.
A compound statement consists of one or more ‘clauses.’ A clause consists of a header and a ‘suite.’ The clause headers of a particular compound statement are all at the same indentation level. Each clause header begins with a uniquely identifying keyword and ends with a colon. A suite is a group of statements controlled by a clause.
The if Statement
The if statement is used for conditional execution:
else clause, if present, is executed:
The while Statement
The while statement is used for repeated execution as long as an expression is true:
else clause, if present, is executed and the loop terminates:
break statement executed in the first suite terminates the loop without executing the else clause’s suite. A continue statement executed in the first suite skips the rest of the suite and goes back to testing the expression.
The for Statement
The for statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object:
break statement executed in the first suite terminates the loop without executing the else clause’s suite. A continue statement executed in the first suite skips the rest of the suite and continues with the next item.
The try Statement
The try statement specifies exception handlers and/or cleanup code for a group of statements:
except Clause
The except clause(s) specify one or more exception handlers. When no exception occurs in the try clause, no exception handler is executed. When an exception occurs in the try suite, a search for an exception handler is started:
except clause with an expression, the expression must evaluate to an exception type or a tuple of exception types. Parentheses can be dropped if multiple exception types are provided and the as clause is not used:
except* Clause
The except* clause(s) specify one or more handlers for groups of exceptions (BaseExceptionGroup instances):
A
try statement can have either except or except* clauses, but not both.finally Clause
If finally is present, it specifies a ‘cleanup’ handler. The try clause is executed, including any except and else clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The finally clause is executed. If there is a saved exception it is re-raised at the end of the finally clause:
The with Statement
The with statement is used to wrap the execution of a block with methods defined by a context manager:
with statement proceeds as follows:
- The context expression is evaluated to obtain a context manager
- The context manager’s
__enter__()method is invoked - If a target was included, the return value from
__enter__()is assigned to it - The suite is executed
- The context manager’s
__exit__()method is invoked
The match Statement
The match statement is used for pattern matching:
case) and a subject value (following match). The pattern is matched against the subject value:
Patterns
Literal Patterns
A literal pattern corresponds to most literals in Python:Capture Patterns
A capture pattern binds the subject value to a name:Wildcard Pattern
The wildcard pattern (_) always succeeds and binds no name:
Sequence Patterns
A sequence pattern contains several subpatterns to be matched against sequence elements:Mapping Patterns
A mapping pattern contains one or more key-value patterns:Class Patterns
A class pattern represents a class and its positional and keyword arguments:OR Patterns
An OR pattern is two or more patterns separated by vertical bars:Guards
A guard is an additional condition that must succeed for code inside the case block to execute:Function Definitions
A function definition defines a user-defined function object:Parameters
identifier
A simple parameter name.
default
A parameter with a default value.
varargs
A parameter that receives excess positional arguments as a tuple.
kwargs
A parameter that receives excess keyword arguments as a dictionary.
Annotations
Parameters may have an annotation of the form: expression following the parameter name. Functions may have “return” annotation of the form -> expression after the parameter list:
