Python sum() Function
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, dictionary and range 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 example below, 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