Shorthand Shortcut Compound Assignment Operators in Python
Operators are symbols that are used to perform different operations on variables and constants. Different operators perform different types of operations. They are divided into different types based on the operation they perform. One of such types is the assignment operators.
These Assignment operators are written by combining both operation and assignment in a single line of code to make the code more concise. This representation saves coding time as the programmer need not write the same variable name again. Hence, the code becomes more readable and crisp.
In this tutorial, all the assignment operators and their operations will be discussed thoroughly with example programs to give a clear view.
Just as the name suggests, these operators are used to assign a value or the result of an expression to variables, there are 13 operators in assignment operators.
These are the 13 operators:
Operator | Name | Short hand Notation | Operation |
= | Assign | a = b | a = b |
+= | Add and assign | a += b | a = a + b |
-= | Subtract and assign | a -= b | a = a - b |
*= | Multiply and assign | a *= b | a = a * b |
/= | Divide and assign | a /= b | a = a / b |
//= | Floor divide and assign | a //= b | a = a // b |
%= | Modulus and assign | a %= b | a = a % b |
**= | Exponent and assign | a **= b | a = a ** b |
&= | Bitwise AND and assign | a &= b | a = a & b |
|= | Bitwise OR and assign | a |= b | a = a | b |
^= | Bitwise XOR and assign | a ^= b | a = a ^ b |
>>= | Bitwise right shift and assign | a >>= b | a = a >> b |
<<= | Bitwise left shift and assign | a <<= b | a = a << b |
The two variables on the right-hand and left-hand sides of the operator that participate in the operation are called the Operands. In the above notations, a and b are the operands.
Now, let us concentrate on each operator:
1. =
= is the classic assign operator we use in mathematics. This operator assigns the value or the result of the expression on its right-hand side to the left side variable/ operand.
Example program:
a = int (input ("Enter the value of a: "))
b = int (input ("Enter the value of b: "))
sum = a + b
print ("The sum of a and b: ", sum)
Output:
Enter the value of a: 3
Enter the value of b: 5
The sum of a and b: 8
Understanding:
We took the values of a and b from the user and made an expression a + b as an expression on the right-hand side of the =. The result of the expression is computed to 7, and this will be assigned to the left-hand side variable or the operand sum.
2. +=
This is a shorthand notation that denotes:
a += b is equivalent to a = a + b.
- Using shorthand operator notations helps reduce the length of the code with an equal amount of logic as the normal notation but with a compact structure.
It first adds the two operands (a + b), and then assigns the result to the first operand. Here, a will be assigned with the value of a + b
Example program:
a = int (input ("Enter the value of a: "))
b = int (input ("Enter the value of b: "))
a += b
print ("The sum of a and b: ", a)
Output:
Enter the value of a: 3
Enter the value of b: 5
The sum of a and b: 8
3. -=
This is a shorthand notation that denotes:
a -= b is equivalent to a = a - b.
It first subtracts the two operands and then assigns the result to the first operand. Here, a will be assigned with the value of a - b
Example program:
a = int (input ("Enter the value of a: "))
b = int (input ("Enter the value of b: "))
a -= b
print ("The difference between a and b: ", a)
Output:
Enter the value of a: 3
Enter the value of b: 5
The difference between a and b: -2
4. *=
This is a shorthand notation that denotes:
a *= b is equivalent to a = a * b.
It first multiplies the two operands and then assigns the result to the first operand. Here, a will be assigned with the value of a * b
Example program:
a = int (input ("Enter the value of a: "))
b = int (input ("Enter the value of b: "))
a *= b
print ("The product of a and b: ", a)
Output:
Enter the value of a: 3
Enter the value of b: 5
The product of a and b: 15
5. /=
This is a shorthand notation that denotes:
a /= b is equivalent to a = a / b.
It first divides the two operands and then assigns the result to the first operand. Here, a will be assigned with the value of a / b
Example program:
a = int (input ("Enter the value of a: "))
b = int (input ("Enter the value of b: "))
a /= b
print ("The quotient of a and b: ", a)
Output:
Enter the value of a: 3
Enter the value of b: 5
The quotient of a and b: 6
6. %=
This is a shorthand notation that denotes:
a %= b is equivalent to a = a % b.
It first does the modulus operation (finds the remainder) of the two operands and then assigns the result to first operand. Here, a will be assigned with the value of a % b
Example program:
a = int (input ("Enter the value of a: "))
b = int (input ("Enter the value of b: "))
a %= b
print ("The remainder of a and b: ", a)
Output:
Enter the value of a: 3
Enter the value of b: 5
The remainder of a and b: 3
7. //=
This is a shorthand notation that denotes:
a //= b is equivalent to a = a // b.
It first divides the two operands and then assigns the integer result to the first operand. Here, a will be assigned with the value of a // b
The difference between / and // is that / performs division and results in an integral or float quotient but // rounds up the result to an integer.
Example program:
a = int (input ("Enter the value of a: "))
b = int (input ("Enter the value of b: "))
a //= b
print ("The floor division quotient of a and b: ", a)
Output:
Enter the value of a: 3
Enter the value of b: 5
The floor division quotient of a and b: 0
8. **=
This is a shorthand notation that denotes:
a **= b is equivalent to a = a ** b.
It first calculates the exponent (raised power) using the two operands and then assigns the result to first operand. Here, a will be assigned with the value of a ** b
Example program:
a = int (input ("Enter the value of a: "))
b = int (input ("Enter the value of b: "))
a **= b
print ("The value of a raised to the power of b: ", a)
Output:
Enter the value of a: 3
Enter the value of b: 5
The value of a raised to the power of b: 243
9. &=
This is a shorthand notation that denotes:
a &= b is equivalent to a = a & b.
It first performs the bitwise AND operation on the operands and then assigns the result to the first operand. Here, a will be assigned with the value of a ** b
Example program:
a = int (input ("Enter the value of a: "))
b = int (input ("Enter the value of b: "))
a &= b
print ("The value of a &= b: ", a)
Output:
Enter the value of a: 3
Enter the value of b: 5
The value of a &= b: 1
10. |=
This is a short OR notation that denotes:
a |= b is equivalent to a = a | b.
It first performs the bitwise OR operation on the operands and then assigns the result to the first operand. Here, a will be assigned with the value of a |= b
Example program:
a = int (input ("Enter the value of a: "))
b = int (input ("Enter the value of b: "))
a |= b
print ("The value of a |= b: ", a)
Output:
Enter the value of a: 3
Enter the value of b: 5
The value of a |= b: 7
11. ^=
This is a shorthand notation that denotes:
a ^= b is equivalent to a = a ^ b.
It first performs the bitwise XOR operation on the operands and then assigns the result to the first operand, a will be assigned with the value of a ^= b
Example program:
a = int (input ("Enter the value of a: "))
b = int (input ("Enter the value of b: "))
a ^= b
print ("The value of a ^= b: ", a)
Output:
Enter the value of a: 3
Enter the value of b: 5
The value of a ^= b: 6
12. <<=
This is a short hand notation that denotes:
a <<= b is equivalent to a = a << b.
It first performs the bitwise LEFT SHIFT operation on the operands and then assigns the result to the first operand. Here, a will be assigned with the value of a <<= b
Example program:
a = int (input ("Enter the value of a: "))
b = int (input ("Enter the value of b: "))
a <<= b
print ("The value of a <<= b: ", a)
Output:
Enter the value of a: 3
Enter the value of b: 5
The value of a <<= b: 96