sum() Sum
Last updated: 2026-09-22
sum() Sum
sum(iterable, start=0) sums iterable items plus start. Do NOT use sum to concatenate strings; use ''.join instead.
| Category | Built-in Functions |
|---|---|
| Kind | Built-in Function |
| Python Version | all |
📝 Syntax
sum(iterable[, start])
⚙️ Parameters
| iterable Required | An iterable of numbers. |
|---|---|
| start Optional | The initial value of the sum.(Default:0) |
Returns:The total of all items added to start, left to right.
💥 Raises
TypeError— Raised when an item's type does not support addition.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
6
16
abc
⚠️ Warning sum is very slow for string concatenation; use ''.join() instead.
🏷️ Related Built-in Functions
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat does sum([]) return?
AIt returns 0 — the sum of an empty iterable is the default start value of 0.
QWhy can't I use sum(['a', 'b']) to concatenate strings?
Asum() starts accumulating from 0 by default, and a str cannot be added to an int, raising a TypeError; to join strings use ''.join(['a', 'b']), which is faster and never raises here.
QWhat does sum([1, 2, 3], 10) return?
AIt returns 16 — the start argument serves as the initial value: 10 + 1 + 2 + 3.
QIs sum() only for numbers?
AIt works with any type that supports + (numbers, lists, etc.), but in practice it is mainly used for numbers; to flatten nested lists use a comprehension or itertools.chain.