Simple Statements
A simple statement is comprised within a single logical line. Several simple statements may occur on a single line separated by semicolons.Expression Statements
Expression statements are used (mostly interactively) to compute and write a value, or (usually) to call a procedure (a function that returns no meaningful result; in Python, procedures return the valueNone).
None, it is converted to a string using the built-in repr() function and the resulting string is written to standard output.
Assignment Statements
Assignment statements are used to (re)bind names to values and to modify attributes or items of mutable objects:Assignment Rules
Assignment of an object to a target list is recursively defined as follows:- If the target list is a single target with no trailing comma, the object is assigned to that target
- If the target list contains one target prefixed with an asterisk (a “starred” target), the object must be an iterable with at least as many items as there are targets in the target list, minus one:
Assignment to Names
If the target is an identifier (name):- If the name does not occur in a
globalornonlocalstatement in the current code block, the name is bound to the object in the current local namespace - Otherwise, the name is bound to the object in the global namespace or the outer namespace determined by
nonlocal
Assignment to Attributes
If the target is an attribute reference, the primary expression in the reference is evaluated. It should yield an object with assignable attributes. That object is then asked to assign the assigned object to the given attribute:If the object is a class instance and the attribute reference occurs on both sides of the assignment operator, the right-hand side expression
a.x can access either an instance attribute or (if no instance attribute exists) a class attribute. The left-hand side target a.x is always set as an instance attribute.Assignment to Subscriptions
If the target is a subscription, the primary’s__setitem__() method is called with two arguments: the subscript and the assigned object:
Augmented Assignment Statements
Augmented assignment is the combination, in a single statement, of a binary operation and an assignment statement:Annotated Assignment Statements
Annotation assignment is the combination, in a single statement, of a variable or attribute annotation and an optional assignment statement:The assert Statement
Assert statements are a convenient way to insert debugging assertions into a program:
assert expression, is equivalent to:
assert expression1, expression2, is equivalent to:
The built-in variable
__debug__ is True under normal circumstances, False when optimization is requested (command line option -O).The pass Statement
pass is a null operation — when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically:
The del Statement
Deletion is recursively defined very similar to the way assignment is defined:
The return Statement
return may only occur syntactically nested in a function definition:
None is substituted. return leaves the current function call with the expression list (or None) as return value:
return statement indicates that the generator is done and will cause StopIteration to be raised. The returned value (if any) is used as an argument to construct StopIteration and becomes the StopIteration.value attribute.
The yield Statement
A yield statement is semantically equivalent to a yield expression:
The raise Statement
If no expressions are present, raise re-raises the exception that is currently being handled:
raise evaluates the first expression as the exception object. It must be either a subclass or an instance of BaseException:
Exception Chaining
Thefrom clause is used for exception chaining:
None in the from clause:
The break Statement
break may only occur syntactically nested in a for or while loop, but not nested in a function or class definition within that loop:
else clause if the loop has one.
The continue Statement
continue may only occur syntactically nested in a for or while loop. It continues with the next cycle of the nearest enclosing loop:
The import Statement
The basic import statement (no from clause) is executed in two steps:
- Find a module, loading and initializing it if necessary
- Define a name or names in the local namespace
Lazy Imports
Thelazy keyword is a soft keyword that only has special meaning when it appears immediately before an import statement:
lazy keyword, the import becomes lazy: the module is not loaded immediately at the import statement. Instead, a lazy proxy object is created and bound to the name. The actual module is loaded on first use of that name.
The global Statement
The global statement causes the listed identifiers to be interpreted as globals:
global statement applies to the entire current scope. A SyntaxError is raised if a variable is used or assigned to prior to its global declaration in the scope.
The nonlocal Statement
The nonlocal statement causes the listed identifiers to refer to previously bound variables in the nearest enclosing function scope:
SyntaxError is raised.
The type Statement
The type statement declares a type alias, which is an instance of typing.TypeAliasType:
__value__ attribute (lazy evaluation).
Type aliases may be made generic by adding a type parameter list after the name.