Python sum() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in

Definition

In Python, sum(iterable) adds up the items of an iterable (a list, tuple, set, range, generator, ...) and returns the total. An empty iterable gives 0.

The optional start value is added to the total and is also what an empty iterable returns. Since Python 3.8 you can pass it by keyword: sum(values, start=10). sum() refuses strings; use "".join() for text.

sum() is a built-in function: it lives in the builtins module, so it is always available without an import.

Syntax

Pass any iterable of numbers, and optionally a start value.

python
total = sum(numbers)
total_plus = sum(numbers, start)
CallWhat it does
sum([1, 2, 3])Returns 6.
sum([1, 2, 3], 10)Returns 16: the start value is added.
sum(x * x for x in range(4))Returns 14 from a generator expression.
sum([])Returns 0, the default start.

A generator expression needs no extra brackets when it is the only argument: sum(x for x in data).

Library declarations
python
sum(iterable, /, start=0)

Parameters

ParameterDescription
iterableRequired. An iterable of numbers (or other objects that support +, except strings).
startOptional. The value the total starts from. Default 0. Keyword form allowed since Python 3.8.

Return Value

Return ValueDescription
numberstart plus every item. The type follows the items: ints give an int, any float gives a float.

Python Examples

Example 1 — Totals, Averages, and the start Value

Add a list of scores, compute an average with len(), and see what start and an empty list do.

python
scores = [88, 92, 78, 82]

print(sum(scores))
print(sum(scores) / len(scores))
print(sum(scores, 100))
print(sum([]))
print(sum([0.5, 0.25, 0.125]))

How It Works

  1. Plain total. 88 + 92 + 78 + 82 is 340.
  2. Average. Dividing the total by len(scores) with / always gives a float: 85.0.
  3. start is added. sum(scores, 100) begins at 100, so the result is 440.
  4. Empty and float input. An empty list returns the start value 0; floats like 0.5 and 0.25 add up to the float 0.875.

Example 2 — Sum Computed Values and Count Matches

Feed sum() a generator expression to total prices and to count items that pass a test.

python
orders = [
    {"item": "pen", "qty": 3, "price": 2},
    {"item": "book", "qty": 1, "price": 15},
    {"item": "bag", "qty": 2, "price": 30},
]

total = sum(o["qty"] * o["price"] for o in orders)
big_items = sum(1 for o in orders if o["price"] >= 15)
passed = sum([True, False, True, True])

print("Order total:", total)
print("Items costing 15 or more:", big_items)
print("True values counted:", passed)

How It Works

  1. Computed items. The generator yields 6, 15, and 60 (quantity times price), and sum() adds them to 81.
  2. No temporary list. A generator expression produces one value at a time, so memory use stays flat for big data.
  3. Counting with 1s. sum(1 for ... if ...) adds 1 for each order priced 15 or more: book and bag.
  4. True counts as 1. bool is a subclass of int, so summing booleans counts the True values.

Example 3 — Join Lists with start=[] and Avoid sum() for Strings

Use start=[] to flatten a short list of lists, see the string error, and get an accurate float total with math.fsum().

python
import math

nested = [[1, 2], [3], [4, 5]]
flat = sum(nested, start=[])
print(flat)

words = ["sum", "is", "for", "numbers"]
try:
    sum(words, "")
except TypeError:
    print("Use ''.join() for strings:", " ".join(words))

print(math.fsum([0.1] * 10))

How It Works

  1. Lists support +. Starting from an empty list, sum() concatenates [1, 2] + [3] + [4, 5].
  2. Fine for small data. Each + copies the list so far; for many lists, itertools.chain is faster.
  3. Strings are blocked. Even with "" as start, sum() raises TypeError and points you to join().
  4. Accurate floats. math.fsum() tracks the lost low-order bits, so ten 0.1 values total exactly 1.0.

Frequently Asked Questions

Call sum(my_list). It works with any iterable of numbers, including tuples, sets, ranges, and generator expressions.
It is added to the total and returned for an empty iterable. sum([1, 2], 10) returns 13.
sum() deliberately rejects strings because repeated + on strings is slow. Use "".join(words) or " ".join(words) instead.
Floats are binary approximations, so small errors add up. Python 3.12 made sum() of floats more accurate, and math.fsum() gives the most accurate result on all versions.
Use sum(my_dict.values()). sum(my_dict) would add the keys instead.

Next: super()

Call a method from the parent class.

super() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful