Python Assignment Operators


Introduction to Python Assignment Operators

Assignment Operators are used for assigning values to the variables. We can also say that assignment operators are used to assign values to the left-hand side operand. For example, in the below table, we are assigning a value to variable ‘a’, which is the left-side operand.

OperatorDescriptionExampleEquivalent
=Assignmenta = 2a = 2
+=Addition and Assignmenta += 2a = a + 2
-=Subtraction and Assignmenta -= 2a = a – 2
*=Multiplication and Assignmenta *= 2a = a * 2
/=Division and Assignmenta /= 2a = a / 2
%=Modulus and Assignmenta %= 2a = a % 2
//=Floor Division and Assignmenta //= 2a = a // 2
**=Exponential and Assignmenta **= 2a = a ** 2
&=Bitwise AND and Assignmenta &= 2a = a & 2
|=Bitwise OR and Assignmenta |= 2a = a | 2
^=Bitwise XOR and Assignmenta ^= 2a = a ^ 2
>>=Bitwise Right Shift and Assignmenta >>= 2a = a >> 2
<<=Bitwise Left Shift and Assignmenta <<= 3a = a << 2

Assignment Operators

Assignment Operator

Equal to sign ‘=’ is used as an assignment operator. It assigns values of the right-hand side expression to the variable or operand present on the left-hand side.

Assigns value 3 to variable ‘a’.

In [1]:
a = 3
print(a)
3

Addition and Assignment Operator

The addition and assignment operator adds left-side and right-side operands and then the sum is assigned to the left-hand side operand.

Below code is equivalent to:
 a = a + 2.

In [1]:
a = 3
a += 2

print(a)
5

Subtraction and Assignment Operator

The subtraction and assignment operator subtracts the right-side operand from the left-side operand, and then the result is assigned to the left-hand side operand.

Below code is equivalent to:
 a = a – 2.

In [1]:
a = 3
a -= 2

print(a)
1

Multiplication and Assignment Operator

The multiplication and assignment operator multiplies the right-side operand with the left-side operand, and then the result is assigned to the left-hand side operand.

Below code is equivalent to:
 a = a * 2.

In [1]:
a = 3
a *= 2

print(a)
6

Division and Assignment Operator

The division and assignment operator divides the left-side operand with the right-side operand, and then the result is assigned to the left-hand side operand.

Below code is equivalent to:
 a = a / 2.

In [1]:
a = 3
a /= 2

print(a)
1.5

Modulus and Assignment Operator

The modulus and assignment operator divides the left-side operand with the right-side operand, and then the remainder is assigned to the left-hand side operand.

Below code is equivalent to:
 a = a % 3.

In [1]:
a = 8
a %= 3

print(a)
2

Floor Division and Assignment Operator

The floor division and assignment operator divides the left side operand with the right side operand. The result is rounded down to the closest integer value(i.e. floor value) and is assigned to the left-hand side operand.

Below code is equivalent to:
 a = a // 3.

In [1]:
a = 8
a //= 3

print(a)
2

Exponential and Assignment Operator

The exponential and assignment operator raises the left-side operand to the power of the right-side operand, and the result is assigned to the left-hand side operand.

Below code is equivalent to:
 a = a ** 3.

In [1]:
a = 2
a **= 3

print(a)
8

Bitwise AND and Assignment Operator

Bitwise AND and assignment operator performs bitwise AND operation on both the operands and assign the result to the left-hand side operand.

Below code is equivalent to:
 a = a & 3.

In [1]:
a = 2
a &= 3

print(a)
2

Illustration:

Numeric ValueBinary Value
2010
3011
Bitwise AND2010

Bitwise OR and Assignment Operator

Bitwise OR and assignment operator performs bitwise OR operation on both the operands and assign the result to the left-hand side operand.

Below code is equivalent to:
 a = a | 3.

In [1]:
a = 2
a |= 3

print(a)
3

Illustration:

Numeric ValueBinary Value
2010
3011
Bitwise OR3011

Bitwise XOR and Assignment Operator

Bitwise XOR and assignment operator performs bitwise XOR operation on both the operands and assign the result to the left-hand side operand.

Below code is equivalent to:
 a = a ^ 3.

In [1]:
a = 2
a ^= 3

print(a)
1

Illustration:

Numeric ValueBinary Value
2010
3011
Bitwise XOR1001

Bitwise Right Shift and Assignment Operator

Bitwise right shift and assignment operator right shifts the left operand by the right operand positions and assigns the result to the left-hand side operand.

Below code is equivalent to:
 a = a >> 1.

In [1]:
a = 2
a >>= 1

print(a)
1
In [2]:
a = 4
a >>= 1

print(a)
2

Illustration:

Numeric InputBinary ValueRight shift by 1Numeric Output
2001000011
4010000102

Bitwise Left Shift and Assignment Operator

Bitwise left shift and assignment operator left shifts the left operand by the right operand positions and assigns the result to the left-hand side operand.

Below code is equivalent to:
 a = a << 1.

In [1]:
a = 2
a <<= 1

print(a)
4
In [2]:
a = 4
a <<= 1

print(a)
8

Illustration:

Numeric InputBitwise ValueLeft shift by 1Numeric Output
2001001004
4010010008

Resources

Handwritten Notes

References:

  1. Different Assignment operators in Python
  2. Assignment Operator in Python
  3. Assignment Expressions