Assignment Operators
What are Assignment Operators
Generally in programming, assignment operators are those operators(symbols) that are used to assign a value to a variable.
Basic Assignment operator
Basic assignment operator represented by =
assigns the value on the its right to a variable on its left
Example
>>> >>> # >>> x = 10 >>> >>> # >>> x 10 >>> >>> # >>> x = 20 >>> >>> x 20 >>>
Addition Assignment operator
Addition assignment operator symbolised by +=
adds the operand(value) on its right to the variable or property on its left, and assigns the result to the variable or property on its left.
Example
>>> >>> # >>> x = 18 >>> >>> # >>> x += 2 20 >>>
Subtraction Assignment operator
Subtraction assignment operator symbolised by -=
subtracts the operand(value) on its right from the variable or property on its left, and assigns the result to the variable or property on its left.
Example
>>> >>> # >>> x = 20 >>> >>> # >>> x -= 10 10 >>>
Multiplication Assignment operator
Multiplication assignment operator symbolised by *=
multiply the operand(value) on its right by the variable or property on its left, and assigns the result to the variable or property on its left.
Example
>>> >>> # >>> x = 10 >>> >>> # >>> x *= 2 20 >>>
Division Assignment operator
Division assignment operator represented by /=
divides the variable or object on the its left by the operand(value) on its right, and then assigns the result to the variable or object on its left
Example
>>> >>> # >>> x = 10 >>> >>> x 10 >>> >>> >>> x /= 2 >>> >>> # >>> x 5.0 >>>
Floor Division Assignment operator
Floor-Division assignment operator represented by //=
divides the variable or object on the its left by the operand(value) on its right, and then assigns the result to the variable or object on its left
Example
>>> >>> # >>> x = 10 >>> >>> # >>> x 10 >>> >>> x //= 2 >>> >>> # >>> x 5 >>>
Modulus Assignment operator
Division assignment operator represented by %=
divides the variable or object on the its left by the operand(value) on its right, and then assigns the result to the variable or object on its left
Example
>>> >>> # >>> x = 9 >>> >>> x 9 >>> >>> # >>> x %= 2 >>> >>> x 1 >>>