Assignment Operators in Python
Assignment Operators in Python
Assignment Operator combines the effect of arithmetic and assignment operator
Symbol | Description | Example |
= | Assigned values from right side operands to left variable | >>>x=12* >>>y="greetings" |
+= | added and assign back the result to left operand | >>>x+=2 |
-= | subtracted and assign back the result to left operand | x-=2 |
*= | multiplied and assign back the result to left operand | x*=2 |
/= | divided and assign back the result to left operand | x/=2 |
%= | taken modulus using two operands and assign the result to left operand | x%=2 |
**= | performed exponential (power) calculation on operators and assign value to the left operand | x**=2 |
//= | performed floor division on operators and assign value to the left operand | x / /= 2 |
Example: To test Assignment Operators:
#Demo Program to test Assignment Operators
x=int (input("Type a Value for X : "))
print ("X = ",x)
print ("The x is =",x)
x+=20
print ("The x += 20 is =",x)
x-=5
print ("The x -= 5 is = ",x)
x*=5
print ("The x *= 5 is = ",x)
x/=2
print ("The x /= 2 is = ",x)
x%=3
print ("The x %= 3 is = ",x)
x**=2
print ("The x **= 2 is = ",x)
x//=3
print ("The x //= 3 is = ",x)
#Program End
Output:
Type a Value for X : 10
X = 10
The x is = 10
The x += 20 is = 30
The x -= 5 is = 25
The x *= 5 is = 125
The x /= 2 is = 62.5
The x %= 3 is = 2.5
The x **= 2 is = 6.25
The x //= 3 is = 2.0