Search⌘ K
AI Features

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.

Python 3.14.0
# Creating variables by assigning values
current_score = 100
player_name = "Alex"
print(current_score)
print(player_name)
  • Line 2: We create an integer object 100 in memory and bind the name current_score to it.

  • Line 3: We create a string object "Alex" and bind the name player_name to it.

  • Lines 5–6: Passing the variable names to print() displays the values they reference.

Python variables reference objects in memory, with names acting as labels rather than value holders
Python variables reference objects in memory, with names acting as labels rather than value holders

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.

Python 3.14.0
original_value = 50
alias_value = original_value # Assigning one variable to another
print(original_value)
print(alias_value)
# Checking their identity
print(id(original_value))
print(id(alias_value))
  • Line 2: We bind original_value to the object 50.

  • Line 3: We bind alias_value to the same object that original_value refers to. Remember, we did not create a new 50.

  • 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.

Python
# Checking types
num_a = 100
num_b = 100.0
text = "Hero"
print(type(num_a))
print(type(num_b))
print(type(text))
  • Line 6: The output <class 'int'> confirms Python inferred an integer because 100 has no decimal.

  • Line 7: The output <class 'float'> confirms Python inferred a float because 100.0 has 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.

Python 3.14.0
status = 1
print(status)
status = "Active" # Reassigning a variable to a different type
print(status)
  • Line 2: status references the integer 1.

  • Line 4: We reassign status. The label is ripped off the integer 1 and now directed to the string "Active". The integer 1 is 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, Score and score are two different variables

  • Names cannot start with a number.

  • Names can contain letters, numbers, and underscores (_).

  • Names cannot be Python keywords (like if, class, or import).

PEP 8 style conventions (should follow):

  • Use snake-caseAll lowercase letters with underscores separating words for variable names. For example, original_value.

  • Choose descriptive names that explain the variable's intent.

  • Avoid single-letter names like x or n unless 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.