Incrementing variables with += is a concise, readable, and Pythonic shorthand for adding a value to a variable and assigning the result back to the same variable — equivalent to x = x + y but shorter and often clearer. The += operator (and its cousins -=, *=, /=, //=, %=, **=, &=, |=, ^=, <<=, >>=) work on numbers, strings, lists, and other types that support the corresponding operation. In 2026, these augmented assignment operators remain essential — they reduce repetition, improve performance slightly (fewer lookups), and make accumulators, counters, and running totals cleaner and less error-prone.
Here’s a complete, practical guide to incrementing with += (and related operators): basic usage, supported types, real-world patterns, performance notes, and modern best practices with type hints and safety.
For numbers, += adds and assigns — works with int, float, complex.
x = 5
x += 3 # x = x + 3
print(x) # 8
total = 0.0
total += 12.5 # Accumulate float
total += 7.3
print(total) # 19.8
Strings support += for concatenation — though join() is usually faster for many concatenations.
greeting = "Hello"
greeting += ", world!"
print(greeting) # Hello, world!
# Inefficient loop (quadratic time)
s = ""
for word in ["Python", "is", "great"]:
s += " " + word
print(s.strip()) # Python is great
# Better: use list + join
parts = []
for word in ["Python", "is", "great"]:
parts.append(word)
print(" ".join(parts)) # Python is great
Lists support += via extend() — adds elements in-place (faster than + when you don’t need a new list).
a = [1, 2, 3]
a += [4, 5] # extend in-place
print(a) # [1, 2, 3, 4, 5]
# Accumulate in loop
result = []
for i in range(5):
result += [i ** 2] # or result.append(i ** 2)
print(result) # [0, 1, 4, 9, 16]
Real-world pattern: counters, accumulators, running totals — += shines in loops for counting, summing, or building collections.
# Count even numbers
numbers = range(1, 21)
even_count = 0
for num in numbers:
if num % 2 == 0:
even_count += 1
print(f"Even count: {even_count}") # 10
# Running sum
total = 0
for num in numbers:
total += num
print(f"Total: {total}") # 210
Best practices make += usage clean, safe, and performant. Prefer += for accumulators/counters — clearer than x = x + y. Use extend() or += for lists — in-place is faster than + when building incrementally. Avoid += in string loops — use list.append() + join() to avoid quadratic time. Add type hints for clarity — x: int or total: float — improves readability and mypy checks. Modern tip: use Polars for large data aggregations — df.group_by("col").agg(pl.col("value").sum()) is 10–100× faster than Python loops. In production, use collections.Counter for frequency counting — += 1 is fine, but Counter is optimized. Combine with sum(), any(), all() — often eliminates loops entirely. Avoid mutable default arguments with += — can cause unexpected shared state.
Incrementing with += (and friends) makes accumulators, counters, and builders cleaner and slightly faster — a small syntax win with big readability and performance impact. In 2026, use it for numbers/lists, avoid string concatenation in loops, prefer Counter/sum for counting, and type hints for safety. Master augmented assignment, and you’ll write concise, efficient code that scales from small scripts to large data pipelines.
Next time you need to add to a variable repeatedly — reach for +=. It’s Python’s cleanest way to say: “Add this and keep the result here.”