Python Tutorial Python Advanced Python References Python Libraries

Python - assignment operators example



The example below shows the usage of assignment and compound assignment operators:

  • = Assignment operator
  • += Addition AND assignment operator
  • -= Subtraction AND assignment operator
  • *= Multiply AND assignment operator
  • /= Division AND assignment operator
  • **= Exponent AND assignment operator
  • %= Modulo AND assignment operator
  • //= Floor division AND assignment operator
a = 25

print("a =", a)

#Addition AND assignment operator
a += 5;
print("a += 5 makes a =", a)

#Subtraction AND assignment operator
a -= 8;
print("a -= 8 makes a =", a)

#Multiply AND assignment operator
a *= 4;
print("a *= 4 makes a =", a)

#Division AND assignment operator
a /= 8;
print("a /= 8 makes a =", a)

#Exponent AND assignment operator
a **= 2;
print("a **= 2 makes a =", a)

#Modulo AND assignment operator
a %= 50;
print("a %= 50 makes a =", a)

#Floor division AND assignment operator
a //= 15;
print("a //= 15 makes a =", a)

The output of the above code will be:

a = 25
a += 5 makes a = 30
a -= 8 makes a = 22
a *= 4 makes a = 88
a /= 8 makes a = 11.0
a **= 2 makes a = 121.0
a %= 50 makes a = 21.0
a //= 15 makes a = 1.0

❮ Python - Operators