sum()
The built-in sum() function provides a Pythonic way to add together the items of an iterable. It efficiently computes the total sum of numeric values, with the option to include an initial value for the summation process:
>>> sum([1, 2, 3, 4, 5])
15
>>> sum([1, 2, 3, 4, 5], start=100)
115
sum() Signature
sum(iterable, /, start=0)
Arguments
| Argument | Description | Default Value |
|---|---|---|
iterable |
Any Python iterable containing numeric values. It can also hold concatenable objects, such as lists and tuples, but only when start is a matching empty container. |
Required argument |
start |
An optional argument to initialize the summation. It can be a number, list, or tuple, but not a string, bytes, or bytearray. CPython rejects those with a dedicated error pointing to ''.join() or b''.join() instead. |
0 |
Concatenating lists or tuples requires an explicit start. The default start=0 makes the first addition int + list, so sum([[1], [2]]) raises TypeError: unsupported operand type(s) for +: 'int' and 'list'. Passing a matching empty container fixes it:
>>> sum([[1], [2]], start=[])
[1, 2]
Return Value
- The sum of the
startvalue and the values in the input iterable from left to right. - The return type follows Python’s usual numeric coercion rules rather than the type of the input elements. A mix of types promotes to the wider type, so
sum([1, 2.0])returns3.0. - Booleans sum to an
int, sosum([True, True])returns2. - An empty iterable returns the
startvalue unchanged, sosum([])returns0.
sum() Examples
With a list of integers as an argument:
>>> sum([1, 2, 3, 4, 5])
15
With a tuple of floating-point numbers as an argument:
>>> sum((10.5, 20.3, 30.2))
61.0
Since Python 3.12, sum() uses an algorithm that gives higher accuracy when summing floats, and Python 3.14 extends that algorithm to complex numbers. The Python documentation still points to fsum() in the math module for extended precision, to ''.join(sequence) for concatenating a sequence of strings, and to chain() in itertools for concatenating a series of iterables.
With a comprehension that generates square values:
>>> sum([x ** 2 for x in range(1, 6)])
55
With a starting value:
>>> sum([1, 2, 3], start=10)
16
With a list of Decimal values:
>>> from decimal import Decimal
>>> sum([Decimal("10.2"), Decimal("12.5"), Decimal("11.8")])
Decimal('34.5')
With a list of rational numbers:
>>> from fractions import Fraction
>>> sum([Fraction(51, 5), Fraction(25, 2), Fraction(59, 5)])
Fraction(69, 2)
sum() Common Use Cases
The most common use cases for the sum() function include:
- Calculating the total of a series of numeric values.
- Summing numeric values as part of a larger mathematical computation.
sum() Real-World Example
Say you’re tracking daily sales figures across different stores and want to calculate the total sales for the week. Here’s how you can do it using the sum() function:
>>> weekly_sales = [
... [100, 150, 200], # Monday
... [80, 120, 160], # Tuesday
... [130, 170, 210], # Wednesday
... [90, 110, 140], # Thursday
... [200, 250, 300] # Friday
... ]
>>> sum(sum(daily_sales) for daily_sales in weekly_sales)
2410
In this example, sum() helps to efficiently calculate the total sales by first summing each day’s sales figures and then aggregating those daily totals.
Related Resources
Tutorial
Python's sum(): The Pythonic Way to Sum Values
In this step-by-step tutorial, you'll learn how to use Python's sum() function to add numeric values together. You also learn how to concatenate sequences, such as lists and tuples, using sum().
For additional information on related topics, take a look at the following resources:
- Summing Values the Pythonic Way With sum() (Course)
- Lists vs Tuples in Python (Tutorial)
- Numbers in Python (Tutorial)
- Representing Rational Numbers With Python Fractions (Tutorial)
- Python range(): Represent Numerical Ranges (Tutorial)
- Lists and Tuples in Python (Course)
- Lists vs Tuples in Python (Quiz)
- The Python range() Function (Course)