Augmented Assignment Operators in Python
Last Updated :
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 & Assignment
a = 15
b = 20
a += b
print(a)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
Similar Reads
Assignment Operators in Python The Python Operators are used to perform operations on values and variables. These are the special symbols that carry out arithmetic, logical, and bitwise computations. The value the operator operates on is known as the Operand. Assignment Operators are used to assign values to variables. This opera
7 min read
Increment += and Decrement -= Assignment Operators in Python If you're familiar with Python, you would have known Increment and Decrement operators ( both pre and post) are not allowed in it. Python is designed to be consistent and readable. One common error by a novice programmer in languages with ++ and -- operators are mixing up the differences (both in pr
3 min read
Python Arithmetic Operators Python operators are fundamental for performing mathematical calculations. Arithmetic operators are symbols used to perform mathematical operations on numerical values. Arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). OperatorDescriptionS
4 min read
Chaining comparison operators in Python Checking multiple conditions in a single expression is common in programming. In Python, comparison operator chaining allows us to write cleaner, more readable code when evaluating multiple conditions. Instead of using multiple and conditions, Python enables chaining comparisons directly in a mathem
4 min read
Python Membership and Identity Operators There is a large set of Python operators that can be used on different datatypes. Sometimes we need to know if a value belongs to a particular set. This can be done by using the Membership and Identity Operators. In this article, we will learn about Python Membership and Identity Operators.Python Me
5 min read
Comparison Operators in Python Python operators can be used with various data types, including numbers, strings, boolean and more. In Python, comparison operators are used to compare the values of two operands (elements being compared). When comparing strings, the comparison is based on the alphabetical order of their characters
4 min read
Solidity - Assignment Operators Solidity is a high-level, statically-typed programming language for Ethereum smart contracts. Python, JavaScript, and C++ impact it. Solidity has several variable and value assignment operators. Solidity supports the following types of operators: Simple Assignment Operator.Arithmetic Assignment Oper
5 min read
Difference between == and is operator in Python In Python, == and is operators are both used for comparison but they serve different purposes. The == operator checks for equality of values which means it evaluates whether the values of two objects are the same. On the other hand, is operator checks for identity, meaning it determines whether two
4 min read
Python: Operations on Numpy Arrays NumPy is a Python package which means 'Numerical Python'. It is the library for logical computing, which contains a powerful n-dimensional array object, gives tools to integrate C, C++ and so on. It is likewise helpful in linear based math, arbitrary number capacity and so on. NumPy exhibits can lik
3 min read
Operator Functions in Python | Set 1 Python has predefined functions for many mathematical, logical, relational, bitwise etc operations under the module "operator". Some of the basic functions are covered in this article. 1. add(a, b) :- This function returns addition of the given arguments. Operation - a + b. 2. sub(a, b) :- This func
5 min read