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 SymbolDescriptionExampleEquivalent to
=Assignmentx = y
+=Addition Assignmentx += yx = x + y
-=Subtraction Assignmentx -= yx = x – y
*=Multiplication Assignmentx *= yx = x * y
/=Division Assignmentx /= yx = x / y
%=Modulus Assignmentx %= yx = x % y
**=Exponentiation Assignmentx **= yx = x ** y
//=Floor-division Assignmentx //= yx = x // y
&=AND Assignmentx &= yx = x & y
|=OR Assignmentx |= yx = x | y
^=XOR Assignmentx ^= yx = x ^ y
<<=Zero fill left shift Assignmentx <<= yx = x << y
>>=Signed right shift Assignmentx >>= yx = 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
Try Online
ADVERTISEMENT

Conclusion

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