Module 4 · Operators — topic 11 of 16
Assignment operators
The full family of augmented assignment operators (+=, -=, *=, /=, //=, %=, **= and more), how they work with numbers, strings and lists, and the in-place behaviour of mutable objects.
Learning objectives
- Use every augmented assignment operator
- Explain how += differs for lists and strings
- Write counters and accumulators concisely
Definition
The full family of augmented assignment operators (+=, -=, *=, /=, //=, %=, **= and more), how they work with numbers, strings and lists, and the in-place behaviour of mutable objects.
Introduction
Updating a variable based on its own value is so common that Python has a shorthand for every arithmetic operator: x += 5 instead of x = x + 5, x *= 2 instead of x = x * 2, and so on.
Why it matters
Counters, running totals and repeated scaling appear in nearly every program. The shorthand is shorter, avoids repeating the variable name (and misspelling it), and states the intent “update this value” clearly.
Detailed explanation
The variable must already exist; count += 1 on an undefined name raises NameError.
For immutable values such as numbers and strings, += creates a new object and rebinds the name.
For lists, += extends the existing list in place, like list.extend(). Other names referring to the same list see the change.
x /= y always makes x a float, even if both were ints.
Python has no ++ or -- operators; use += 1 and -= 1.
Syntax
- The left side must already have a value.
- The operation uses the current value, then rebinds the name.
- There is no ++ in Python.
Code examples
A running total
866
Strings and lists
Hello, world [1, 2, 3, 4]
More examples
Counters, accumulators and shared lists
letters: 35 long words: 3 alias sees: ['pen', 'ink'] items item
Step-by-step explanation
- total_letters accumulates the length of every word.
- long_words counts words longer than four letters: quick, brown and jumps.
- cart += ["ink"] extends the same list object, so alias sees the change.
- label += "s" creates a new string, so copy_of_label keeps the old value.
Expected output
Running “Counters, accumulators and shared lists” prints exactly:
Practical applications
- Web servers count requests with counter += 1.
- Games update positions with x += speed each frame.
- Compound interest loops use amount *= 1 + rate.
Best practices
- Initialise counters and totals before loops.
- Prefer augmented assignment for updates.
- Remember += on lists changes the shared list.
Common mistakes
Using ++
Avoid
Prefer
Python has no increment operator; count++ is a SyntaxError.
Augmenting an undefined variable
Avoid
Prefer
Initialise accumulators before the loop.
Key takeaways
- x op= y means x = x op y for every arithmetic operator.
- += extends lists in place but creates new strings and numbers.
- Python has no ++ or --.
Try it yourself
- 1Start with 1 and use *= in a loop to compute 10 factorial.
- 2Count the vowels in a sentence with a counter and +=.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?