Inline operations (also called augmented assignment) are a concise, readable, and Pythonic shorthand for updating a variable by performing an operation and assigning the result back to the same variable. Instead of writing x = x + y, you write x += y — this works with arithmetic (+=, -=, *=, /=, //=, %=, **=), bitwise (&=, |=, ^=, <<=, >>=), and logical (and=, or= in some contexts) operators. In 2026, inline operations remain essential — they reduce repetition, improve performance slightly (fewer lookups), make accumulators/counters clearer, and are widely used in loops, data aggregation, string building, and production code for readability and efficiency.
Here’s a complete, practical guide to inline operations in Python: core operators with examples, supported types, real-world patterns, performance advantages, and modern best practices with type hints, safety, and scalability.
Arithmetic inline operations update numeric variables — work with int, float, complex, and even strings/lists for some operators.
x = 10
x += 5 # x = x + 5 ? 15
x *= 2 # x = x * 2 ? 30
x -= 7 # x = x - 7 ? 23
x /= 3 # x = x / 3 ? 7.666...
x //= 2 # x = x // 2 ? 3.0
x %= 2 # x = x % 2 ? 1.0
print(x) # 1.0
Bitwise inline operations update integers — useful for flags, bit manipulation, low-level optimization.
flags = 0b1100 # 12 in decimal
flags &= 0b1010 # bitwise AND ? 0b1000 (8)
flags |= 0b0110 # bitwise OR ? 0b1110 (14)
flags ^= 0b0101 # bitwise XOR ? 0b1011 (11)
flags <<= 1 # left shift ? 0b10110 (22)
flags >>= 2 # right shift ? 0b101 (5)
print(bin(flags)) # 0b101
String and list inline operations — += appends/concatenates in-place (strings create new objects, lists extend).
s = "Hello"
s += ", world!" # s = s + ", world!"
print(s) # Hello, world!
lst = [1, 2, 3]
lst += [4, 5] # lst.extend([4, 5])
print(lst) # [1, 2, 3, 4, 5]
Real-world pattern: accumulators, counters, running totals — inline ops make loops cleaner and more efficient.
# 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 inline operations safe, readable, and performant. Prefer inline ops for accumulators/counters — x += y clearer than x = x + y. Avoid += for strings in loops — use list accumulation + join() to prevent quadratic time. Modern tip: use Polars for large aggregations — pl.col("value").sum() is 10–100× faster than Python loops. Add type hints — x: int or total: float — improves readability and mypy checks. Use collections.Counter for frequency counting — += 1 is fine, but Counter optimized. Combine with sum(), any(), all() — often eliminates loops entirely. Avoid mutable default arguments with += — can cause shared state bugs. For bitwise flags, use enum.Flag — more readable than raw integers.
Inline operations with +=, *=, &=, etc. make updates concise, readable, and slightly faster — perfect for counters, accumulators, and in-place modifications. In 2026, use them for numbers/lists, avoid string += in loops, prefer Counter/sum for counting, and type hints for safety. Master inline ops, and you’ll write cleaner, more efficient code that scales from small scripts to large data pipelines.
Next time you need to update a variable with an operation — reach for inline ops. It’s Python’s cleanest way to say: “Apply this operation and keep the result here.”