022. Assignment Operators
In-place updates communicate intent
022. Assignment Operators
RAM Manager — Accumulating Memory Samples
Aryan’s RAM manager polls memory usage every second and keeps running statistics. Each polling cycle it needs to:
- Add the latest sample to a running total (
total_mb) - Increment the sample counter (
count) - Halve the smoothing weight for the exponential average (
weight) - Reduce the alert threshold by 5 MB each cycle to tighten the boundary (
threshold)
The broken code performs these updates correctly in long form, but uses the wrong operator in one place: it resets threshold to a brand-new value (threshold = 5) instead of subtracting 5 from the current threshold (threshold -= 5). After the first poll cycle the threshold is stuck at 5 MB regardless of where it started.
The lesson:
x = 5replaces the variable;x -= 5updates it. Augmented assignment operators (+=,-=,*=,/=) communicate that you are mutating an existing value, not creating a new one.
💡 Fun fact: Python’s += was inspired by C’s compound assignment operators, introduced in the C language standard around 1978. However, Python’s version has a subtle difference for mutable types: my_list += [1] calls __iadd__ and mutates the list in place, while my_list = my_list + [1] creates an entirely new list object.
⚠️ Watch out: The most dangerous mistake is writing plain = where -= or += is needed — threshold = 5 silently replaces the threshold with 5 on every cycle, instead of decrementing it. This produces a plausible-looking number with no error, making it one of the hardest bugs to spot in a code review.
🤔 Think about it: list_a += [1] mutates the original list, but list_a = list_a + [1] creates a new one. If two variables point to the same list, which form affects both variables and which affects only one?
Learning objectives
- Use +=, -=, *=, /= for in-place updates
- Distinguish between replacing a value (=) and updating it (-=, +=)
- Recognize the mutable-type difference between += and = for lists
Key concepts
- assignment operators
- +=
- -=
- *=
Try it
Concept detail
Augmented assignment operators combine an operation with assignment in one step: +=, -=, *=, /=, //=, **=, %=, &=, |=, ^=, >>=, <<=. x += 5 is equivalent to x = x + 5. But they are NOT identical for mutable types: for a list, list += [1] calls list.iadd and mutates in-place, while list = list + [1] creates a new list object. This matters when multiple variables point to the same list. The critical danger with plain assignment: ‘threshold = 5’ silently throws away the current value and replaces it — a bug that compiles and runs without error but produces wrong results. Augmented assignment makes the “update” intent explicit and prevents this.
Solution
# RAM monitor accumulates stats over one polling cycle.
# Starting values:
total_mb = 0
count = 0
weight = 1.0
threshold = 200 # alert threshold in MB — decreases by 5 each cycle
# Simulate one poll cycle: current usage = 120 MB
sample = 120
total_mb += sample # accumulate total
count += 1 # increment count
weight /= 2 # halve smoothing weight
threshold -= 5 # tighten the alert boundaryTests
def test_total_mb():
assert total_mb == 120
def test_count():
assert count == 1
def test_weight():
assert weight == 0.5
def test_threshold():
# threshold started at 200, should be 200 - 5 = 195
assert threshold == 195