Python sum function is used to calculate the sum of all the values in an iterable object such as tuple and list. It takes the iterable object in the parameter and returns the sum.

What is iterable in Python

Iterable is a type of object in Python which retrieves its elements one by one. Anything which you can access through a for loop is iterable. It may be List, Strings, tuples, Set and dictionary.

Python sum() function

This article is the most comprehensive guide for the Python sum() function. You will also learn with the code examples for each type of object.

Sum() is an inbuilt Python function which you can use to find the aggregate of a sequence of numeric values. It calculates from left to right.

Syntax

sum(iterable, start)

Parameters

Iterable the required iterable item whose sum you want to find

Start optional numeric value added to the return value. The default value is 0.

How to find a sum of the list values in Python

The following lines of code compute the sum of the list elements:

Thelist = [2,4,5,6,7]

print(sum(Thelist))

Output

24

With start value 2

Thelist = [2, 4, 5, 6, 7]

print(sum(Thelist,2))

Output

26

How to find a sum of tuple values in Python

The following lines of code uses tuple to find the sum:

tup = (2,4,5,6,7)

print(sum(tup))

Output

24

With start value 2

tup = (2,4,5,6,7)

print(sum(tup,2))

Output

26

How to find sum of Set values in Python

set1 = {1, 4, 5, 3, 7}

print(sum(set1))

Output

20

How to find a sum of dictionary values in Python

You can easily find sum of dictionary elements with the sum method:

# define the dictionary

dict_1 = {“a”:5, “b”:10, “c”: 15}

# values function Returns values of a dictionary

values = dict_1.values()

# compute and store in a variable

total = sum(values)

print(total)

Output

30

Error and exception in Python sum()

You can also directly pass a tuple or a list as a parameter.

All the elements must be numbers to perform the sum operation. Otherwise, the python interpreter will throw unsupported operand type(s)

See the following example:

Thelist = [2, 4, 5, 6, 7,’4′]

print(sum(Thelist))

We defined the list with a string element. The sum function threw an error of unsupported type.

Output

How to find a sum of floating point numbers in Python

Define the floating point numbers in an iterable and use the sum method. You can easily add the values by using the following example:

Thelist  = [1.3, 4.5, 5.0, 3.6, 7.7]

print(sum(Thelist))

Output

22.1

Conclusion

The sum is an inbuilt Python function that provides an easy way to calculate the sum of an iterable object. The object may be a Python list, tuple, set or dictionary. You just need to pass any object and call the sum method. It returns the sum of all the numeric elements of your passed parameter. It also allows you to give a start parameter to add in the total aggregate.

You must use your parameter object with numeric values otherwise the sum throws typeerror unsupported operand type.

In the article, we discussed the method and multiple examples in detail.

About Post Author

1 thought on “How to use sum in Python

Leave a Reply