Assignment Operators and Arithmetic Assignment Operators

Both Excel VBA and Python use the assignment operator = for assignment. Previous sections have covered this.

In Python, there are also arithmetic assignment operators (Table 3-7). For example, a += 1 is equivalent to a = a + 1 (add 1 to a).

Table 3-7 Arithmetic Assignment Operators in Python

Examples:

code.python
>>> a = 9; a += 1; a  # 10
>>> a = 9; a -= 1; a  # 8
>>> a = 9; a *= 2; a  # 18
>>> a = 9; a /= 2; a  # 4.5
>>> a = 9; a %= 4; a  # 1
>>> a = 9; a **= 2; a  # 81
>>> a = 9; a //= 2; a  # 4