Function Details: sum

Description


Returns the sum of a 'start' value (default: 0) plus an iterable of numbers.


Extended Description


The sum() function adds the items of an iterable from left to right and returns the total. It starts with the optional 'start' value (which defaults to 0) and adds each item from the iterable to it. For numbers, it's equivalent to using the + operator in a loop. However, sum() is preferred for better performance and readability. Note that sum() is not recommended for concatenating strings; it's better to use ''.join(sequence) for that purpose.


Read More about sum from Python Documentation

Function Signature


sum(iterable: Iterable, start: Union[int, float] = 0) -> Union[int, float]

Module: builtins

Parameters



Parameter List


  • iterable: Iterable
  • start: Union[int, float]

Return


Returns the sum of all items in the iterable plus the start value.


Return Type


Union[int, float]

Output

Explanation

These examples show how sum() works with different types of iterables.