Assignment Operators
Assignment operators in Python are used to assign values to variables.
They also allow you to update a variable’s value in a shorter and cleaner way.
Assignment operators help reduce code length and improve readability.
What Are Assignment Operators?
Assignment operators assign values to variables or modify existing values.
python
x = 10
Here:
=is the assignment operatorxis the variable10is the value
Types of Assignment Operators
| Operator | Description |
|---|---|
= | Assign |
+= | Add and assign |
-= | Subtract and assign |
*= | Multiply and assign |
/= | Divide and assign |
%= | Modulus and assign |
//= | Floor divide and assign |
**= | Exponent and assign |
&= | Bitwise AND and assign |
|= | Bitwise OR and assign |
^= | Bitwise XOR and assign |
>>= | Right shift and assign |
<<= | Left shift and assign |
Basic Assignment (=)
Assigns a value to a variable.
python
x = 5
print(x)
python
name = "Python"
print(name)
python
price = 99.99
print(price)
Add and Assign (+=)
Adds a value and assigns the result.
python
x = 10
x += 5
print(x)
python
count = 0
count += 1
print(count)
python
total = 100
total += 50
print(total)
Subtract and Assign (-=)
Subtracts a value and assigns the result.
python
x = 20
x -= 5
print(x)
python
balance = 500
balance -= 100
print(balance)
python
score = 90
score -= 10
print(score)
Multiply and Assign (*=)
Multiplies and assigns the result.
python
x = 5
x *= 3
print(x)
python
value = 10
value *= 2
print(value)
python
price = 100
price *= 1.1
print(price)
Divide and Assign (/=)
Divides and assigns the result (always float).
python
x = 20
x /= 2
print(x)
python
value = 9
value /= 2
print(value)
python
amount = 100
amount /= 4
print(amount)
Modulus and Assign (%=)
Finds remainder and assigns.
python
x = 10
x %= 3
print(x)
python
value = 25
value %= 4
print(value)
python
num = 7
num %= 2
print(num)
Floor Divide and Assign (//=)
Performs floor division and assigns.
python
x = 10
x //= 3
print(x)
python
value = 9
value //= 2
print(value)
python
num = 15
num //= 4
print(num)
Exponent and Assign (**=)
Raises to power and assigns.
python
x = 2
x **= 3
print(x)
python
value = 5
value **= 2
print(value)
python
num = 9
num **= 0.5
print(num)
Bitwise Assignment Operators (Advanced)
python
x = 5
x &= 3
print(x)
python
y = 5
y |= 3
print(y)
python
z = 5
z ^= 3
print(z)
The Walrus Operator (:=)
The Walrus Operator allows you to assign and use a value in the same expression.
It was introduced in Python 3.8.
Why It’s Special
- Reduces repeated calculations
- Makes conditions cleaner
- Often used inside
ifandwhile
Basic Walrus Operator Example
python
if (n := len("Python")) > 3:
print(n)
python
data = [1, 2, 3, 4]
if (size := len(data)) > 2:
print(size)
python
text = "Hello World"
if (count := text.count("o")) > 1:
print(count)
Walrus Operator in while Loop (Very Powerful)
python
while (num := int(input("Enter number: "))) != 0:
print(num)
python
while (line := input("Enter text: ")) != "exit":
print(line)
python
while (value := len(input("Word: "))) < 5:
print("Too short")
Walrus vs Normal Assignment
Without Walrus
python
length = len("Python")
if length > 3:
print(length)
With Walrus
python
if (length := len("Python")) > 3:
print(length)
When NOT to Use Walrus Operator
- When it reduces readability
- In very simple assignments
- If teammates are beginners
Best Practice
Use the walrus operator only when it improves clarity, not just to be clever.
Summary
- Assignment operators assign and update values
+=,-=,*=,/=simplify code- Bitwise assignment operators work on binary values
- Walrus operator
:=assigns inside expressions - Walrus is powerful but should be used carefully
Practice
- Use all assignment operators on one variable
- Rewrite a loop using walrus operator
- Compare readability with and without walrus
- Predict output of walrus-based conditions