Skip to main content

How to Subtract from a Variable in Python with Augmented Assignment (-=)

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<variable> -= <amount>
PartWhat 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:

subtract_assign.py
refund_cents = 3995
print(refund_cents)

refund_cents -= 695
print(refund_cents)

The subtract_assign.py script in a macOS-style code window: refund_cents is 3995 and printed, then refund_cents -= 695 and it's printed again.

  • refund_cents starts at 3995 cents, $39.95.
  • refund_cents -= 695 takes off a $6.95 shipping fee and keeps the result in refund_cents.

Step 2: Run It​

Terminal
uv run python subtract_assign.py
Output
3995
3300

The output in a terminal: 3995, then 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​

MistakeWhat happensDo this instead
Swapping the characters, x =- 5No error: it means x = -5, so x becomes -5Write -=, minus first
-= on a variable that has no value yetNameError: name 'total' is not defined, or inside a function UnboundLocalError: cannot access local variable 'total' where it is not associated with a valueGive it a starting value first, such as total = 0
-= on a stringTypeError: unsupported operand type(s) for -=: 'str' and 'int'Convert the text to a number first, such as int(text)

Troubleshooting​

SymptomLikely causeFix
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​