Skip to content

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.

Beginner 10 min read·Lesson 54 of 306 in Python Programming

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

OperatorEquivalent to
x += yx = x + y
x -= yx = x - y
x *= yx = x * y
x /= yx = x / y
x //= yx = x // y
x %= yx = x % y
x **= yx = x ** y
x &= y, x |= y, x ^= ybitwise updates
x <<= y, x >>= yshift updates

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

Python
count += 1
total -= discount
price *= 1.18
value //= 10
  • 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

Python
balance = 1000
balance += 500
balance -= 200
balance *= 2
balance //= 3
print(balance)
Output
866

Strings and lists

Python
message = "Hello"
message += ", world"
print(message)
items = [1, 2]
items += [3, 4]
print(items)
Output
Hello, world
[1, 2, 3, 4]

More examples

Counters, accumulators and shared lists

Python
words = "the quick brown fox jumps over the lazy dog".split()
long_words = 0
total_letters = 0
for w in words:
total_letters += len(w)
if len(w) > 4:
long_words += 1
print("letters:", total_letters, "long words:", long_words)
cart = ["pen"]
alias = cart
cart += ["ink"]
print("alias sees:", alias)
label = "item"
copy_of_label = label
label += "s"
print(label, copy_of_label)
Output
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:

TextOutput
letters: 35 long words: 3
alias sees: ['pen', 'ink']
items item

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

count++

Prefer

count += 1

Python has no increment operator; count++ is a SyntaxError.

Augmenting an undefined variable

Avoid

for n in nums:
total += n

Prefer

total = 0
for n in nums:
total += n

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

  1. 1
    Start with 1 and use *= in a loop to compute 10 factorial.
  2. 2
    Count 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?
Practise this topic