Python sum() Function
Description
sum() The method computes 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 value to add. If this value is not set, it defaults to 0.
Return Value
Returns the computed result.
Example
The following examples demonstrate the use of the sum function:
sum
(
[
0
,
1
,
2
]
)
3
sum
(
(
2
,
3
,
4
)
,
1
)
Add 1 after summing the tuple
10
sum
(
[
0
,
1
,
2
,
3
,
4
]
,
2
)
Add 2 after summing the list
12
Python built-in function