What an augmented assignment operator is
An augmented assignment operator combines an arithmetic operation with an assignment. Instead of writing the variable name twice, you write it once and put the operator in front of the equals sign:
number = 10
number = number + 5 # long form
number += 5 # augmented form, same effect
print(number) # 20
As with a plain assignment, Python first computes the right-hand side, then stores the result back into the variable on the left. So x += y behaves like x = x + y. The variable must already exist, because the old value is needed for the calculation.
The augmented operators in Python
Every arithmetic operator has an augmented version:
| Operator | Same as | Example (starting from the value shown) | Result |
|---|---|---|---|
+= | x = x + y | x = 10; x += 5 | 15 |
-= | x = x - y | x = 10; x -= 5 | 5 |
*= | x = x * y | x = 3; x *= 5 | 15 |
/= | x = x / y | x = 20; x /= 4 | 5.0 |
//= | x = x // y | x = 20; x //= 3 | 6 |
%= | x = x % y | x = 11; x %= 3 | 2 |
**= | x = x ** y | x = 3; x **= 3 | 27 |
There are also bitwise versions: &=, |=, ^=, <<= and >>=. They work on the binary digits of integers, for example c = 6; c &= 3 leaves c equal to 2.
Notice the table entry for /=: it gives 5.0, not 5. Ordinary division with / always produces a float in Python, even when both operands are integers and the division is exact. If you want to stay with integers, use //=, which divides and rounds down.
There is no ++ or --
Python has no increment operator ++ and no decrement operator --. To add one to a counter, write count += 1. To subtract one, write count -= 1. Writing count++ is a syntax error, and count-- is as well.
Augmented assignment beyond numbers
The same operators work on any type that supports the matching operation. For strings, += joins text and *= repeats it:
s = "ab"
s += "cd"
print(s) # abcd
s *= 2
print(s) # abcdabcd
Mixing types still fails. After q = "a", the statement q += 1 raises TypeError: can only concatenate str (not "int") to str.
One subtle difference from the long form appears with mutable objects such as lists. For a list, += changes the existing list in place instead of building a new one, so every name that refers to that list sees the change:
a = [1, 2]
b = a
b += [3]
print(a, b) # [1, 2, 3] [1, 2, 3]
t = (1, 2)
u = t
u += (3,)
print(t, u) # (1, 2) (1, 2, 3)
A tuple cannot be changed, so u += (3,) creates a new tuple and rebinds u, leaving t alone.
The exercise, step by step
The lesson walks one variable through four augmented assignments and tracks its value after each one.
- Create
numberwith the value 18. Python needs no type or keyword. Assigning a value creates the variable, and 18 makes it anint. - Add 7 with
+=.number += 7turns 18 into 25. - Subtract 5 with
-=.number -= 5turns 25 into 20. - Divide by 4 with
/=. 20 divided by 4 is 5, but because/always returns a float,numberbecomes5.0and its type changes frominttofloat. - Multiply by 3 with
*=.5.0 * 3is15.0, still a float.
number = 18
number += 7
number -= 5
number /= 4
number *= 3
Printing number at the end shows 15.0.
Common mistakes
- Using an augmented operator on a variable that does not exist. A fresh
z += 1raisesNameError: name 'z' is not defined, since there is no old value to read. Assign a starting value first. - Swapping the characters. The operator character comes first and the equals sign second.
x =+ 1is not an augmented assignment: it parses asx = +1and setsxto 1. - Expecting
/=to keep an integer. The result is always a float. Use//=when you need whole numbers. - Forgetting that
//=rounds toward negative infinity. Withx = -7,x //= 2gives-4, not-3. - Trying to write
x++. Usex += 1.
Key takeaways
x op= yis a short form ofx = x op y: the right side is computed first, then stored back intox.- Python has
+=,-=,*=,/=,//=,%=,**=and the bitwise versions, but no++or--. /=always produces a float.//=floors the result.- The variable must exist before you use an augmented assignment on it.
- On a list,
+=extends the same object in place.
