reference
In Python, a variable is a location in memory (on the stack) that holds a reference to an object. The ‘stack’ is static memory set by the bytecode compiler. The stack is also known as ‘local storage’.
values
Conversely, values of the variables are stored on the ‘heap’ which is dynamic memory managed by the PVM or JIT compiler depending on whether CPython or PyPy is the implementation. The heap is also known as ‘free storage’.
All variables in Python reference objects.
All variables in Python reference objects. Each variable is an address location on the stack and it holds a reference (address) to the actual storage location in memory where the value is stored. This method of variable storage is different than that employed in some other languages. For instance, in C++, the variable location actually stores the value and not a reference to the value.
Variables (the references to the actual values)
Variables (the references to the actual values) and the values to which they point are both stored in RAM (random access memory).
Variables (the references)
Variables (the references) are stored on the program stack.
Values
Values are stored on the program heap.
Variable values
Variable values, the data stored at a location in memory, can change while the program is running.
data type
The data type to which a variable references can change.
If two variables
If two variables contain the same value and are in the same scope they point to the same object in memory
out of scope
When a variable goes out of scope it is marked as ready for deletion.
When a program exits
When a program exits, all references to the program’s variable data are lost and the memory is released back to the operating system.
scope
Variables created inside a function have scope which is local to that function. This means that those variables are not visible from and cannot be used outside of that function. This is known as ‘function-level scope’.
Variables in some languages
Variables in some languages like C++ and Java have block-level scope. Python uses function-level scope which means variable visibility is limited to the function containing the variable. If a variable is not in a function, it is visible within the entire module. PHP and Javascript also use function-level scope.
module-level or global scope
Variables created outside of all functions have module-level or global scope and are visible to all code in that module.
Identifiers
Identifiers provide a convenient means to identify variables which is much nicer than using a hexadecimal addresses. An identifier is a name for a variable, function, module, class, or any other object.
assignment operator
And, each of the variables is initialized by using the ‘=’ and a value. The ‘=’ is known as the assignment operator. A variable is initialized the first time it is assigned a value. Again, the ‘=’ is known as the assignment operator. It assigns values on the right to variables on the left. Variables on the left are also known as ‘lvalues’. The ‘l’ represents a location in memory. To assign something to the right of the ‘=’ to something on the left, the item on the left must have a name and be a storage location.
Naming rules
Naming rules for Python identifiers must be followed or syntax errors will result.
Naming Conventions
Naming Conventions for Python identifiers are recommended but not required.
Numbers
Numbers (Integer, Boolean, Real, Complex)
Sequences
Sequences (Immutable - String, Tuple, Bytes; Mutable - Lists, Byte Arrays)
Mappings
(Dictionary)
Sequences
in
Python Language Reference
Sequences -
the Python Language Reference states that sequences “represent finite ordered sets indexed by non-negative numbers.” This means the sets (or groups of items) are placed in order (one after another but not necessarily sorted) and each item can be accessed by an index number. Items are also known as elements. Sequences come in two categories: immutable and mutable.
Immutable
Immutable - sequences that cannot be changed after creation. These include strings, tuples, and bytes.
Mutable
Mutable - sequences that can be changed after creation.