Instead of:
>>> spam = 42
>>> spam = spam + 42
>>> spam
84
You can use shortcuts also known as augmented assignment operators.
>>> spma = 42
>>> spma += 1
>>> spma
43
Table of augmented assignment operators #
Augmented assignment statement | Equivalent assignment statement |
---|---|
spam += 1 | spam = spam + 1 |
spam -= 1 | spam = spam -1 |
spam *= 1 | spam = spam * 1 |
spam /= 1 | spam = spam / 1 |
spam %= 1 | spam = spam % 1 |