Operator | Description | Example |
---|---|---|
= | Assigns the value on the right to the variable on the left | x = 10 |
+= | Adds the right operand to the left and assigns the result | x += 5 (equivalent to x = x + 5 ) |
-= | Subtracts the right operand from the left and assigns the result | x -= 5 |
*= | Multiplies the left operand by the right and assigns the result | x *= 5 |
/= | Divides the left operand by the right and assigns the result | x /= 5 |
%= | Performs modulus operation and assigns the remainder | x %= 5 |
**= | Raises the left operand to the power of the right and assigns the result | x **= 2 (equivalent to x = x ** 2 ) |
//= | Performs floor division and assigns the result | x //= 2 |
+=
and *=
can also work with strings and lists. For example:x = y = z = 10
is common. However, chaining arithmetic assignment operators like x += y -= z
is not valid in Python.Click here to know more our program!