Assignment Operators
We can use Assignment operators in Python to assign values to variables.
num = 10
is a simple assignment operator that assigns the value 10 on the right to the variable num
on the left.
E.g
a = 10
We can combine assignment operator with some other operator to form compound assignment operator.
x += 10 ==> x = x+10 x -= 10 ==> x = x-10 x *= 10 ==> x = x*10
The following is the list of all possible compound assignment operators in Python.
= ==> Assign value of right side of expression to left side operand += ==> Add AND: Add right side operand with left side operand and then assign to left operand -= ==> Subtract AND: Subtract right operand from left operand and then assign to left operand *= ==> Multiply AND: Multiply right operand with left operand and then assign to left operand /= ==> Divide AND: Divide left operand with right operand and then assign to left operand %= ==> Modulus AND: Takes modulus using left and right operands and assign result to left operand //= ==> Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operand **= ==> Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operand &= ==> Performs Bitwise AND on operands and assign value to left operand |= ==> Performs Bitwise OR on operands and assign value to left operand ^= ==> Performs Bitwise xOR on operands and assign value to left operand >>= ==> Performs Bitwise right shift on operands and assign value to left operand <<= ==> Performs Bitwise left shift on operands and assign value to left operand
E.g
x=10 x+=20 print(x) ==> 30 x=10 x&=5 print(x) ==> 0
Assignment Operators