Assignment Operators
Assignment operators store values in variables. Beyond the basic =,
Python provides powerful shortcuts like += and *= that combine operations
with assignment - making your code shorter and more expressive!
Basic Assignment
The = operator assigns a value to a variable. Python also supports assigning
multiple variables at once and swapping values elegantly.
Click Run to execute your code
a, b = 1, 2) and
swap syntax (a, b = b, a) are unique features that make code cleaner.
In most languages, swapping requires a temporary variable!
Augmented Assignment Operators
Augmented assignment operators combine an arithmetic operation with assignment in a single step. These are shortcuts that make code more concise.
| Operator | Example | Equivalent To | Description |
|---|---|---|---|
+= | x += 5 | x = x + 5 | Add and assign |
-= | x -= 5 | x = x - 5 | Subtract and assign |
*= | x *= 5 | x = x * 5 | Multiply and assign |
/= | x /= 5 | x = x / 5 | Divide and assign |
//= | x //= 5 | x = x // 5 | Floor divide and assign |
%= | x %= 5 | x = x % 5 | Modulus and assign |
**= | x **= 5 | x = x ** 5 | Exponent and assign |
Click Run to execute your code
x += 1
is not only shorter than x = x + 1, but can also be slightly more efficient
for some data types!
Assignment with Strings
Augmented assignment works with strings too! Use += to concatenate strings
and *= to repeat them.
Click Run to execute your code
+= in a loop is
inefficient for large strings because strings are immutable. Each += creates
a new string object. For many concatenations, use "".join() or f-strings instead.
The Walrus Operator :=
Python 3.8 introduced the "walrus operator" (:=), which assigns a value AND
returns it in a single expression. It's called the walrus operator because :=
looks like a walrus face sideways!
Click Run to execute your code
Common Mistakes
1. Confusing = with ==
# Wrong - assigns instead of comparing!
if x = 5: # SyntaxError in Python!
print("x is 5")
# Correct - use == for comparison
if x == 5:
print("x is 5")
2. Using augmented operators on undefined variables
# Wrong - count doesn't exist yet!
count += 1 # NameError: name 'count' is not defined
# Correct - initialize first
count = 0
count += 1
3. Forgetting that /= produces a float
# Surprise! Result is a float
x = 10
x /= 2
print(x) # 5.0, not 5!
# Use //= for integer division
x = 10
x //= 2
print(x) # 5
4. Wrong order in multiple assignment
# Wrong - values don't match variables
name, age = 25, "Alice" # Now name=25, age="Alice"!
# Correct - keep order consistent
name, age = "Alice", 25
Exercise: Score Tracker
Task: Manage a game score using assignment operators.
Requirements:
- Start with score = 0
- Add 10 points using
+= - Double the score using
*= - Subtract a penalty of 5 using
-= - Divide by 3 using
/=
Click Run to execute your code
Show Solution
score = 0
print(f"Initial score: {score}")
# 1. Add 10 to score using +=
score += 10
print(f"After +10: {score}") # 10
# 2. Multiply score by 2 using *=
score *= 2
print(f"After x2: {score}") # 20
# 3. Subtract 5 from score using -=
score -= 5
print(f"After -5: {score}") # 15
# 4. Divide score by 3 using /=
score /= 3
print(f"Final score: {score}") # 5.0
Summary
- = assigns a value to a variable
- Multiple assignment:
a, b = 1, 2assigns multiple values at once - Value swap:
a, b = b, aswaps without a temp variable - Augmented operators (+=, -=, *=, /=, //=, %=, **=) combine operation + assignment
- String += concatenates, *= repeats
- Walrus operator := (Python 3.8+) assigns AND returns a value
What's Next?
Now that you can efficiently assign and update values, let's explore bitwise operators - powerful tools for working with individual bits in numbers, essential for low-level programming and optimizations.
Enjoying these tutorials?