Facebook Page Twitter Page LinkedIn Page
× Python Tutorial Python Advanced Python Resources Python Libraries


The Python sum() function is used to calculate sum of all elements of an iterator. An iterable object can be any data structure like list, tuple, set, string and dictionary etc. The range() function can also be used as an iterable.

Syntax

sum(iterable, value)

Parameters

iterable Required. iterable object like list, tuple, set, string , dictionary and range() etc containing numbers.
value Optional. A number, which is added to the return value.

Example:

In the below example, sum() function is used to calculate sum of all elements of a given list.

MyList = [5, 10, 15]

print(sum(MyList))

print(sum(MyList, -5))

The output of the above code will be:

30
25

❮ Python Built-in Functions

5