Variables are called variables because they can vary. A counter increments, a username updates, a status toggles. Python makes reassignment simple: you use the same equal sign you used to create the variable, and the name updates to point at the new value. The old object stays in memory as long as something still references it, and Python's garbage collector cleans up once nothing does.
This article builds on what Python variables are and how to create them. If a variable is a name pointing at an object, reassignment simply moves that name to point somewhere else. That simplicity hides a few details about mutable types and augmented operators that prevent common beginner bugs.
Basic reassignment
Reassigning a variable uses the exact same syntax as creating one, with no special keyword:
score = 10
print(score) # 10
score = 25
print(score)Each time Python sees the same name assigned again, it evaluates the right side, produces a new object, and points the name at that object instead. The previous value becomes irrelevant to that name, and Python reclaims its memory once nothing else references it. This pattern drives loops that count, functions that track state as conditions change, and any program where a value needs to move forward step by step.
Reassignment does not affect other names
Reassigning one variable never changes what another variable points to, even if the two once shared the same object. This is clearest with an immutable type like an integer:
a = 100
b = a # b points to the same integer
a = 200 # a now points to a new integer
print(a) # 200
print(b) # 100, unaffectedWhen the third line runs, Python creates a new integer for 200 and points a at it. The name b still points at the original value; the two names briefly shared an object, but reassigning one never pulls the other along. Lists follow the same rule for reassignment, but with a nuance: reassigning a name that points to a list changes which object that name points to, while modifying the list in place, for example by appending to it, is visible through every name that points to that same list. Reassignment moves a name. In-place modification changes the object underneath every name that shares it.
Reassigning to a different type
Because Python is dynamically typed, a name that held one type can later hold a completely different one. A variable can point to an integer on one line and a string a few lines later, with no conversion step required, since the type lives on the object rather than on the name. This flexibility is convenient for short scripts, but in longer functions it usually reads better to keep a variable's type consistent for its whole lifetime; a name that changes type midway through a function is often a sign the function is trying to do too much and would read more clearly split into smaller pieces.
Augmented assignment operators
Python provides shorthand operators that combine an operation with reassignment in one step:
count = 0
count += 1 # same as: count = count + 1
total = 100
total -= 20 # same as: total = total - 20Augmented assignment exists for every binary operator, including multiplication, division, and the bitwise operators. Each one applies the operation using the variable's current value and the right-side operand, then stores the result back under the same name. For a mutable type like a list, the augmented form can be more efficient than writing it out in full, because it can modify the object in place rather than building an entirely new one. For immutable types like integers, both forms behave identically, so the shorthand is chosen for readability rather than speed.
Common reassignment mistakes
The most common mistake is forgetting that string methods return a new value instead of changing the original, since strings cannot be modified in place. Calling .upper() on a string and not storing the result leaves the original variable unchanged; you have to reassign the variable with the method's return value to keep the new version. A second common mistake is a typo that creates a new variable instead of reassigning the one you meant, which runs without error but silently produces the wrong output. A third is assuming reassignment cascades between two variables that once shared a value, when in fact each name only ever points independently at whatever it was most recently assigned.
The next article covers assigning multiple variables at once, which combines reassignment with tuple unpacking to swap values and write cleaner loops. After that, naming rules rounds out the foundation you need before moving on to Python's built-in data types.
Rune AI
Key Insights
- Reassignment changes which object a variable name references.
- The old object is unaffected if another variable still points to it.
- A variable can be reassigned to a value of a completely different type.
- Augmented operators like += combine an operation with reassignment.
- Reassignment is not the same as mutating an object in place.
Frequently Asked Questions
Does reassigning a variable delete the old value?
Can I change the type of a variable by reassigning it?
What is the difference between reassigning a variable and modifying a mutable object?
Conclusion
Reassignment is how a program's state evolves over time. Understanding that reassignment changes what a name points to, not what other names point to the same object, is the key to writing predictable Python code.
More in this topic
Create and Assign Variables in Python
Learn every way to create and assign variables in Python, from basic single assignment to expressions, type hints, and practical patterns beginners use every day.
Python Variable Naming Rules
Learn Python's variable naming rules, including allowed characters, reserved keywords, PEP 8 conventions, and how to choose names that make your code readable.
Python Variables Explained
Understand what Python variables are, how they store and reference data, and why Python's variable model is different from other programming languages.