Updating in place (+=, -=…)
`x += 1` is the short way to write `x = x + 1`: it takes the current value, applies the operation and stores it back in the same name.
Used in 35 lessons of the course, from 00.03.
An augmented assignment operator combines an operation with an assignment. counter += 1 means: read counter, add 1 to it and store the result back in counter.
There is one for several operators: +=, -=, *=, /=, //=, %=, **=. The name must already exist, because its value is read first.
It is written this way so you don't repeat the name twice. It is shorter and makes it clear you are updating something, not creating a new value.
It also works with strings (+= concatenates) and lists (+= adds items to the same list). In the course you will mostly see it in counters, like counter += 1 inside a loop, and to build a text piece by piece.
total = 10
total -= 3 # total is now 7
Reach for it when: you want to change a value based on its current value: counting, accumulating a total or appending text.
Examples
Common mistake
count += 1 print(count)
Why: `+=` first reads the value of `count`, but that name does not exist yet.
How to write it: Give it a starting value first: count = 0 and then count += 1.
Common mistake
x = 5 x =+ 1 print(x)
Why: `=+` is not an operator: it is an assignment of `+1`. The old value is lost.
How to write it: Put the operator before the equals sign: x += 1.
