On this page
sum
Prev: str | Up: __builtins__ | Next: super
Rel: abs, divmod, max, min, pow
Ref: docs.python.org — sum
Tags: #public #python3 #builtins
Import & Availability
# Built-in (available globally without import)
from builtins import sum
Overview & Signature
sum(iterable, /, start=0)
Return the sum of a 'start' value (default: 0) plus an iterable of numbers When the iterable is empty, return the start value.
Usage
assert sum([1, 2, 3, 4]) == 10
assert sum([1, 2, 3], start=10) == 16
Gotchas
- Do NOT use
sum()to concatenate strings or lists:sum(["a", "b"], "")raisesTypeError(use"".join()), andsum(list_of_lists, [])is $O(n^2)$ (useitertools.chain.from_iterable()).
REPL Inspect
help(sum)
Linked from
Related notes
- tuts/python3/__builtins__/__builtins__ Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/str Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/super Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__ links to this page; shared tags: #builtins
- tuts/python3/__builtins__/abs Linked here; shared tags: #builtins
- tuts/python3/__builtins__/divmod Linked here; shared tags: #builtins
- tuts/python3/__builtins__/max Linked here; shared tags: #builtins
- tuts/python3/__builtins__/min Linked here; shared tags: #builtins
- tuts/python3/__builtins__/pow Linked here; shared tags: #builtins
- tuts/c/builtins shared tags: #builtins