Python sum() Function | Tutorial
Description
The sum() method calculates the sum of items in an iterable.
Syntax
Here is the syntax for the sum() method:
sum(iterable[, start])
Parameters
- iterable -- An iterable object, such as a list, tuple, or set.
- start -- A value to be added to the sum. If not specified, it defaults to 0.
Return Value
Returns the calculated sum.
Examples
The following examples demonstrate the use of the sum() function:
>>> sum([0, 1, 2])
3
>>> sum((2, 3, 4), 1) # tuple sum plus 1
10
>>> sum([0, 1, 2, 3, 4], 2) # list sum plus 2
12
YouTip