How to Subtract from a Variable in Python with Augmented Assignment (-=)
Counting down stock, taking a fee off a total, lowering a score: all of them change a variable by subtracting from it. Python's -= does the subtraction and stores the result in one short statement. This guide takes a shipping fee off a refund, with real output from Python 3.15.2.
Syntax
<variable> -= <amount>
| Part | What it means |
|---|---|
<variable> | A variable that already has a value |
-= | Subtract, then store the result back in the variable |
<amount> | What to subtract |
x -= 5 does what x = x - 5 does. Python's language reference calls this an augmented assignment: "the combination, in a single statement, of a binary operation and an assignment statement".
Prerequisites
- uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
The refund is in cents, as money often is in code, so the numbers stay whole:
refund_cents = 3995
print(refund_cents)
refund_cents -= 695
print(refund_cents)

refund_centsstarts at 3995 cents, $39.95.refund_cents -= 695takes off a $6.95 shipping fee and keeps the result inrefund_cents.
Step 2: Run It
uv run python subtract_assign.py
3995
3300

The refund went from 3995 to 3300 cents: $39.95 minus $6.95 is $33.00. The variable itself changed, so every later line sees 3300.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Swapping the characters, x =- 5 | No error: it means x = -5, so x becomes -5 | Write -=, minus first |
-= on a variable that has no value yet | NameError: name 'total' is not defined, or inside a function UnboundLocalError: cannot access local variable 'total' where it is not associated with a value | Give it a starting value first, such as total = 0 |
-= on a string | TypeError: unsupported operand type(s) for -=: 'str' and 'int' | Convert the text to a number first, such as int(text) |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A value suddenly turns negative instead of going down | =- was typed instead of -= | Swap the two characters |
FAQ
Are there other operators like -=?
Yes: +=, *=, /=, //=, %= and **=, among others. Each combines its operation with storing the result.
Is x -= 5 exactly the same as x = x - 5?
For numbers, the result is the same. The reference notes the two are "similar, but not exactly equal": with -=, x is evaluated only once, and some types change the object in place.
Conclusion
refund_cents -= 695 took the shipping fee off the refund and stored the result: 3995 became 3300.
Additional Resources
- Augmented assignment statements —
-=and the other operators.
