Python Modulus Assignment

Python modulus assignment is done with %=, the modulus assignment operator.

Example:

>>> x = 8
>>> x %= 5
>>> x
3

Read more about the Python modulo operation

You can’t do modulo operation and assignment 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 do modulo operation and assignment to a literal

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