Python Assignment Operators
Python Assignment operators assign values to variables. The right operand's value is assigned to the left operand. Like
A = 44
Here, A is assigned the value 44 using ('=') the python's assignment operator.
Python also provides the compound assignment feature. These are explained in the below-given table.
Assignment operators in Python are:
Operators | Operations | Examples |
---|---|---|
= | It assigns the right operand's value to the left operand |
x=5 My_list = [1,5,99,65] t = 'ab' + 'c' |
+= | Add the left operand's value to the right operand, then assign it to the left operand |
A=5 A+=2 (returns 7) B=9 B+=9 (returns 18) |
-= | Subtract the right operand from the left operand, then assign it to the left operand |
A=7 A-=2 (returns 5) B=9 B-=9 (returns 0) |
*= | Multiply the left operand's value with the right operand's value, then assign it to the left operand |
A=7 A*=2 (returns 14) B=9 B*=9 (returns 81) |
/= | Divide the left operand's value with the right operand, then assign it to the left operand |
A=7 A/=2 (returns 3.5) B=9 B/=9 (returns 1) |
//= | Truncated operator divides the left operand by the right operand then assign to the left operand |
A=7 A//=2 (returns 3) B=9 B//=9 (returns 2) |
%= | Divide the left operand's value by the right operand, then assign the remainder to the left operand |
A=4 A%=3 (returns 1) B=8 B%=2 (returns 0) |
**= | The left operand's value raised to the power equal to the right operand, then assign it to the left operand |
A=2 A**=2 (returns 4) B=5 B**=3 (returns 125) |
&= | &(Bitwise and) the left operand's value is added with the right operand then assign to the left operand |
A = 2 B = 4 A &= B(returns 0) |
|= | | (Bitwise or) the left operand's value is bitwise OR with the right operand then assign to the left operand |
A = 2 B = 4 A |= B(returns 6) |
^= | ^ (Bitwise xor) the left operand to the right operand then assign to the left operand |
A = 2 B = 4 A ^= B(returns 6) |
>>= | This operator right shift the left operand's value to the number of positions equal to the right operand, then assign it to the left operand |
A = 2 B = 4 A >>= B(returns 0) |
<<= | This operator shifts the left operand's value to the number of positions equal to the right operand, then assign it to the left operand |
A = 2 B = 4 A <<= B(returns 32) |