Python Subtraction Assignment

Python subtraction assignment is done with -=, the subtraction assignment operator.

Example:

>>> x = 5
>>> x -= 3
>>> x
2

Read more about the Python subtraction operation

You can’t subtract and assign to an undefined variable

>>> d -= 3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'd' is not defined

You can’t subtract and assign to a literal

>>> 3 -= 3
  File "<stdin>", line 1
SyntaxError: can't assign to literal

Python doesn’t have a -- operator, like some other languages, so you will frequently see statements like this instead:

x -= 1