Python sum() function
Python sum() function
The sum() function in python sums start and the items of an iterable from left to right and returns the total.
Syntax
sum(iterable[, start])
Parameter
iterable: This parameter represents the sequence to sum.
start: It is optional parameter that represents a value that is added to the return value
Return
This function returns the sum of all items in an iterable.
Example 1
# Python Program explaining
# the sum() function
num= [11,12,13,14,15,11,14,15]
# start parameter is not provided
Sum = sum(num)
print("The sum of iterables is",Sum)
# start = 100
Sum = sum(num, 100)
print("Adding 10 to the sum: ",Sum)
Output
The sum of iterables is 105 Adding 10 to the sum: 205
Example 2
# Python Program explaining
# the sum() function
num = [1,2,3,4,5,1,5]
Sum = sum(num)
avg= Sum/len(num)
print ("The average value is: ",avg)
Output
The average value is: 3.0
