Python Assignment Operator


Assignment Operator

The assignment operator is used to assign a value to a variable.

Program
x = 5 #It will assign 5 to variable x print(x) #5 x+=1 #It will increment the value of x by 1 print(x) #6 x-=1 #It will decrement the value of x by 1 print(x) #5 x*=2 #It will multiply the value of x by 2 print(x) #10 x/=2 #It will divide the value of x by 2 print(x) #5.0 - It will give float value x = 5 #reassign value to x x%=2 #It will mod the value of x by 2 print(x) #remainder = 1 x = 5 #reassign value to x x//=2 #It will do floor division the value of x by 2 print(x) #2 - It will not give the float value x**=3 #It will do the value of x to the power of 3 print(x) #8 = 2 * 2 * 2 . Here x=2 from the previous iteration x&=3 #It will do bitwise & - And operation in between the value of x and 3 print(x) #8 & 3 = 1000 & 0011 = 0000 . Hence Result is 0 x = 8 #reassign value to x x|=3 #It will do bitwise | - Or operation in between the value of x and 3 print(x) #8 | 3 = 1000 | 0011 = 1011 . Hence Result is 11 x = 5 #reassign value to x x^=3 #It will do bitwise ^ - Xor operation in between the value of x and 3 print(x) #5 ^ 3 = 0101 ^ 0011 = 0110 . Hence Result is 6 x = 5 #reassign value to x x>>=1 #In this, all the bit are shifted to right, by pushing 0 from right. print(x) #5 = 0101 = 0010(shifted to right by 1) = 2 x = 5 #reassign value to x x<<=1 #In this, all the bits are shifted to the left, by pushing 0 from left. print(x) #5 = 0101 = 1010(shifted to left by 1) = 10
Input
Output