How to use the sum() function in Python and Examples
In this tutorial, you will learn how to use the sum function in Python. The sum() function adds all the numbers in an iterable element, such as a list, tuple, set, or dictionary, and returns the sum.
This tutorial shows you how to sum a list of elements or numbers, sum a list of lists, and sum a tuple containing numbers.
The sum() Function Syntax
The syntax of the sum() function is as follows:
sum(iterable, start=0)
The sum() function sums the value of the start argument and items of the iterate table from left to right and returns the sum value.
The sum() Function Parameters
- Iterable: A required field. It can be a list, a tuple, a dictionary, etc., but the items in an iterate are usually numbers.
- Start: The value to add to the sum of iterable items. If omitted, it defaults to 0. It is optional.
Python sum() Examples
Example 1: Add up the numbers in a list
This example shows a sample program that uses the sum() function to sum a list of numbers.
The following illustrates an example of a simple program that adds all numbers in a list and returns total:
list = [100, 300, 200, 300, 100, 400, 240, 120, 430] total = sum(list) print("The total is", total)
Output:
The total is 2190
As you can see, it takes only one line of code to sum a list of numbers in Python.
Here is another way to get the same result without using the sum() function.
Let's take a look at an example:
#sum a list of numbers using for loop list = [100, 300, 200, 300, 100, 400, 240, 120, 430] total = 0 for l in list: total += l print("The total is", total)
Here, we used a for loop statement to iterate through the list and add each number to the variable named total. You can also use a while loop statement to get the same result.
Example 2: Adding numbers in a tuple
The following sample code adds up all the numbers in a tuple and returns the total value:
tuple = (240, 320, 120, 230, 130, 140, 440, 220, 420) total = sum(tuple) print("The total is", total)
Output:
The total is 2260
Example 3: Add all the numbers in a list and other numbers
It is also possible to add all the numbers in the list and add another number.
The following sample code adds all the numbers in the list, adds 50, and returns the sum:
list = [200.40, 100, 400, 200.50, 400.40, 300.20] total = sum(list, 50) print("The total is", total)
Output:
The total is 1651.5
Example 4: Add all dictionary values
The sample code below adds all the dictionary values and returns the total:
dictionary = { "item_001": 100, "item_002": 200, "item_003": 300, "item_004": 400 } total = sum(dictionary.values()) print("The total is", total)
Output:
The total is 1000
Example 5: Sum of two lists
This example shows how to sum multiple lists in Python.
The following example shows how to sum two lists of numbers:
#sum two lists list1 = [20, 10, 30, 40, 50] list2 = [60, 80, 70, 90, 100, 110] total = sum(list1) + sum(list2) print("The total is", total)
Example 6: Sum a list of lists
The following example shows a program that sums a list of lists:
#sum a list of lists total = 0 list1 = [1, 2, 3, 4, 5] list2 = [6, 7, 8, 9, 10, 11] list3 = [12, 13, 14, 15, 16, 17] list =[list1, list2, list3] for l in list: total += sum(l) print("The total is", total)
Here, as you can see, we are adding all the numbers in the list using the for loop statement and the sum() function.
Summary
In this tutorial, you have learned how to use the Python sum() function, which adds up all the values in an iterable element such as a list, tuple, set, or dictionary and returns the sum.
You can use the sum() function to sum or add all elements in a list and sum a list of lists.