Have you ever written code like this?
total = total + price
It works, but it’s a bit long and repetitive. Python has a better way: the += operator.
The += operator adds a value to a variable and saves the result in the same variable, all in one step. It makes your code shorter, cleaner, and easier to read.
In this guide, you’ll learn how += operator works, see examples for different data types, and discover practical ways to use it in Python.
The += operator combines addition and assignment into one step. For example:
# Increment count by 5
count = 10
count += 5
print(count)
Output:
Here’s what happens step by step:
So instead of repeating the variable name like count = count + 5, you can use += to make it cleaner.
For numbers, += simply adds the right-hand value to the left-hand variable.
# Increment score by 25
score = 100
score += 25
print(score)
Output:
Explanation:
This is helpful when you want to keep a running total, like tracking points in a game or calculating expenses.
When used with strings, += joins one string to another.
# Append text to greeting
greeting = "Hello"
greeting += ", Python!"
print(greeting)
Output:
Explanation:
Note: Strings are immutable in Python, so every time you use += with strings, a new string is created in memory. For short strings or a few additions, this is fine. For long loops, consider using a list and join() for better performance.
With lists, += adds elements from another list.
# Extend todo_list with new tasks
todo_list = ["code", "test"]
todo_list += ["debug", "deploy"]
print(todo_list)
Output:
Explanation:
This is very handy when you want to combine multiple lists quickly, like merging inventory or task lists.
Note: You cannot use += directly on tuples because tuples are immutable.
# Calculate total cost of prices
total_cost = 0
prices = [2.99, 4.50, 1.25]
for price in prices:
total_cost += price
print(f"Total: ${total_cost:.2f}")
Output:
Explanation:
This is much cleaner than writing total_cost = total_cost + price for each item.
# Add points if level completed
player_points = 0
level_completed = True
if level_completed:
player_points += 50
print(player_points)
Output:
Explanation:
This is useful when you want to update a value only under certain conditions.
# Extend inventory with new items
inventory = ["laptop", "mouse"]
new_items = ["keyboard", "monitor"]
inventory += new_items
print(inventory)
Output:
Explanation:
This makes it simple to merge lists without writing long code.
# Error: cannot add string to integer
count = 10
count += "5"
Why it fails: Python cannot add a number (int) and a string.
Fix: Convert the string to a number first:
# Convert string "5" to int before adding
count += int("5")
output:
Always check your variable types when using +=.
# Slow approach: concatenating strings in a loop
log = ""
for i in range(1000):
log += str(i) # Slow for large loops
# Better approach: use a list and join
log_parts = []
for i in range(1000):
log_parts.append(str(i))
log = "".join(log_parts)
Explanation: Using += repeatedly with strings creates many temporary strings in memory. Using a list and join() is faster for large loops.
1. Use for clarity: Only use += where it makes code simpler.
2. Break complex calculations into steps:
# Hard to read
distance = speed * time
distance += acceleration * time ** 2 / 2
# Easy to read
base = speed * time
adjust = acceleration * time ** 2 / 2
distance = base + adjust
3. Use with loops and lists: Great for totals, concatenation, and combining items.
Convert an integer to a string in Python
Python switch statement
JavaScript vs Python
The += operator is small but powerful. It:
Next time you see x = x + y, think about replacing it with x += y. It keeps your Python code clean and simple.
Get free consultation for your digital product idea to turn it into reality!
Get Started