
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
sum() function in Python
In this tutorial, we are going to learn about the sum() function.
The function sum() is used to sum all the numbers in an iterable. Let's see some examples.
Example
# initialinzing a list numbers = [1, 2, 3, 4, 5] # printing the sum print(sum(numbers))
Output
If you run the above code, then you will get the following result.
15
The sum() takes one optional argument i.e., start that will be added to the result. Let's see it.
Example
# initialinzing a list numbers = [1, 2, 3, 4, 5] # printing the sum print(sum(numbers, 5))
Output
If you run the above code, then you will get the following result.
20
If we place any string or any other data type inside the iterable, then we will get an error. Let's see it with the following example.
Example
# initialinzing a list numbers = [1, 2, 3, [1, 2, 3], '5'] # printing the sum print(sum(numbers, 5))
Output
If you run the above code, then you will get the following result.
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-10-40c38246060a> in <module> 3 4 # printing the sum ----> 5 print(sum(numbers, 5)) TypeError: unsupported operand type(s) for +: 'int' and 'list'
Conclusion
If you have any doubts in the tutorial, mention them in the comment section.
- Related Articles
- Prefix sum array in python using accumulate function
- Sum 2D array in Python using map() function
- Map function and Dictionary in Python to sum ASCII values
- Evaluate the sum of rows using the eval() function – Python Pandas
- Combination Sum in Python
- Two Sum in Python
- Path Sum in Python
- Calendar function in Python
- Function Decorators in Python?
- Help function in Python
- Union() function in Python
- issubset() function in Python
- ldexp() function in Python
- Iterator function in Python
- Function Annotations in Python

Advertisements