Built-in Functions
sum()
Add the items in an iterable together.
sum(iterable, start=0)
sum() sums the items of an iterable from left to right and returns the total.
An optional start value (default 0) is added to the sum.
Parameters
| Name | Description |
|---|---|
| iterable | Numbers to add. |
| start | Optional starting value, which defaults to 0. |
Returns
The total.
Try it
Run it and change it
costs = [4, 7, 3] print(sum(costs))
Worth knowing
- Fastest string joining: Never use
sum()to concatenate strings; use''.join(sequence)instead for performance.