sum() is a built-in Python function that computes the sum of all items in an iterable (like lists, tuples, ranges, generators, or NumPy arrays) — optionally starting from an initial value. It’s fast, memory-efficient, and works with any numeric type (int, float, complex) or objects with __add__. In 2026, sum() remains essential in data science (total magnitudes, cumulative sums in pandas/Polars/Dask), scientific computing (vector sums, weighted averages), and algorithms (prefix sums, dot products) — outperforming manual loops and list comprehensions for large data.
Here’s a complete, practical guide to using sum() in Python: basic summation, start value, real-world patterns (earthquake total energy, weighted scores, cumulative sums), and modern best practices with type hints, performance, edge cases, and integration with NumPy/Dask/Polars/pandas/xarray.
Basic sum(iterable, start=0) — sum elements with optional starting value.
# Simple list sum
print(sum([1, 2, 3, 4, 5])) # 15
# With start value
print(sum([1, 2, 3], 10)) # 16 (10 + 1+2+3)
# Float sum
print(sum([1.5, 2.5, 3.5])) # 7.5
# Empty iterable
print(sum([])) # 0 (default start)
print(sum([], 100)) # 100
Summing with NumPy/Dask/Polars — vectorized & parallel alternatives.
import numpy as np
import dask.array as da
import polars as pl
arr = np.array([1, 2, 3, 4, 5])
print(np.sum(arr)) # 15 (faster for arrays)
darr = da.from_array(arr, chunks=2)
print(darr.sum().compute()) # 15 (parallel)
pl_df = pl.DataFrame({"mag": [7.2, 6.8, 5.9]})
print(pl_df["mag"].sum()) # 19.9
Real-world pattern: earthquake magnitude & energy summation — total & weighted sums.
import pandas as pd
df = pd.read_csv('earthquakes.csv')
# Total magnitude sum
total_mag = sum(df['mag'])
print(f"Total magnitude sum: {total_mag:.1f}")
# Approximate total energy (10^(1.5 * mag + 4.8))
total_energy = sum(10 ** (1.5 * mag + 4.8) for mag in df['mag'])
print(f"Total radiated energy: {total_energy:.2e} Joules")
# Weighted sum (e.g., magnitude × depth)
weighted = sum(m * d for m, d in zip(df['mag'], df['depth']))
print(f"Weighted sum (mag × depth): {weighted:.1f}")
# Polars: columnar sum
import polars as pl
pl_df = pl.from_pandas(df)
total_pl = pl_df["mag"].sum()
print(f"Polars total mag: {total_pl}")
Best practices for sum() in Python & data workflows. Prefer sum(iterable) — over manual loops for clarity & speed. Modern tip: use Polars pl.col('mag').sum() — fastest columnar sum; Dask ddf['mag'].sum().compute() for distributed. Use start — when accumulating: total = sum(lst, initial). Add type hints — def total_mag(mags: list[float]) -> float: return sum(mags). Avoid sum() on large generators — materializes everything; use sum(gen) carefully. Use sum(1 for _ in iterable) — count items without list. Use sum() with map() — sum(map(int, lines)) for total from file. Use sum() with filter() — sum(filter(lambda x: x > 0, lst)) for positive sum. Use sum() in comprehensions — sum(x**2 for x in lst) for squared sum. Use np.sum() — for NumPy arrays (faster). Use ddf.sum().compute() — Dask reduction. Use pl.sum_horizontal() — Polars row-wise sum. Use sum() in assertions — assert sum(lst) == expected. Use sum() with enumerate() — sum(i * v for i, v in enumerate(lst)) for weighted sum. Use sum() with zip() — sum(a * b for a, b in zip(lst1, lst2)) for dot product. Use sum() in generators — sum(gen()) for lazy computation. Use sum() with itertools.accumulate() — for prefix sums. Use sum() in reduce — but prefer sum for addition. Use sum() with complex — sum(complex_nums). Use sum() with fractions — exact arithmetic.
sum(iterable, start=0) computes the total of all items — numeric types, custom objects with __add__, or via generator expressions. In 2026, use for totals, weighted sums, counts, and integrate with pandas/Polars/Dask/NumPy for vectorized reductions. Master sum(), and you’ll calculate aggregates efficiently and expressively in any numeric or data workflow.
Next time you need the total — use sum(). It’s Python’s cleanest way to say: “Add up everything in this collection — fast and simple.”