Behind Python Variables: How Mutability Impacts State and Behavior.!
Let’s look at a simple example:
x = 10
y = x
print(x, y) # 10 10
x = 15
print(x, y) # 15 10
Why does y stay 10?
➡️ When x = 10, Python creates an integer object 10 in memory.
➡️ y = x makes y point to the same object.
➡️ Reassigning x = 15 doesn’t change 10; instead, it creates a new object 15.
➡️ Since integers are immutable, y still points to the old value 10.
Mutable vs Immutable
Why it matters?
Understanding mutability helps:
✅ Avoid tricky bugs.
✅ Predict how variables behave
✅ Write cleaner, safer code
Next time you debug, remember: Are you working with a new object or changing the same one?