Assignment Operators

Assignment Operators are used to assign or store a specific value in a variable.

The following table lists out all the assignment operators in Python with their description, and an example.

Operator Symbol Description Example Equivalent to
= Assignment x = y
+= Addition Assignment x += y x = x + y
-= Subtraction Assignment x -= y x = x – y
*= Multiplication Assignment x *= y x = x * y
/= Division Assignment x /= y x = x / y
%= Modulus Assignment x %= y x = x % y
**= Exponentiation Assignment x **= y x = x ** y
//= Floor-division Assignment x //= y x = x // y
&= AND Assignment x &= y x = x & y
|= OR Assignment x |= y x = x | y
^= XOR Assignment x ^= y x = x ^ y
<<= Zero fill left shift Assignment x <<= y x = x << y
>>= Signed right shift Assignment x >>= y x = x >> y
Python Assignment Operators

Example

In the following program, we will take values for variables x and y, and perform assignment operations on these values using Python Assignment Operators.

Python Program

x, y = 5, 2
x += y
print(x) # 7

x, y = 5, 2
x -= y
print(x) # 3

x, y = 5, 2
x *= y
print(x) # 10

x, y = 5, 2
x /= y
print(x) # 2.5

x, y = 5, 2
x %= y
print(x) # 1

x, y = 5, 2
x **= y
print(x) # 25

x, y = 5, 2
x //= y
print(x) # 2

x, y = 5, 2
x &= y
print(x) # 0

x, y = 5, 2
x |= y
print(x) # 7

x, y = 5, 2
x ^= y
print(x) # 7

x, y = 5, 2
x <<= y
print(x) # 20

x, y = 5, 2
x >>= y
print(x) # 1

Conclusion

In this Python Tutorial, we learned what Assignment Operators are, and how to use them in a program with examples.