search

What is the use of the "sum" function in Python?

The **sum** function in Python is used to sum up elements of an iterable (such as a list, tuple, or dictionary) and return the sum. It takes two arguments: the iterable and an optional start value. The start value, if provided, is added to the sum of elements in the iterable. By default, the start value is 0. The elements in the iterable must be numbers (integer or float). Here is an example:

numbers = [1, 2, 3, 4, 5]
print(sum(numbers)) # Output: 15

numbers = [1, 2, 3, 4, 5]
print(sum(numbers, 10)) # Output: 25

Related Questions You Might Be Interested