Page Contents
Python Assignment operator is used to assign a value to a variable. All the assignment operators and their application are shown below in the form of a table. Every time we will take the value of x as 10 .
Operator | Example | Description | Result |
---|---|---|---|
= (Assign) | x = 2 | x = 2 | 2 |
+= (Add and Assign) | x += 2 | x = x+2 | 12 |
-= (Subtract and Assign) | x -= 2 | x = x-2 | 8 |
*= (Multiply and Assign) | x *= 2 | x = x*2 | 20 |
/= (Divide and Assign) | x /= 2 | x = x/2 | 5.0 |
//= (Divide (floor) and Assign) | x //= 2 | x = x//2 | 5 |
%= (Modulus and Assign) | x %= 2 | x = x%2 | 0 |
**= (Exponent and Assign) | x **= 2 | x = x**2 | 100 |
&= (Bitwise And and Assign) | x &= 2 | x = x&2 | 2 |
|= (Bitwise Or and Assign) | x |= 2 | x = x|2 | 10 |
^= (Bitwise Xor and Assign) | x ^= 2 | x = x^2 | 8 |
>>= (Bitwise Right Shift and Assign) | x >>= 2 | x = x>>2 | 2 |
<<= (Bitwise Left Shift and Assign) | x <<= 2 | x = x<<2 | 40 |
Now we will see about all the assignment operators in details with code . Here left operand is the variable and right operand is the value . we can also use variable as right operand .
Assign (“=”) Operator
Python Assign(“=”) operator is used to assign value to a variable –
Example 1:
x=10
print("x =",x)
x = 10
In the above code we assign the value of x
variable as 10
using =
operator . After that we print the value of x
.
Add and Assign (“+=”) Operator
Python Add and Assign(“+=”) operator is used to add value with the variable and then assign the result to the variable .
Example 1:
x=10
x += 2 # treated as x=x+2
print("x =",x)
x = 12
- In the above code first we assign the value of
x
as10
. - Then we use
+=
operator with value2
. After that value ofx
changes to x+2 = 10+2 =12
. - After that we print
x
.
Subtract and Assign (“-=”) Operator
Python Subtract and Assign(“-=”) operator is used to subtract value from the variable and then assign the result to the variable .
Example 1:
x=10
x -= 2 # treated as x=x-2
print("x =",x)
x = 8
- In the above code first we assign the value of
x
as10
. - Then we use
-=
operator with value2
. After that value ofx
changes to x-2 = 10-2 =8
. - After that we print
x
.
Multiply and Assign (“*=”) Operator
Python Multiply and Assign(“*=”) operator is used to multiply value with the variable and then assign the result to the variable .
Example 1:
x=10
x *= 2 # treated as x=x*2
print("x =",x)
x = 20
- In the above code first we assign the value of
x
as10
. - Then we use
*=
operator with value2
. After that value ofx
changes to x*2 = 10*2 =20
. - After that we print
x
.
Divide and Assign (“/=”) Operator
Python Divide and Assign(“/=”) operator is used to divide the variable with the value and then assign the result to the variable .
Example 1:
x=10
x /= 2 # treated as x=x/2
print("x =",x)
x = 5.0
- In the above code first we assign the value of
x
as10
. - Then we use
/=
operator with value2
. After that value ofx
changes to x/2 = 10/2 =5.0
. - After that we print
x
.
Divide(Floor) and Assign (“//=”) Operator
Python Divide(Floor) and Assign(“//=”) operator is used to divide(Floor) the variable with the value and then assign the result to the variable .
Example 1:
x=10
x //= 2 # treated as x=x//2
print("x =",x)
x = 5
- In the above code first we assign the value of
x
as10
. - Then we use
//=
operator with value2
. After that value ofx
changes to x//2 = 10//2 =5
. - After that we print
x
.
Modulus and Assign (“%=”) Operator
Python Modulus and Assign(“%=”) operator is used to take modulus of the variable by the value and then assign the result to the variable .
Example 1:
x=10
x %= 2 # treated as x=x%2
print("x =",x))
x = 0
- In the above code first we assign the value of
x
as10
. - Then we use
%=
operator with value2
. After that value ofx
changes to x%2 = 10%2 =0
. - After that we print
x
.
Exponent and Assign (“**=”) Operator
Python Exponent and Assign(“**=”) operator is used to take exponential of the variable by the value and then assign the result to the variable .
Example 1:
x=10
x **= 2 # treated as x=x**2
print("x =",x)
x = 100
- In the above code first we assign the value of
x
as10
. - Then we use
**=
operator with value2
. After that value ofx
changes to x**2 = 10**2 =100
. - After that we print
x
.
Bitwise AND and Assign (“&=”) Operator
Python Bitwise AND and Assign(“&=”) operator is used to take bitwise AND of the variable with the value and then assign the result to the variable .
Example 1:
x=10
x &= 2 # treated as x=x&2
print("x =",x)
x = 2
- In the above code first we assign the value of
x
as10
. - Then we use
&=
operator with value2
. After that value ofx
changes to x&2 = 10&2 =2
. - After that we print
x
.
Bitwise OR and Assign (“|=”) Operator
Python Bitwise OR and Assign(“|=”) operator is used to take bitwise OR of the variable with the value and then assign the result to the variable .
Example 1:
x=10
x |= 2 # treated as x=x|2
print("x =",x)
x = 10
- In the above code first we assign the value of
x
as10
. - Then we use
|=
operator with value2
. After that value ofx
changes to x|2 = 10|2 =10
. - After that we print
x
.
Bitwise XOR and Assign (“^=”) Operator
Python Bitwise XOR and Assign”^=” operator is used to take bitwise XOR of the variable with the value and then assign the result to the variable .
Example 1:
x=10
x ^= 2 # treated as x=x^2
print("x =",x)
x = 8
- In the above code first we assign the value of
x
as10
. - Then we use
^=
operator with value2
. After that value ofx
changes to x^2 = 10^2 =8
. - After that we print
x
.
Bitwise Left Shift and Assign (“<<=”) Operator
Python Bitwise Left Shift and Assign(“<<=”) operator is used to take bitwise Left Shift of the variable with the value and then assign the result to the variable .
Example 1:
x=10
x <<= 2 # treated as x=x<<2
print("x =",x)
x = 40
- In the above code first we assign the value of
x
as10
. - Then we use
<<=
operator with value2
. After that value ofx
changes to x<<2 = 10<<2 =40
. - After that we print
x
.
Bitwise Right Shift and Assign (“>>=”) Operator
Python Bitwise Right Shift and Assign(“>>=”) operator is used to take bitwise Right Shift of the variable with the value and then assign the result to the variable .
Example 1:
x=10
x >>= 2 # treated as x=x>>2
print("x =",x)
x = 2
- In the above code first we assign the value of
x
as10
. - Then we use
>>=
operator with value2
. After that value ofx
changes to x>>2 = 10>>2 =2
. - After that we print
x
.
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .