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.

CategoryBuilt-in Functions
KindBuilt-in Function
Python Versionall

📝 Syntax

sum(iterable[, start])

⚙️ Parameters

iterable RequiredAn iterable of numbers.
start OptionalThe initial value of the sum.(Default:0)

Returns:The total of all items added to start, left to right.

💥 Raises

▶ 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.
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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏