sum()
sum(iterable, /, start=0) number · Updated March 13, 2026 · Built-in Functions The sum() function adds up all numeric items in an iterable and returns their total. By default, it starts from 0, but you can provide a custom starting value to offset the result. This is one of the most frequently used functions in Python for quick calculations.
Syntax
sum(iterable, /, start=0)
The forward slash (/) marks iterable as a positional-only parameter—you cannot use sum(iterable=my_list). The start parameter, however, can be passed by name.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
iterable | iterable | — | Any iterable containing numbers (int, float, or objects implementing __add__) |
start | number | 0 | The initial value to add to the sum. Defaults to 0. |
Returns: The sum of all items in the iterable plus the start value. Returns start if the iterable is empty.
Examples
Basic numeric sum
The simplest use case is adding up a list of numbers:
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print(total)
# 15
Using the start parameter
The start parameter lets you offset the result or use a non-zero starting point:
values = [1, 2, 3]
result = sum(values, start=10)
print(result)
# 16
This is useful when you need to add a base value to your sum, like calculating a running total or adding a baseline.
Summing floating-point numbers
Works with floats too, though for financial calculations you might want math.fsum():
prices = [19.99, 5.50, 3.00]
total = sum(prices)
print(total)
# 28.49
# With a discount applied as start
discount = -5.00
total_after_discount = sum(prices, start=discount)
print(total_after_discount)
# 23.49
Summing with a custom start type
The start parameter determines the return type when summing mixed types:
# Start with float to get float result
values = [1, 2, 3]
total = sum(values, start=0.0)
print(total)
# 6.0
# Start with int to keep int result
total_int = sum(values, start=0)
print(total_int)
# 6
Common Patterns
Calculating a running total
When processing items in a loop and tracking cumulative sums:
transactions = [150, 200, 50, 300, 100]
running = 0
for amount in transactions:
running = sum([running, amount])
print(f"Running total: {running}")
# Running total: 150
# Running total: 350
# Running total: 400
# Running total: 700
# Running total: 800
Counting items that meet a condition
Combine sum() with a generator expression to count matching items:
scores = [85, 92, 78, 90, 88, 76, 95]
passing = sum(1 for s in scores if s >= 80)
print(f"Passing scores: {passing}")
# Passing scores: 5
This works because True equals 1 and False equals 0 in Python.
Summing dictionary values
Get the total of all values in a dictionary:
inventory = {"apples": 50, "bananas": 30, "oranges": 45}
total_items = sum(inventory.values())
print(total_items)
# 125
Summing across multiple iterables
Use start with a tuple or list to aggregate multiple collections:
week_sales = [100, 150, 200]
weekend_sales = [300, 250]
total = sum(week_sales, start=sum(weekend_sales))
print(total)
# 1000
What sum() cannot do
For certain tasks, other approaches work better:
- String concatenation: Use
''.join()instead—sum(['a', 'b', 'c'])raises TypeError - Floating-point precision: Use
math.fsum()for better accuracy with decimals - Large iterables: Consider
itertools.accumulate()for memory-efficient running totals
See Also
- built-in::min — find the smallest item in an iterable
- built-in::max — find the largest item in an iterable