Incrementing variables +=
The += operator (augmented assignment) is one of the most useful and frequently used shortcuts in Python. It allows you to increment, add to, or update a variable in a clean, readable way. In data science and especially when working with dates and time, += is invaluable for building counters, accumulating time deltas, updating running totals, and tracking metrics over time.
TL;DR — How += Works
x += yis the same asx = x + y- Works with numbers, strings, lists, and many other types
- Makes code shorter and more readable
- Extremely common in loops and accumulators
1. Basic Usage of +=
# Simple numeric increment
counter = 0
counter += 1 # counter = counter + 1
counter += 5 # counter = 10
# With floating point (common in time calculations)
total_hours = 0.0
total_hours += 2.5
total_hours += 1.75
2. Real-World Data Science Examples in Dates and Time
import pandas as pd
from datetime import timedelta
df = pd.read_csv("sales_data.csv", parse_dates=["order_date"])
# Example 1: Accumulating total days since first order
first_order = df["order_date"].min()
days_since_first = 0
for row in df.itertuples():
days_since_first += (row.order_date - first_order).days
# ... process row
# Example 2: Building a running time counter
running_total_minutes = 0
for row in df.itertuples():
duration = (row.end_time - row.start_time).total_seconds() / 60
running_total_minutes += duration
# Example 3: Incrementing date counters
date_counter = {}
for row in df.itertuples():
date_str = row.order_date.date().isoformat()
date_counter[date_str] = date_counter.get(date_str, 0) + 1
3. Other Useful Augmented Assignments
# Common in data science
total += value
count += 1
price *= 1.1 # multiply
discount /= 2 # divide
remainder %= 7 # modulo
4. Best Practices in 2026
- Use
+=for all simple increments and accumulations - Combine with
defaultdict(int)orCounterfor cleaner counting - Use in loops when building running totals or time-based counters
- Keep code readable — avoid chaining too many operations on one line
- Works beautifully with pandas when updating derived columns
Conclusion
The += operator is a small but mighty tool that makes Python code cleaner and more efficient. In data science and date/time analysis, it is used constantly for accumulating totals, counting occurrences, tracking time deltas, and updating running metrics. Mastering augmented assignment operators like +=, *=, and /= will make your feature engineering and data processing code more professional and Pythonic.
Next steps:
- Review your current loops and counters and replace manual
x = x + 1patterns with the cleaner+=operator