Skip to content
Related Articles

Related Articles

Augmented Assignment Operators in Python

Improve Article
Save Article
  • Last Updated : 07 Sep, 2021
Improve Article
Save Article

An assignment operator is an operator that is used to assign some value to a variable. Like normally in Python, we write “a = 5 to assign value 5 to variable ‘a’. Augmented assignment operators have a special role to play in Python programming. It basically combines the functioning of the arithmetic or bitwise operator with the assignment operator. So assume if we need to add 7 to a variable “a” and assign the result back to “a”, then instead of writing normally as “a = a + 7“, we can use the augmented assignment operator and write the expression as “a += 7“. Here += has combined the functionality of arithmetic addition and assignment.

So, augmented assignment operators provide a short way to perform a binary operation and assigning results back to one of the operands. The way to write an augmented operator is just to write that binary operator and assignment operator together. In Python, we have several different augmented assignment operators like +=, -=, *=, /=, //=, **=, |=, &=, >>=, <<=, %= and ^=. Let’s see their functioning with the help of some exemplar codes:

1. Addition and Assignment (+=): This operator combines the impact of arithmetic addition and assignment. Here,

 a = a + b can be written as a += b

Example:

Python3




# Addition & Assignment
a = 15
b = 20
 
a += b
print(a)

Output

35

2. Subtraction and Assignment (-=): This operator combines the impact of subtraction and assignment.  

a = a – b can be written as a -= b

Example: 

Python3




# Subtraction & Assignment
a = 107
b = 99
 
a -= b
print(a)

Output

8

3. Multiplication and Assignment (*=): This operator combines the functionality of multiplication and assignment.  

a = a * b can be written as a *= b

Example:

Python3




# Multiplication & Assignment
a = 12
b = 23
 
a *= b
print(a)

Output

276

4. Division and Assignment (/=): This operator has the combined functionality of division and assignment.  

a = a / b can be written as a /= b

Example: 

Python3




# Division & Assignment
a = 56
b = 5
 
a /= b
print(a)

Output

11.2

5. Floor Division and Assignment (//=): It performs the functioning of floor division and assignment.  

a = a // b can be written as a //= b

Example: 

Python3




# Floor Division & Assignment
a = 56
b = 8
 
a //= b
print(a)

Output

7

6. Modulo and Assignment (%=): This operator combines the impact of the modulo operator and assignment.  

a = a % b can be written as a %= b

Example: 

Python3




# Modulo & Assignment
a = 34
b = 5
 
a %= b
print(a)

Output

4

7. Power and Assignment (**=): This operator is equivalent to the power and assignment operator together.  

a = a**b can be written as a **= b

Example: 

Python3




# Power & Assignment
a = 5
b = 3
 
a **= b
print(a)

Output

125

8. Bitwise AND & Assignment (&=): This operator combines the impact of the bitwise AND operator and assignment operator. 

a = a & b can be written as a &= b

Example: 

Python3




# Bitwise AND & Assignment
a = 12
b = 10
 
a &= b
print(a)

Output

8

9. Bitwise OR and Assignment (|=): This operator combines the impact of Bitwise OR and assignment operator.  

a = a | b can be written as a |= b

Example: 

Python3




# Bitwise OR and Assignment
a = 12
b = 10
 
a |= b
print(a)

Output

14

10. Bitwise XOR and Assignment (^=): This augmented assignment operator combines the functionality of the bitwise XOR operator and assignment operator. 

a = a ^ b can be written as a ^= b

Example: 

Python3




# Bitwise XOR and Assignment
a = 12
b = 10
 
a ^= b
print(a)

Output

6

11. Bitwise Left Shift and Assignment (<<=): It puts together the functioning of the bitwise left shift operator and assignment operator.  

a = a << b can be written as a <<= b

Example: 

Python3




# Bitwise Left Shift and Assignment
a = 17
b = 2
 
a <<= b
print(a)

Output

68

12. Bitwise Right Shift and Assignment (>>=): It puts together the functioning of the bitwise right shift operator and assignment operator.  

a = a >> b can be written as a >>= b

Example: 

Python3




# Bitwise Right Shift and Assignment
a = 17
b = 2
 
a >>= b
print(a)

Output

4

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!