Python sum() function
Python sum() Function
Description
The sum() function returns the sum of all numeric items in any iterable (list or tuple). By default, the optional start argument is 0. If a start value is given, the numbers in the list are added to the start value.
Syntax
sum(iterable, start)
Parameters
x : an iterable with numeric operands
start: initial value of sum
Return Value
This function returns the sum of the numeric operands in the iterable.
Example
x = [10,20,30]
total = sum(x)
print ("x: ",x, "sum(x): ", total)
x = (10, -20, 10)
total = sum(x)
print ("x: ",x, "sum(x): ", total)
x = [10,20,30]
start = 5
total = sum(x, start)
print ("x: ",x, "start:", start, "sum(x, start): ", total)
This will produce the following output:
x: [10, 20, 30] sum(x): 60
x: (10, -20, 10) sum(x): 0
x: [10, 20, 30] start: 5 sum(x, start): 65