top of page

Assignment (Shortcut) operators

If

variable = variable op expression

then

variable op= expression

i += 2 * j => i = i+2*j

var /= 2 => var = var /2

rem %= 10 => rem = rem % 10

j -= (i + var + rem) => j-(i + var + rem)

x **= 2 => x = x **2

x *= 2 => x = x*2

sheep += 1 => sheep = sheep +1

Reference:

 

In fact, shortcut operators put a parenthesis to the whole right side.

a = 6

b = 3

a /= 2 * b

print(a)

a /= 2 * b => a = a/(2*b) => a = 6/(2*3) = 1

For this reason, this is also called Python Assignment Operators

bottom of page