Python Assignment Operators
Assignment operators are used to assign values to variables. Besides the simple assignment =, Python provides shorthand operators that combine arithmetic operations with assignment. These operators update the value of a variable based on its current value.
1. Simple Assignment (=)
Assigns the value on the right to the variable on the left.
x = 10
print(x) # Output: 10
2. Add and Assign (+=)
Adds the right operand to the left operand and assigns the result to the left operand.
x = 5
x += 3 # Equivalent to: x = x + 3
print(x) # Output: 8
3. Subtract and Assign (-=)
Subtracts the right operand from the left operand and assigns the result.
x = 10
x -= 4 # Equivalent to: x = x - 4
print(x) # Output: 6
4. Multiply and Assign (*=)
Multiplies the left operand by the right operand and assigns the result.
x = 6
x *= 2 # Equivalent to: x = x * 2
print(x) # Output: 12
5. Divide and Assign (/=)
Divides the left operand by the right operand and assigns the result (float division).
x = 15
x /= 4 # Equivalent to: x = x / 4
print(x) # Output: 3.75
6. Floor Divide and Assign (//=)
Performs floor division (division that rounds down to nearest integer) and assigns the result.
x = 15
x //= 4 # Equivalent to: x = x // 4
print(x) # Output: 3
7. Modulus and Assign (%=)
Calculates the remainder of division and assigns it.
x = 17
x %= 5 # Equivalent to: x = x % 5
print(x) # Output: 2
8. Exponent and Assign (**=)
Raises the left operand to the power of the right operand and assigns the result.
x = 3
x **= 3 # Equivalent to: x = x ** 3
print(x) # Output: 27
9. Bitwise AND and Assign (&=)
Performs bitwise AND and assigns the result.
x = 5 # (0101 in binary)
x &= 3 # (0011 in binary)
print(x) # Output: 1 (0001 in binary)
10. Bitwise OR and Assign (|=)
Performs bitwise OR and assigns the result.
x = 5 # (0101 in binary)
x |= 3 # (0011 in binary)
print(x) # Output: 7 (0111 in binary)
11. Bitwise XOR and Assign (^=)
Performs bitwise XOR and assigns the result.
x = 5 # (0101 in binary)
x ^= 3 # (0011 in binary)
print(x) # Output: 6 (0110 in binary)
12. Bitwise Right Shift and Assign (>>=)
Shifts bits to the right and assigns the result.
x = 16 # (10000 in binary)
x >>= 2 # Shift right by 2 bits
print(x) # Output: 4 (00100 in binary)
13. Bitwise Left Shift and Assign (<<=)
Shifts bits to the left and assigns the result.
x = 4 # (00100 in binary)
x <<= 2 # Shift left by 2 bits
print(x) # Output: 16 (10000 in binary)
Summary Table
| Operator | Equivalent to | Description | ||
|---|---|---|---|---|
= |
x = y |
Assign value | ||
+= |
x = x + y |
Add and assign | ||
-= |
x = x - y |
Subtract and assign | ||
*= |
x = x * y |
Multiply and assign | ||
/= |
x = x / y |
Divide and assign | ||
//= |
x = x // y |
Floor divide and assign | ||
%= |
x = x % y |
Modulus and assign | ||
**= |
x = x ** y |
Exponent and assign | ||
&= |
x = x & y |
Bitwise AND and assign | ||
| ` | =` | `x = x | y` | Bitwise OR and assign |
^= |
x = x ^ y |
Bitwise XOR and assign | ||
>>= |
x = x >> y |
Bitwise right shift and assign | ||
<<= |
x = x << y |
Bitwise left shift and assign |
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
