Python sum()
Clear, original guide to python sum(): syntax, examples for lists/tuples/bytes/complex, start parameter, math.fsum for precision, and common TypeError issues.
Drake Nguyen
Founder · System Architect
Overview of python sum()
The python sum() built-in function computes the total of numeric items in an iterable. It is commonly used to add elements from lists, tuples, bytes-like sequences, and other numeric iterables. This guide explains the syntax, common use cases, and pitfalls such as floating-point precision and the "sum() takes no keyword arguments" error.
Syntax and start parameter
Basic syntax:
sum(iterable[, start])
The optional start parameter defaults to 0. When provided, the function returns the sum of start and all values in the iterable — for example, using python sum() with start parameter adds that initial value before accumulating the iterable.
Examples: python sum list of numbers
Summing a list of integers:
s = sum([1, 2, 3])
print(s) # 6
# Using the start parameter
s = sum([1, 2, 3], 10)
print(s) # 16
Iterables supported: tuple, bytes, bytearray
sum() accepts any iterable of numbers. Examples include tuples and bytes-like objects:
# bytes
print(sum(bytes([1, 2]))) # 3
# bytearray with a start value
print(sum(bytearray([1, 2]), 10)) # 13
# tuple including integer literals in different bases
print(sum((1, 0b11, 0o17, 0xFF))) # 274
print(sum((1, 0b11, 0o17, 0xFF), 0xF)) # 289
Floating point values and precision
sum() works with floats, but standard floating-point accumulation can introduce rounding errors. For higher precision when summing many floating values, prefer math.fsum which reduces rounding error:
import math
print(sum([1.5, 2.5, 3])) # 7.0
# Use math.fsum for better precision on many small floats
print(math.fsum([0.1] * 10)) # more accurate than sum([0.1]*10)
Complex numbers
sum() can accumulate complex numbers; the start parameter can also be a complex value:
print(sum([1 + 2j, 3 + 4j])) # (4+6j)
print(sum([1 + 2j, 3 + 4j], 2 + 2j)) # (6+8j)
print(sum([1 + 2j, 2, 1.5 - 2j])) # (4.5+0j)
Common errors and gotchas
- TypeError: sum() takes no keyword arguments — passing the start value as a keyword (for example
sum([1,2,3], start=10)) triggers this error because sum() accepts positional arguments only. - Incompatible types — sum() expects numeric types. Trying to concatenate strings with sum (e.g.,
sum(['a', 'b'])) will raise a TypeError; use''.join()for string concatenation. - Start value type — the start parameter should be compatible with the iterable elements; supplying a mismatched type can cause errors.
When to use sum() and alternatives
- Use
python sumfor straightforward numeric aggregation of lists, tuples, bytes, and bytearray values. - For many floating-point values where precision matters, use
math.fsum(math fsum vs sum python). - For non-numeric concatenation (strings, lists), prefer specific functions like
str.joinoritertools.chaininstead of sum().
Quick reference and tips
- Primary usage:
sum(iterable)orsum(iterable, start). - Accepts many iterable types: lists, tuples, bytes, bytearray, and iterators of numbers.
- Does not accept keyword arguments — always pass start positionally.
- For complex numbers or mixed numeric types, ensure the start value is compatible.
Official guidance: consult the Python library reference for the built-in sum() function and math.fsum for details on behavior and numeric precision.
Related examples to try
- python sum[] list: sum a list of floats and compare with math.fsum results.
- python sum of tuple values: pass a tuple of integers and experiment with different start values.
- python sum bytes bytearray: sum small byte sequences to see how bytes are interpreted as integers.