Augmented operators in Python 3 | Assignment operators in Python 3

Augmented operators in Python 3 | Assignment operators in Python 3

Augmented operators in Python are used to update the value of a variable in an easy way. There are seven assignment Operators in Python .

  • +=
  • -=
  • *=
  • **=
  • /=
  • //=
  • %=
Operator Description Example
+= It is used to add value of right operand to the left operand. Result will be stored in left operand. a=10
a+=5 [ a=a+5 ]
a becomes 15 as 5 is added to value of a i.e. 10
-= It is used to subtract value of right operand from left operand. Result will be stored in left operand.

a=10
a-=5

[a=a-5]
a becomes15 as 5 is subtracted from value of a i.e. 10

*= It is used to multiply value of right operand to the left operand. Result will be stored in left operand. a=10
a*=5 [a=a*5]
a becomes 50 as 5 is multiplied with value of a i.e. 10
**= It is used to find left operand raised to power second operand. Result will be stored in left operand. a=10
a**=3 [a=a**3]
a becomes 1000 as value of a i.e. 10 raised to power 3 is 1000.
/= It is used to divide the value of left operand by right operand and store the quotient in left operand. a=10
a/=3 [a=a/3]
a becomes 3.3333333333333335 as value of a i.e. 10 is divided by 3. Quotient 3.3333333333333335 is stored in variable a.
//= It is used to divide the value of left operand by right operand and store the quotient (without decimal point) in left operand. a=10
a//=3 [a=a/3]
a becomes 3 as value of a i.e. 10 is divided by 3. Quotient 3 is stored in variable a.
%= It is used to divide the value of left operand by right operand and store the remainder in left operand. a=10
a%=3 [a=a%3]
a becomes 1 as value of a i.e. 10 is divided by 3. Remainder 1 is stored in variable a.

#Program to demonstrate Augmented/Assignment Operators

a=10
a+=5
print(a)

#Output is 15 as 5 is added to value of a i.e. 10.
a-=3
print(a)
#Output is 12 as 3 is subtracted from value of a i.e. 15.
a*=3
print(a)
#Output is 36 as 3 is multiplied with value of a i.e. 12.
a**=2
print(a)
#Output is 1296 as value of a i.e. 36 raised to power 2 is 1296.
a/=10
print(a)
#Output is 129.6 as value of a i.e. 1296 is divided by 10.
a//=2
print(a)
#Output is 64 as value of a i.e. 129.6 is divided by 2. Digits after decimal point are discarded.
a%=3
print(a)
#Output is 1.0 as as value of a i.e 64 is divided by 3 to get remainder 1.

 

Spread the love
Lesson tags: Assignment operators in Python 3, Augmented operators in Python 3, python operators
Back to: Python Programming Tutorial
Spread the love