Variables
Explore the fundamental concepts of Python variables, including how they function as references to objects rather than containers. Understand how to create variables, dynamic typing with automatic type inference, variable reassignment, and important syntax and style rules for naming. This lesson helps you grasp Python's flexible and dynamic approach to handling data in programming.
We are now ready to move beyond printing static text and begin working with data. In many programming languages, variables are commonly described as containers or boxes: you create a box and then store a value inside it. Python, however, follows a different and more flexible model. Rather than acting as containers, Python variables function more like labels or name tags. An object is created in memory, and a variable simply refers to that object by attaching a name to it.
Although this distinction may seem subtle at first, it underpins Python’s dynamic nature and contributes significantly to its simplicity and ease of use.
How to create a variable
In Python, a variable is created by assigning a value to a name using the assignment operator (=). Here's the syntax:
<name> = <value>
This is not a mathematical equality statement; it is a command. It tells Python to evaluate the value on the right side and bind it to the name on the left. Because Python is dynamically typed, we do not need to declare the data type before use.
Line 2: We create an integer object
100in memory and bind the namecurrent_scoreto it.Line 3: We create a string object
"Alex"and bind the nameplayer_nameto it.Lines 5–6: Passing the variable names to
print()displays the values they reference.
Variables as references
As mentioned earlier, Python variables do not store values directly; they store references to objects. When one variable is assigned to another, Python does not create a copy of the underlying data. Rather, it assigns an additional name to the same object, effectively allowing multiple variables to refer to a single object.
We can verify this via the built-in id() function, which returns the unique memory identifier of an object. If two variables point to the same object, their IDs will be identical.
Line 2: We bind
original_valueto the object50.Line 3: We bind
alias_valueto the same object thatoriginal_valuerefers to. Remember, we did not create a new50.Lines 8–9: The
id()output confirms that both variables reference the exact same memory address.
Automatic type inference
In Python, we do not explicitly declare variable types (e.g., we do not write int x = 5). Instead, Python infers the type based on the value we assign. We can verify what type Python has detected using the built-in type() function.
Line 6: The output
<class 'int'>confirms Python inferred an integer because100has no decimal.Line 7: The output
<class 'float'>confirms Python inferred a float because100.0has a decimal.Line 8: The output
<class 'str'>confirms Python inferred a string.
Reassignment and dynamic binding
Because Python variables act as references rather than fixed containers, they can be reassigned to refer to different objects at any point during program execution. A variable that initially refers to a numeric object, for example, can later be bound to a string or another data type.
Although this flexibility enables concise, expressive code, it should be used with care. Frequently changing the type of object a variable refers to can reduce code readability and make programs more difficult to understand, maintain, and debug.
Line 2:
statusreferences the integer1.Line 4: We reassign
status. The label is ripped off the integer1and now directed to the string"Active". The integer1is no longer referenced by this name.
Naming rules and conventions
Python enforces strict rules on what makes a valid variable name (syntax), and the community encourages strong conventions on what makes a good variable name (style).
Syntax rules (must follow):
Names are case-sensitive. For example,
Scoreandscoreare two different variablesNames cannot start with a number.
Names can contain letters, numbers, and underscores (
_).Names cannot be Python keywords (like
if,class, orimport).
PEP 8 style conventions (should follow):
Use
for variable names. For example,snake-case All lowercase letters with underscores separating words original_value.Choose descriptive names that explain the variable's intent.
Avoid single-letter names like
xornunless they are for simple counters.
We have established how to create names and bind them to data. We moved away from treating Python variables as storage boxes and adopted the view of them as references or labels. This distinction explains why multiple variables can point to the same data and why we can reassign names so freely.