Python Operators
Estimated reading: 3 minutes 23 views

πŸ” 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

OperatorExampleSame AsDescription
=x = 5Assigns 5 to variable x
+=x += 3x = x + 3Adds and assigns
-=x -= 2x = x - 2Subtracts and assigns
*=x *= 4x = x * 4Multiplies and assigns
/=x /= 2x = x / 2Divides and assigns (float division)
//=x //= 2x = x // 2Floor division and assigns
%=x %= 3x = x % 3Modulo and assigns
**=x **= 2x = x ** 2Exponentiation and assigns
&=x &= 2x = x & 2Bitwise AND and assigns
`=``x= 3`
^=x ^= 1x = x ^ 1Bitwise XOR and assigns
>>=x >>= 1x = x >> 1Bitwise right shift and assigns
<<=x <<= 2x = x << 2Bitwise 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Python Assignment Operators

Or Copy Link

CONTENTS
Scroll to Top