Augmented Assignment Operators in Python - GeeksforGeeks

07 Sep, 2021
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 & Assignmenta = 15b = 20 a += bprint(a)
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 & Assignmenta = 107b = 99 a -= bprint(a)
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 & Assignmenta = 12b = 23 a *= bprint(a)
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 & Assignmenta = 56b = 5 a /= bprint(a)
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 & Assignmenta = 56b = 8 a //= bprint(a)
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 & Assignmenta = 34b = 5 a %= bprint(a)
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 & Assignmenta = 5b = 3 a **= bprint(a)
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 & Assignmenta = 12b = 10 a &= bprint(a)
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 Assignmenta = 12b = 10 a |= bprint(a)
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 Assignmenta = 12b = 10 a ^= bprint(a)
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 Assignmenta = 17b = 2 a <<= bprint(a)
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 Assignmenta = 17b = 2 a >>= bprint(a)
4
 
surindertarika1234
Picked
Class 11
Python
School Learning
School Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Measures of Central Tendency
What is Cyber Bullying? Definition, Types, Effects, Laws
Importance of Chemistry in Everyday Life
Measure of Dispersion
What is Plagiarism? Definition, Types, How to Avoid, Laws
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe