Assignment Operators
These operators are useful to store the right side value into a left side variable.
They can also be used to perform simple arithmetic operations like addition, subtraction,
etc., and then store the result into a variable.
In the Table below, let’s assume the values x = 20, y = 10 and z = 5:
Assignment Operators
Operator Example Meaning Result
Assignment operator. Stores right side value into left
= z = x+y z = 30
side variable, i.e. x+y is stored into z.
Addition assignment operator. Adds right operand to
+= z+=x the left operand and stores the result into left operand, z = 25
i.e. z = z+x.
Subtraction assignment operator. Subtracts right
-= z-=x operand from left operand and stores the result into z = -15
left operand, i.e. z = z-x.
Multiplication assignment operator. Multiplies right
*= z*=x operand with left operand and stores the result into left z = 100
operand, i.e. z = z *x.
Division assignment operator. Divides left operand with
/= z/=x right operand and stores the result into left operand, z = 0.5
i.e. z = z/x.
Modulus assignment operator. Divides left operand
%= z%=x with right operand and stores the remainder into left z=5
operand, i.e. z = z%x.
Exponentiation assignment operator. Performs power
**= z**=y value and then stores the result into left operand, i.e. z z= 9765625
= z**y.
Floor division assignment operator. Performs floor
//= z//=y division and then stores the result into left operand, z=0
i.e. z = z// y.
It is possible to assign the same value to two variables in the same statement as:
a=b=1
print(a, b) # will display 1 1
Another example is where we can assign different values to two variables as:
a=1; b=2
print(a, b) # will display 1 2
The same can be done using the following statement:
a, b = 1, 2
print(a, b) # will display 1 2
Python does not have increment operator ( ++ ) and decrement operator ( -- ) that are
available in C and Java.