π Python Assignment Operators β Complete Guide with Examples (2025)
Assignment operators in Python are used to assign values to variables. But Python doesn’t stop at the plain =
operatorβit also offers a variety of augmented assignment operators that simplify code and make it more Pythonic.
Whether you’re a beginner or an experienced developer, understanding these operators helps you write clean and efficient code.
π§ What You’ll Learn
πΉ Difference between =
, +=
, *=
, and more
πΉ Real-world examples using each assignment operator
πΉ Pythonic tips and gotchas
πΉ Interview-useful explanations
π What is an Assignment Operator in Python?
An assignment operator is used to assign values to variables. Python supports both basic and compound (augmented) assignment operators.
βοΈ List of Python Assignment Operators
Operator | Example | Same As | Description |
---|---|---|---|
= | x = 5 | – | Assigns 5 to variable x |
+= | x += 3 | x = x + 3 | Adds and assigns |
-= | x -= 2 | x = x - 2 | Subtracts and assigns |
*= | x *= 4 | x = x * 4 | Multiplies and assigns |
/= | x /= 2 | x = x / 2 | Divides and assigns (float division) |
//= | x //= 2 | x = x // 2 | Floor division and assigns |
%= | x %= 3 | x = x % 3 | Modulo and assigns |
**= | x **= 2 | x = x ** 2 | Exponentiation and assigns |
&= | x &= 2 | x = x & 2 | Bitwise AND and assigns |
` | =` | `x | = 3` |
^= | x ^= 1 | x = x ^ 1 | Bitwise XOR and assigns |
>>= | x >>= 1 | x = x >> 1 | Bitwise right shift and assigns |
<<= | x <<= 2 | x = x << 2 | Bitwise left shift and assigns |
π‘ Examples of Python Assignment Operators
1. β
Basic Assignment (=
)
x = 10
print(x)
π Assigns 10 to variable x
.
2. β Add and Assign (+=
)
x = 5
x += 3
print(x) # Output: 8
π Adds 3 to x
and assigns the result back to x
.
3. β Subtract and Assign (-=
)
x = 10
x -= 4
print(x) # Output: 6
π Subtracts 4 from x
.
4. βοΈ Multiply and Assign (*=
)
x = 3
x *= 4
print(x) # Output: 12
π Multiplies x
by 4.
5. β Divide and Assign (/=
)
x = 9
x /= 2
print(x) # Output: 4.5
π Performs floating point division.
6. π½ Floor Division and Assign (//=
)
x = 9
x //= 2
print(x) # Output: 4
π Truncates the decimal part.
7. π Modulo and Assign (%=
)
x = 10
x %= 3
print(x) # Output: 1
π Remainder after division.
8. β‘ Exponentiate and Assign (**=
)
x = 2
x **= 3
print(x) # Output: 8
π Raises x
to the power of 3.
9. π£ Bitwise AND and Assign (&=
)
x = 6
x &= 3
print(x) # Output: 2
π Performs bitwise AND: 110 & 011 = 010
10. π£ Bitwise OR and Assign (|=
)
x = 6
x |= 1
print(x) # Output: 7
π Bitwise OR: 110 | 001 = 111
11. π£ Bitwise XOR and Assign (^=
)
x = 6
x ^= 3
print(x) # Output: 5
π XOR: 110 ^ 011 = 101
12. π Bitwise Right Shift and Assign (>>=
)
x = 8
x >>= 2
print(x) # Output: 2
π Right shifts 2 bits: 1000 >> 2 = 0010
13. π Bitwise Left Shift and Assign (<<=
)
x = 3
x <<= 2
print(x) # Output: 12
π Left shifts 2 bits: 0011 << 2 = 1100
π Pythonic Tip
π‘ Use augmented operators like +=
, *=
when possibleβthey’re not just shorter but often faster because Python can optimize them internally.
β οΈ Common Pitfall
x = 10
y = x += 5 # β SyntaxError
β You canβt use augmented assignments inside expressions in Python. They must stand alone.
π Summary β Key Takeaways
=
assigns values+=
,-=
,*=
etc. modify and assign in one step- Augmented assignment improves readability and performance
- Use cautiously with immutable vs mutable types
β FAQs β Python Assignment Operators
β What is the difference between =
and ==
in Python?
=
is an assignment operator, while ==
is a comparison operator that checks if two values are equal.
β Can I use +=
with strings?
Yes! It works for both numbers and strings.
msg = "Hello "
msg += "World"
print(msg) # Hello World
β Are assignment operators supported in Python functions?
Yes, but make sure to use global
or nonlocal
if modifying variables from outer scopes.
β Is x = x + y
slower than x += y
?
Slightly. x += y
may be optimized better internally, especially for mutable objects.
Share Now :