Assignment operators are used in Python to assign values to variables.
a = 5 is a simple assignment operator that assigns the value 5 on the right to the variable a on the left.
There are various compound operators in Python like a += 5 that adds to the variable and later assigns the same. It is equivalent to a = a + 5.
| Operator |
Example |
Equivalent to |
| = |
x = 5 |
x = 5 |
| += |
x += 5 |
x = x + 5 |
| -= |
x -= 5 |
x = x - 5 |
| *= |
x *= 5 |
x = x * 5 |
| /= |
x /= 5 |
x = x / 5 |
| %= |
x %= 5 |
x = x % 5 |
| //= |
x //= 5 |
x = x // 5 |
| **= |
x **= 5 |
x = x ** 5 |
| &= |
x &= 5 |
x = x & 5 |
| |= |
x |= 5 |
x = x | 5 |
| ^= |
x ^= 5 |
x = x ^ 5 |
| >>= |
x >>= 5 |
x = x >> 5 |
| <<= |
x <<= 5 |
x = x << 5 |