Assignment and Binding
Variables are created and bound to objects using the assignment operator (=). This operation evaluates the expression on the right side to instantiate an object in memory, and then binds the identifier on the left side to that object’s memory address.
Memory and Reference Mechanics
When multiple variables are assigned the same value, Python may bind them to the same object in memory to optimize resources (a process known as interning). You can inspect the memory address of the underlying object using the built-inid() function, and verify object identity using the is operator.
Identifier Rules
Variable names must adhere to strict lexical rules enforced by the Python parser:- Allowed Characters: Can contain alphanumeric characters, underscores (
_), and natively supported Unicode characters (e.g.,A-Z,a-z,0-9,π,é). - Prefix Constraints: Cannot begin with a numeric digit.
- Case Sensitivity:
Variableandvariablerepresent distinct, independent references. - Reserved Words: Cannot conflict with Python’s built-in keywords (e.g.,
class,return,def,yield).
Scope and Namespace
A variable’s accessibility is determined by its scope, which dictates the specific namespace where the identifier-to-object binding exists. Python resolves variable scope hierarchically using the LEGB rule:- Local: Bound within the current function or method.
- Enclosing: Bound within the local scope of any enclosing functions (used in closures).
- Global: Bound at the top level of a script or module.
- Built-in: Pre-assigned in Python’s built-in namespace (e.g.,
len,Exception).
Tired of Poor Python Skills? Fix That With Deep Grasping!Learn More





