Sum of numbers in the list is required everywhere. Python provide an inbuilt function sum() which sums up the numbers in the list.
Syntax:
sum(iterable, start) iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers. start : this start is added to the sum of numbers in the iterable. If start is not given in the syntax , it is assumed to be 0.
Possible two syntaxes:
sum(a) a is the list , it adds up all the numbers in the list a and takes start to be 0, so returning only the sum of the numbers in the list. sum(a, start) this returns the sum of the list + start
Below is the Python implementation of the sum()
# Python code to demonstrate the working of # sum() numbers = [1,2,3,4,5,1,4,5] # start parameter is not provided Sum = sum(numbers) print(Sum) # start = 10 Sum = sum(numbers, 10) print(Sum) |
Output:
25 35
Error and Exceptions
TypeError : This error is raised in the case when there is anything other then numbers in the list.
# Python code to demonstrate the exception of # sum() arr = ["a"] # start parameter is not provided Sum = sum(arr) print(Sum) # start = 10 Sum = sum(arr, 10) print(Sum) |
Runtime Error :
Traceback (most recent call last):
File "/home/23f0f6c9e022aa96d6c560a7eb4cf387.py", line 6, in
Sum = sum(arr)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
So the list should contain numbers
Practical Application: Problems where we require sum to be calculated to do further operations such as finding out the average of numbers.
# Python code to demonstrate the practical application # of sum() numbers = [1,2,3,4,5,1,4,5] # start = 10 Sum = sum(numbers) average= Sum/len(numbers) print average |
Output:
3
Recommended Posts:
- Python | Ways to sum list of lists and return sum list
- Sum 2D array in Python using map() function
- Map function and Dictionary in Python to sum ASCII values
- Prefix sum array in Python using accumulate function
- Numpy MaskedArray.sum() function | Python
- Python - Call function from another function
- Wand function() function in Python
- Returning a function from a function - Python
- How to use the NumPy sum function?
- Python | Maximum sum of elements of list in a list of lists
- Python List Comprehension to find pair with given sum from two arrays
- Python program to find Cumulative sum of a list
- Sum of list (with string types) in Python
- Python program to find sum of absolute difference between all pairs in a list
- Python | Pandas Series.sum()
- Python program to find sum of elements in list
- Python | Pandas Series.cumsum() to find cumulative sum of a Series
- Python program to find the sum of all items in a dictionary
- Python | Pandas dataframe.sum()
- numpy.sum() in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

