Assignment operators
Assignment operators are used to assigning values.
a = 2 is a simple assignment operator that assigns 2 to the variable a.
Python has compound operators, for example, +=, a += 1 that adds value to the variable and later assigns the same. a+=1 is equal to a = a + 1
Here is the list of the assignment operators
Operator |
Example |
Equivalent to |
= |
a= 1 |
a = 5 |
+= |
a+= 1 |
a= a+ 1 |
-= |
a -= 1 |
a=a - 1 |
*= |
a *= 2 |
a= a * 2 |
/= |
a /= 2 |
a= a / 2 |
%= |
a %= 2 |
a = x % 2 |
//= |
a//= 2 |
a = a // 2 |
**= |
a**= 2 |
a = a ** 2 |
&= |
a &= 2 |
a= a & 2 |
|= |
a |= 2 |
a = a | 5 |
^= |
a ^= 2 |
a = a^ 2 |
>>= |
a >>= 5 |
a= a>> 5 |
<<= |
a <<= 5 |
a = a << 5 |