Built-in Functions and Types
sum()
Combine the items of an iterable by addition, including an initial value. It is suitable for numeric totals and consumes a one-pass iterator. It is not a general text-concatenation tool; use str.join() for strings. An empty iterable returns start. Incompatible addends raise TypeError. Floating-point totals can still have rounding error.
Formal Syntax & Structure
python syntaxpython
sum(iterable, /, start=0)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| iterable | iterable of addable values | Items to accumulate. |
| start | number or compatible addable object | Initial value, default zero; a string is not permitted. |
Return Value
number or compatible result
Total of start and the supplied items.
Working Example
Example Snippetpython
opening_balance = 20changes = [8, -3, 12]print(sum(changes, start=opening_balance))