Python sum() Function
Description
sum()The method calculates the sum of a sequence.
Syntax
The following is the syntax of the sum() method:
sum(iterable[, start])
Parameters
- iterable -- An iterable object, such as a list, tuple, or set.
- start -- Specifies the parameter to add. If this value is not set, it defaults to 0.
Return Value
Returns the calculation result.
Examples
The following shows examples of using the sum function:
>>>sum([0,1,2])
3
>>> sum((2, 3, 4), 1) # Calculate the sum of the tuple and then add 1
10
>>> sum([0,1,2,3,4], 2) # Calculate the sum of the list and then add 2
12
Python Built-in Functions