Assignment statements are used to (re)bind names to values and to modify attributes or items of mutable objects: 7.2 Assignment statements The Python Language Reference, Release 3.9.5 (See section Primaries for the syntax definitions for attributeref , subscription , and slicing .) An assignment statement evaluates the expression list (remember that this can be a single expression or a comma- separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to right. Assignment is defined recursively depending on the form of the target (list). When a target is part of a mutable object (an attribute reference, subscription or slicing), the mutable object must ultimately perform the assignment and decide about its validity, and may raise an exception if the assignment is unacceptable. The rules observed by various types and the exceptions raised are given with the definition of the object types (see section The standard type hierarchy ). Assignment of an object to a target list, optionally enclosed in parentheses or square brackets, is recursively defined as follows. Assignment of an object to a single target is recursively defined as follows. The name is rebound if it was already bound. This may cause the reference count for the object previously bound to the name to reach zero, causing the object to be deallocated and its destructor (if it has one) to be called. Chapter 7. Simple statements 84 • If the target is an attribute reference: The primary expression in the reference is evaluated. It should yield an object with assignable attributes; if this is not the case,$_{TypeError}$ is raised. That object is then asked to assign the assigned object to the given attribute; if it cannot perform the assignment, it raises an exception (usually but not necessarily$_{AttributeError}$). – Otherwise: the name is bound to the object in the global namespace or the outer namespace determined by$_{nonlocal}$ , respectively. – If the name does not occur in a$_{global}$ or$_{nonlocal}$ statement in the current code block: the name is bound to the object in the current local namespace. • If the target is an identifier (name): – Else: The object must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets. – If the target list contains one target prefixed with an asterisk, called 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. The first items of the iterable are assigned, from left to right, to the targets before the starred target. The final items of the iterable are assigned to the targets after the starred target. A list of the remaining items in the iterable is then assigned to the starred target (the list can be empty). • Else: The object must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets. • If the target list is a single target with no trailing comma, optionally in parentheses, the object is assigned to that target. $_{assignment_stmt}$::= ( target_list "=")+ ( starred_expression | yield_expression ) target_list ::= target ("," target )* [","] target ::= identifier | "(" [ target_list ] ")" | "[" [ target_list ] "]" | attributeref | subscription | slicing | "*" target