In Python, the sum() function is used to perform summation on a given iterable object.

Function Syntax

sum(iterable, /, start=0)

Parameters:

  • iterable: The iterable object to be summed.
  • start: The starting value, defaults to 0.

The sum() function sums all elements in the iterable object and returns the result. If the iterable contains elements of non-numeric types, a TypeError exception will be raised.

Examples of the sum() Function

Summing all elements in a list:

lst = [1, 2, 3, 4, 5]
print(sum(lst))  # 15

# Specify a starting value
print(sum(lst, start=100))  # 115