Python Assignment and Augmented Assignment Operators
Python assignment operators store a value in a name, and augmented assignment operators update an existing value with a shorter form such as += or *=. The big idea is that = binds a name to an object, while augmented assignment tries to modify or replace that object in one step. This article shows how the operators work, where they behave differently, and the mistakes that often surprise beginners.
Difficulty: Beginner
Helpful to know first: basic Python variables, simple expressions, and how lists, numbers, and strings store values.
How Python Assignment Stores Values in Names
In Python, assignment does not copy a value into a box in the way many first courses describe it. More precisely, the name on the left side of = is made to refer to the object on the right side.
That distinction matters because Python objects can be shared by multiple names. For numbers this is usually invisible because numbers are immutable, but for mutable objects like lists it can change what later code sees.
Here is the smallest useful example:
score = 10
print(score) # Output: 10
score = 15
print(score) # Output: 15The first line binds the name score to the integer object 10. The second assignment makes the same name refer to 15 instead.
A variable name is a label, not the value itself. That model helps explain almost every assignment surprise in Python.
How Augmented Assignment Updates a Value
Augmented assignment combines an operation with assignment. Instead of writing x = x + 3, you can write x += 3. Python reads the current value of x, applies the operator, and then stores the result back into x.
For immutable objects such as integers and strings, augmented assignment usually creates a new object and rebinds the name. For mutable objects such as lists, it may modify the existing object in place when the type supports that behavior.
count = 5
count += 2
print(count) # Output: 7
name = "Py"
name += "thon"
print(name) # Output: PythonBoth examples end with a new value stored in the same name. The operator choice changes the syntax, but the result is still a valid assignment back to the left-hand name.
Worked Examples of Python Assignment Operators
Reassigning a name to a new object
Plain assignment is the foundation. It can create a new name or replace the old reference with a different object.
fruit = "apple"
print(fruit) # Output: apple
fruit = "banana"
print(fruit) # Output: bananaAfter the second line, fruit no longer refers to the old string object. It refers to a different string instead.
Adding numbers with +=
When both sides are numbers, += is a compact way to add and store the result.
total = 12
total += 8
print(total) # Output: 20This is the same result as total = total + 8. The shorter form is often easier to read when the operation is obvious.
Extending a list with +=
Lists are mutable, so augmented assignment can change the same list object in place.
items = ["pen", "paper"]
items += ["eraser"]
print(items) # Output: ['pen', 'paper', 'eraser']This is similar to calling list.extend(). It keeps the same list object and adds more elements to it.
Multiplying and repeating values
Augmented assignment also works with operators such as *=, /=, //=, %=, and **=.
price = 20
price *= 1.05
print(price) # Output: 21.0
pattern = "ha"
pattern *= 3
print(pattern) # Output: hahahaThese operators follow the rules of the underlying data type. A string can be repeated with an integer, but it cannot be divided.
Using multiple assignment to swap values
Python also supports assigning to more than one name at once. This is not an augmented operator, but it is part of the same assignment family and it is often used alongside it.
left = 3
right = 7
left, right = right, left
print(left, right) # Output: 7 3The right side is evaluated before the names on the left are rebound. That makes swapping safe and concise.
How Assignment and Augmented Assignment Behave in Detail
Assignment does not copy by default
Two names can refer to the same list object after assignment. If one name changes that list, the other name sees the change too.
a = [1, 2]
b = a
b.append(3)
print(a) # Output: [1, 2, 3]
print(b) # Output: [1, 2, 3]The two names point to the same list. Assignment created a second reference, not a new list.
Augmented assignment may change an object in place or rebind the name
For mutable types, Python can update the existing object when the type supports in-place behavior. For immutable types, the operation produces a new object and rebinds the name.
numbers = [1, 2]
alias = numbers
numbers += [3]
print(numbers) # Output: [1, 2, 3]
print(alias) # Output: [1, 2, 3]Because lists support in-place addition, both names still refer to the same updated list after +=.
Operator support depends on the type
Each augmented operator is only valid when the left-hand object knows how to perform the operation. Python will raise an error if the operation does not make sense for that type.
text = "cat"
# text -= "a" # TypeError: unsupported operand type(s) for -=: 'str' and 'str'Python does not define subtraction for strings, so -= cannot work there. The specific operator must match the data type.
Evaluation order matters for compound expressions
Python evaluates the right side before completing the assignment. That matters when the right side depends on the current value of the same name.
x = 10
x += 5
print(x) # Output: 15The current value of x is read first, then the result is written back to x.
Predict the Output
Aliasing with a list and +=
a = [1]
b = a
a += [2]
print(a)
print(b)What does this print?
Show answer
It prints:
[1, 2]
[1, 2]a and b refer to the same list. The += operation on a list updates that list in place, so both names see the new element.
Rebinding with an integer and +=
n = 4
m = n
n += 1
print(n)
print(m)What does this print?
Show answer
It prints:
5
4Integers are immutable. n += 1 creates a new integer object and rebinds n, while m still refers to the old integer object.
Common Mistakes with Assignment Operators
Using = when you meant comparison
Beginners sometimes try to use assignment inside an if condition. In Python, = is assignment, not comparison.
Problem: This code tries to assign a value inside the condition, but Python does not allow assignment there.
if score = 10:
print("Perfect")Fix: Use == to compare values.
if score == 10:
print("Perfect")The corrected version works because == asks whether the value equals 10 instead of trying to assign it.
Expecting += to copy a mutable object
With lists, += can affect every name that refers to the same list. That is often the opposite of what a beginner expects.
Problem: The code assumes b will stay unchanged after a += [3], but both names still point to the same list.
a = [1, 2]
b = a
a += [3]
print(b)Fix: Make an actual copy when you need a separate list.
a = [1, 2]
b = a.copy()
a += [3]
print(b)The fixed version works because copy() creates a new list object before the update happens.
Using an augmented operator with the wrong type
Each augmented operator has type rules. If the operation is not defined for the data type, Python raises a TypeError.
Problem: Strings support concatenation and repetition, but not subtraction.
word = "hello"
word -= "l"Fix: Use string methods or a different operation that the type supports.
word = "hello"
word = word.replace("l", "")
print(word) # Output: heoThe corrected version works because replace() returns a new string with the unwanted character removed.
Off-by-one thinking with += in loops
Augmented assignment is often used in loops, and beginners sometimes misread how many times it runs.
Problem: This loop adds one for each number from 0 through 2, which means three updates, not two.
total = 0
for number in range(3):
total += number
print(total)Fix: Remember that range(3) produces 0, 1, and 2.
total = 0
for number in range(3):
total += number
print(total) # Output: 3The corrected version works because it matches the actual sequence produced by range().
Find the Bug
This code is meant to build a running total and keep a separate copy of the starting list. It looks reasonable, but it has a subtle bug about assignment.
starting_values = [2, 4]
values = starting_values
values += [6]
print(starting_values)
print(values)What is the bug, and how do you fix it?
Show answer
The bug is that values = starting_values does not create a copy. Both names refer to the same list, so += changes both.
Fixed code:
starting_values = [2, 4]
values = starting_values.copy()
values += [6]
print(starting_values)
print(values)The fix works because the copy is a separate list, so the update affects only values.
When to Use Augmented Assignment and When to Use Something Else
Use augmented assignment when you want to update a name with a simple, readable operation and the meaning is obvious. Use plain assignment when the new value comes from a more complex expression or when you want to make a copy explicitly.
| Situation | Better choice | Reason |
|---|---|---|
| Add a number to a running total | total += amount | Short and clear |
| Build a new string from pieces | text += part | Readable, though repeated string building can be inefficient in long loops |
| Extend a list in place | items += more_items or items.extend(more_items) | Both update the same list |
| Need a separate copy | copy() or slicing | Assignment alone only creates another reference |
| Need a completely different value | name = complex_expression() | Plain assignment is clearer |
One important detail: += and = are not interchangeable for mutable objects. If the object is shared, augmented assignment can affect every reference to it.
Interview Questions About Python Assignment Operators
What is the difference between = and +=?
= binds a name to a value. += adds to the current value when the type supports addition, then stores the result back into the same name. For mutable objects, += may update the object in place.
Why does b change when a += [1]?
Because a and b refer to the same list object. The list is updated in place, so both names see the change.
Why is assignment not allowed inside if like in some other languages?
Python keeps = for assignment only. That design reduces a common source of bugs where a programmer meant to compare values but accidentally assigned one instead.
Try It Yourself
Write a small program that tracks a shopping list and a running total:
- Create a list with "milk" and "bread".
- Make a second name that refers to the same list.
- Use += to add "eggs".
- Print both names so you can see whether they both changed.
- Then create a separate copy and show that changing the original does not change the copy.
Expected output:
['milk', 'bread', 'eggs']
['milk', 'bread', 'eggs']
['milk', 'bread', 'eggs', 'butter']
['milk', 'bread', 'eggs']Hint: Use list.copy() for the separate copy.
Show solution
shopping_list = ["milk", "bread"]
same_list = shopping_list
shopping_list += ["eggs"]
print(shopping_list)
print(same_list)
copy_list = shopping_list.copy()
shopping_list += ["butter"]
print(shopping_list)
print(copy_list)Summary
- = binds a name to an object; it does not copy by default.
- +=, -=, *=, and similar operators update a value and store the result back.
- For mutable objects, augmented assignment may modify the object in place.
- For immutable objects, augmented assignment usually creates a new object and rebinds the name.
- Shared references are the main reason assignment surprises beginners.
- Use copy() or another copy method when you need a separate object.
Next, read about Python comparison operators so you can tell the difference between assignment, equality, and identity in conditions.