Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

sum()

Accumulates numeric input starting at the supplied initial value.

Version: Python 3.14; repository image, production runtime not attested

Syntax

sum(iterable, /, start=0)

Accumulates numeric input starting at the supplied initial value.

When input is empty an admissible start value is returned unchanged; a nonzero start contributes to a nonempty total as well.

The optional start is an initial addend. With the example input the total is 10+2+4+8 = 24. If no items arrive, the result is the initial 10, or the default integer zero. An inadmissible start, including str, bytes or bytearray, raises TypeError even for empty input; join is the string-concatenation operation.

assert sum([2, 4, 8], 10) == 24 and sum([], start=10) == 10
assert sum([]) == 0

Examples

print(sum([2, 4, 8], 10))
print(sum([], 10))

Result

24
10

The optional start is an initial addend. With the example input the total is 10+2+4+8 = 24. If no items arrive, the result is the initial 10, or the default integer zero. An inadmissible start, including str, bytes or bytearray, raises TypeError even for empty input; join is the string-concatenation operation.

Sources