In Python, values are objects and variables are names that refer to objects. This distinction becomes important when programs work with collections.
Understanding the Concept
An integer such as 10 is an object with a type and a value. Assigning a name makes that object available through the name. Two names can refer to the same object. Some objects, including integers and strings, are immutable, meaning the existing object cannot be changed in place. Other objects, including lists, can be modified. You do not need to manage memory addresses manually; Python tracks references and object lifetimes.
When and Why You Would Use This
Start by observing a shared list. Editing it through one name is visible through the other because both names refer to the same list object. Contrast this with assigning a new integer to a name.
Practical Example
first_names = ["Alex", "Jordan"]
shared_names = first_names
shared_names.append("Taylor")
print(first_names)
Understanding the Example
The output is ['Alex', 'Jordan', 'Taylor']. The append modifies the existing list object, so first_names sees the change. Later lessons teach copying lists and mutability in detail.
Example Output
['Alex', 'Jordan', 'Taylor']
Try It Yourself
Assign a second name to a list, append a new element, and observe both names. Then investigate how list.copy() changes the result.
Common Beginner Mistakes
- Assuming assignment always creates a separate copy of an object.
- Confusing a variable name with the object it refers to.
- Expecting immutable integers or strings to support in-place element edits.
Key Takeaways
- Variables are references to Python objects.
- Different names can refer to the same mutable object.
- Understanding mutability helps explain surprising changes later.