Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Function

sum

sum() adds the items of an iterable from left to right, starting with an optional initial value. It returns the resulting total and is intended primarily for numeric items.

How it works

sum() consumes an iterable and combines its items from left to right, beginning with start, which defaults to 0. It returns the resulting total; the items are normally numeric and start must not be a string.

Syntax

sum(iterable, /, *, start=0)

Parameters

NameTypeDescription
iterableiterableAn iterable whose items are added.
startobjectInitial value added before the iterable's items; defaults to 0 and cannot be a string.

Returns

The total of start and the iterable's items.

Key points

  • The calculation begins with start and incorporates each item in iteration order.
  • Use sum() primarily for iterables containing numeric values.
  • A string cannot be used as the initial value.

Common mistakes

  • Do not pass a string as start; the documented function rejects that type of initial value.

Example: Sum values with an initial total

numbers = [1, 2, 3]
total = sum(numbers, start=10)
print(total)  # 16

Adds the iterable's values from left to right, beginning with the supplied starting value.

When to use it

Aggregate numeric values

Compute a total from values held in a list, tuple, or other iterable.

Add to an initial total

Begin an accumulation with an existing offset or subtotal.

Sum an iterable

Consume generated values without first materializing them into a collection.

Interview questions

QWhat does Python's sum() do?

sum() accepts an iterable and an optional keyword-only start value that defaults to 0. It adds the start value and iterable items from left to right, then returns the total.

Follow-up: What does sum([], start=10) return? It returns 10 because the empty iterable contributes no items and the initial value remains.

QWhat kinds of inputs does sum() expect?

The iterable's items are normally numbers, so numeric values can be accumulated directly. The function processes them in iteration order rather than requiring a sequence.

Follow-up: Can a generator be passed? Yes: because the parameter is an iterable, a generator can supply the items as it is consumed.

QHow does the start argument affect the result?

start is optional and defaults to 0, and it must not be a string. Supplying another suitable initial value lets the total begin from a nonzero value.

Follow-up: Why is an explicit start useful? It lets the caller include an initial offset without separately adding it after the iterable is summed.

  • math.fsum()
  • iter()

Source: Built-in functions. Checked 2026-10-15.