Python sum() Function with Examples


How to Use the Python sum() Function

In this tutorial, you'll learn how to use the sum function in Python. The sum() function adds all numbers in an iterable, such as a list, tuple, set, or dictionary, and returns the sum.

The sum() Function Syntax

The syntax of the sum() function is as follows:

sum(iterable, start=0)

The sum() function sums start and the items of an iterable from left to right and returns the total.

The sum() Function Parameters

  • Iterable: Required. It can be a list, tuple, or dictionary, but iterable’s items are normally numbers.
  • Start: Optional. The value that is added to the sum of items of the iterable. It defaults to 0 if omitted.

Python sum() Examples

Example 1: Adding Numbers in a List

In this example, I show you examples of sample programs that sum a list of numbers using sum().

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)

Result:

The total is 2190

As you can see, to sum a list of numbers in Python, you need to write only one line of code.
Now I show you another way that can archive the same result without using sum().

Let's take a look at an example:

#sum a list of numbers using for loop

list = [100300200300100400240120430]
total = 0
for l in list:
    total += l
print("The total is", total)

Here, we use the for loop statement to iterate over a list and then add each number to a variable named total. You also can use the while loop statement to archive the same result.

Example 2: Adding Numbers in a Tuple

The following sample code adds all numbers in a tuple and returns the total:

tuple = (240, 320, 120, 230, 130, 140, 440, 220, 420)
total = sum(tuple)
print("The total is", total)

Result:

The total is 2260

Example 3: Adding all Numbers in a List and Other Numbers

You also can add all the numbers in a list and then add another number.
The following sample code adds all numbers in a list and then adds 50, and returns the total:

list = [200.40, 100, 400, 200.50, 400.40, 300.20]
total = sum(list, 50)
print("The total is", total)

Result:

The total is 1651.5

Example 4: Adding all Dictionary Values

The following sample code adds all 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)

Result:

The total is 1000

Example 5: Sum Two Lists

In this example, I show you how we sum multiple lists in Python.

The following example illustrates how to sum two lists of numbers:

#sum two lists
list1 = [2010304050]
list2 = [60807090100110]
total = sum(list1) + sum(list2)
print("The total is", total)

Example 5: Sum a List of Lists

The following example illustrates a program that sums a list of lists:

#sum a list of lists
total = 0
list1 = [12345]
list2 = [67891011]
list3 = [121314151617]
list =[list1, list2, list3]
for l in list:
    total += sum(l)
print("The total is", total)

Here, as you can see, we use the for loop statement and sum() function to add all numbers in a list of lists.

In this tutorial, you've learned how to use the sum() function in Python. The sum() function a function used to add all values in an iterable, such as a list, tupleset, or dictionary, and return the total.

Leave a Reply

Your email address will not be published. Required fields are marked *