Assignment Operators in Python:
Python, known for its simplicity and readability, offers a wide array of operators that allow developers to perform various operations. Among these, assignment operators play a fundamental role. They allow you to assign values to variables and perform operations simultaneously, making your code more concise and efficient.
In this comprehensive guide, we’ll delve into assignment operators in Python, exploring how they work and when to use them
The Basics: Assignment (=) Operator
The most basic assignment operator is the equal sign (=). It assigns the value on the right to the variable on the left.
x = 5 # Assign the value 5 to the variable x
Compound Assignment Operators
Python offers several compound assignment operators, which combine an operation with assignment. They provide a shortcut to perform an operation and update a variable in a single step.
1.Addition Assignment (+=)
The += operator adds the value on the right to the variable on the left and updates the variable.
x=5
x+=3
print(x)
8
3.Subtraction Assignment (-=)
The -= operator subtracts the value on the right from the variable on the left and updates the variable.
x=5
x-=3
print(x)
2
4.Multiplication Assignment (*=)
The *= operator multiplies the variable on the left by the value on the right and updates the variable.
x=5
x*=3
print(x)
15
5.Division Assignment (/=)
The /= operator divides the variable on the left by the value on the right and updates the variable.
x=5
x/=3
print(x)
- 6666666666666667
Floor Division Assignment (//=)
The //= operator performs integer division (floor division) on the variable and updates it
x=5
x//=3
print(x)
1
Modulus Assignment (%=)
The %= operator calculates the remainder when the variable on the left is divided by the value on the right and updates the variable.
x=5
x%=3
print(x)
2
Exponentiation Assignment (**=)
The **= operator raises the variable on the left to the power of the value on the right and updates the variable.
x=5
x**=3
print(x)
Get Alexekka’s stories in your inbox
Join Medium for free to get updates from this writer.
125
Bitwise Assignment Operators
In addition to arithmetic operations, Python offers bitwise assignment operators for performing bit-level operations. These operators are essential in scenarios where you need to work with individual bits of binary data.
Bitwise AND Assignment (&=)
The &= operator performs a bitwise AND operation between the variable on the left and the value on the right, updating the variable
a = 5 # Binary: 0101
b = 3 # Binary: 0011
result = a & b # Result: 1 (Binary: 0001)
x=5
x&=3
print(x)
1
Bitwise OR Assignment (|=)
The |= operator performs a bitwise OR operation between the variable on the left and the value on the right, updating the variable.
a = 5 # Binary: 0101
b = 3 # Binary: 0011
result = a | b # Result: 7 (Binary: 0111)
x=5
x|=3
print(x)
7
^ (Bitwise XOR):
Performs a bitwise XOR (exclusive OR) operation on each pair of corresponding bits. It returns 1 if the bits are different; otherwise, it returns 0.
a = 5 # Binary: 0101
b = 3 # Binary: 0011
result = a ^ b # Result: 6 (Binary: 0110)
x=5
x^=3
print(x)
~(Bitwise NOT):
Performs a bitwise NOT operation, which inverts each bit. It turns 0 into 1 and 1 into 0
a = 5 # Binary: 0101
result = ~a # Result: -6 (Binary: 1010)
x=5
x=~3
print(x)
- 4
<< (Left Shift):
Shifts the bits of the left operand to the left by the number of positions specified in the right operand. It effectively multiplies the number by 2 raised to the power of the right operand.
a = 5 # Binary: 0101
result = a << 2 # Result: 20 (Binary: 10100)
x=5
x<<=3
print(x)
40
>> (Right Shift):
Shifts the bits of the left operand to the right by the number of positions specified in the right operand. It effectively divides the number by 2 raised to the power of the right operand (rounded down to the nearest integer)
a = 12 # Binary: 1100
result = a >> 2 # Result: 3 (Binary: 0011)
x=5
x>>=3
print(x)
0
Conclusion
Assignment operators are essential tools for Python developers. They allow you to perform operations and update variables in a concise and efficient manner. Whether you’re working with basic assignments, compound operators, or bitwise operations, understanding and using assignment operators will make your Python code more powerful and maintainable.







