Adds up all the numbers in a list.
Synopsis
sum(list)
Description
sum adds every item, starting from 0. For an empty list it returns 0.
Parameters
| Name | Meaning |
|---|---|
list |
A list (or range) of numbers. |
Return value
The total, an int or a float.
Example
print(sum([1, 2, 3]))
print(sum(range(1, 101)))
print(sum([]))
It prints:
6
5050
0
Common mistake
print(sum(["a", "b", "c"]))
Python stops with:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
sum adds numbers only. To join strings, use "".join(...).
See also
Last updated 2 weeks ago