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 .

1. +=

It is used to add value of right operand to the left operand. Result will be stored in left operand.

Example:

a=10
a+=5  # a=a+5 

a becomes 15  as 5 is added to value of a i.e. 10

2. -=

It is used to subtract value of right operand from the left operand. Result will be stored in left operand.

Example:

a=10
a-=5  # a=a-5 

a becomes 5  as 5 is subtracted from value of a i.e. 10

3. *=

It is used to multiply value of right operand with the left operand. Result will be stored in left operand.

Example:

a=10
a*=5  # a=a*5 

a becomes 50  as 5 is multiplied with value of a i.e. 10

4. **=

It is used to find left operand raised to power second operand. Result will be stored in left operand.

Example:

a=10
a**=3  # a=a**3 

a becomes 1000  as value of a i.e. 10 raised to power 3 is 1000.

5. /=

It is used to divide value of left operand by the right operand. Result will be stored in left operand.

Example:

a=10
a/=3  # a=a/3 

a becomes 3.3333333333  as value of a i.e. 10 is divided by 3.

6. //=

It is used to divide value of left operand by the right operand and perform the floor division. Result will be stored in left operand.

Example:

a=10
a//=3  # a=a//3 

a becomes 3  as value of a i.e. 10 is divided by 3 by using floor division operator //.

7. %=

It is used to divide value of left operand by the right operand and store remainder in left operand.

Example:

a=10
a%=3  # a=a%3 

a becomes 1  as value of a i.e. 10 is divided by 3 and find remainder

%=

#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
Back to: Python Programming Tutorial
Spread the love