sum(iterable[, start])
The sum function sums the start value and the values contained in the iterator included as the first argument, returning the total. By default, start takes the value 0.
- iterable: Iterable object whose elements we want to add.
- start: (Optional) additional amount to add. If this argument is not specified, it is considered the default value of zero.
The sum function returns an integer, real or complex number, depending on the type of the elements in the iterable.
In this first example we have an iterator that contains integers and we do not specify the start argument. The function simply returns the sum of the elements of the iterator as an integer:
Here, the list contains a real number and we set the start argument to 10. The function adds 10 to the values in the list and returns a real number:
In this third example, the list includes a complex number, and the result of the function is also returned as a complex number: