Different Assignment operators in Python

Assignment operators in Python are in-fix which are used to perform operations on variables or operands and assign values to the operand on the left side of the operator. It performs arithmetic, logical, and bitwise computations.

Assignment Operators in Python

Simple assignment operator in Python

The Simple assignment operator in Python is denoted by = and is used to assign values from the right side of the operator to the value on the left side.

Input:

a = b + c

Add and equal operator

This operator adds the value on the right side to the value on the left side and stores the result in the operand on the left side.

Input:

a = 5
a += 10
print (a)

Output:

15

Subtract and equal operator

This operator subtracts the value on the right side from the value on the left side and stores the result in the operand on the left side.

Input:

a = 10
a -= 5
print (a)

Output:

5

Multiply and equal operator

The Multiply and equal operator multiplies the right operand with the left operand and then stores the result in the left operand.

Input:

a = 5
a *= 2
print (a)

Output:

10

Divide and equal operator

It divides the left operand with the right operand and then stores the quotient in the left operand.

Input:

a = 10
a /= 2
print (a)

Output:

5

Modulus and equal operator

The modulus and equal operator finds the modulus from the left and right operand and stores the final result in the left operand.

Input:

a = 10
a %= 3
print (a)

Output:

1

Double divide and equal operator

The double divide and equal or the divide floor and equal operator divides the left operand with the right operand and stores the floor result in the left operand.

Input:

a = 11
a //= 3
print (a)

Output:

4

Exponent assign operator

It performs exponential or power calculation and assigns value to the left operand.

Input:

a = 2
a **= 3
print (a)

Output:

8

Bitwise And operator

Performs Bitwise And operation on both variables and stores the result in the left operand. The Bitwise And operation compares the corresponding bits of the left operand to the bits of the right operand and if both bits are 1, the corresponding result is also 1 otherwise 0.

Input:

a = 3
b = 5
a &= b
print (a)

Output:

7

The binary value of 3 is 0011 and the binary value of 5 is 0101, so when the Bitwise And operation is performed on both the values, we get 0111, which is 7 in decimal.

Bitwise OR operator

Performs Bitwise OR operator on both variables and stores the result in the left operand. The Bitwise OR operation compares the corresponding bits of the left operand to the bits of the right operand and if any one of the bit is 1, the corresponding result is also 1 otherwise 0.

Input:

a = 5
b = 10
a |= b
print (a)

Output:

15

The binary value of 5 is 0101 and the binary value of 10 is 1010, so when the Bitwise OR operation is performed on both the values, we get 1111, which is 15 in decimal.

Bitwise XOR operator

Performs Bitwise XOR operator on both variables and stores the result in the left operand. The Bitwise XOR operation compares the corresponding bits of the left operand to the bits of the right operand and if only one of the bit is 1, the corresponding result is also 1 otherwise 0.

Input:

a = 5
b = 9
a ^= b
print (a)

Output:

12

The binary value of 5 is 0101 and the binary value of 9 is 1001, so when the Bitwise XOR operation is performed on both the values, we get 1100, which is 12 in decimal.

Bitwise right shift assignment operator

This operator performs a Bitwise right shift on the operands and stores the result in the left operand.

Input:

a = 15
b = 2
a >>= b
print (a)

Output:

3

The binary value of 15 is 1111, so when the Bitwise right shift operation is performed on ‘a’, we get 0011, which is 3 in decimal.

Bitwise left shift assignment operator

This operator performs a Bitwise left shift on the operands and stores the result in the left operand.

Input:

a = 15
b = 1
a <<= b
print (a)

Output:

30

The binary value of 15 is 1111, so when the Bitwise right shift operation is performed on ‘a’, we get 00011110, which is 30 in decimal.

Closing Thoughts

In this tutorial, we read about different types of assignment operators in Python which are special symbols used to perform arithmetic, logical and bitwise operations on the operands and store the result in the left side operand. One can read about other Python concepts here.