Python Operators
Estimated reading: 3 minutes 276 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 :
Share

Python Assignment Operators

Or Copy Link

CONTENTS
Scroll to Top