Tutorials  /  Python

Python sum() Function

Ccentron Redaktion · February 2024 ·5 min read ·Python, Tutorial

Python sum() function is used to get the sum of numbers of an iterable.

VM

Try it yourself: cloud servers from €3.12/month

ccloud³ VMs from German data centers – deployed in seconds, billed by the hour, with a €200 starting credit for new accounts. Launch a server →

Python sum() function syntax is:

Code
sum(iterable[, start])

start is an optional number with default value of 0. If start is provided, then the sum of start and all the numbers in the iterable is returned.

Python sum() List of Numbers

Python
s = sum([1, 2, 3])
print(s)

s = sum([1, 2, 3], 10)
print(s)

Output:

Code
6
16

Note that sum() method doesn’t take keyword arguments, so if we write sum([1, 2, 3], start=10) then it will throw exception as TypeError: sum() takes no keyword arguments.

Python Sum of a Sequence of Integers

Since sum accepts iterable as argument, we can pass tuple, bytes of numbers too.

Python
s = sum(bytes([1, 2]))
print(s)

s = sum(bytearray([1, 2]), 10)
print(s)

# sum of integers in different formats, tuple of numbers
s = sum((1, 0b11, 0o17, 0xFF))
print(s)

s = sum((1, 0b11, 0o17, 0xFF), 0xF)
print(s)

Output:

Code
3
13
274
289

Python Sum of Floats

Python
s = sum([1.5, 2.5, 3])
print(s)

Output: 7.0 If you want to add floating point values with extended precision, you can use math.fsum() function.

Python Sum of Complex Numbers

sum() function works with complex numbers too.

Python
s = sum([1 + 2j, 3 + 4j])
print(s)

s = sum([1 + 2j, 3 + 4j], 2 + 2j)
print(s)

s = sum([1 + 2j, 2, 1.5 - 2j])
print(s)

Output:

Code
(4+6j)
(6+8j)
(4.5+0j)

Performance and Limitations of sum()

The sum() function is optimized for performance and can handle large datasets effectively. However, its performance can vary depending on the type of iterable being processed and the total size of the data. Here are some insights and practical tips to understand the performance and limitations of sum():

Performance Insights

1. Processing Large Ranges: The sum() function is highly efficient with iterables like range() because they are generated lazily and don't occupy memory for all elements at once.

# Example: Summing a large range

s = sum(range(1_000_000))

print(s) # Output: 499999500000

Note: This is memory-efficient since range() doesn't store all the numbers in memory.

2. Memory Usage with Lists: When passing a list, sum() requires all elements to be stored in memory, which can lead to memory constraints for extremely large datasets.

large_list = list(range(1_000_000))

s = sum(large_list)

print(s) # Output: 499999500000

Tip: If possible, use range() or a generator expression for better memory efficiency.

3. Generators for Efficiency: Generators allow for on-the-fly computation without preloading all elements into memory, making them ideal for summing very large datasets.

s = sum(x for x in range(1_000_000) if x % 2 == 0)

print(s) # Output: 249999500000

Limitations

1. Single Type of Elements: All elements in the iterable must be of a type that supports addition (+). If mixed types (e.g., strings and numbers) are present, a TypeError will occur.

s = sum([1, '2', 3]) # TypeError: unsupported operand type(s) for +: 'int' and 'str'

2. Handling Float Precision: While sum() can handle floats, it may suffer from precision errors when dealing with very large or very small floating-point numbers. In such cases, consider using math.fsum() for higher precision.

from math import fsum

floats = [1e-16, 1, 1e-16]

print(sum(floats)) # Output: 1.0 (loss of precision)

print(fsum(floats)) # Output: 1.0000000000000002 (better precision)

Practical Tips

  • Use range() or generator expressions for better memory management when summing large datasets.
  • For floating-point numbers, prefer math.fsum() to avoid precision errors.
  • Ensure the elements in the iterable are of compatible types for addition to prevent errors.

By understanding these performance characteristics and limitations, you can maximize the efficiency of the sum() function in your Python programs.

%3Cscript%20type%3D%22application%2Fld%2Bjson%22%3E%0D%0A%7B%0D%0A%20%20%22%40context%22%3A%20%22https%3A%2F%2Fschema.org%22%2C%0D%0A%20%20%22%40graph%22%3A%20%5B%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%22%40id%22%3A%20%22%23article%22%2C%0D%0A%20%20%20%20%20%20%22%40type%22%3A%20%22Article%22%2C%0D%0A%20%20%20%20%20%20%22headline%22%3A%20%22Python%20sum%28%29%20Function%22%2C%0D%0A%20%20%20%20%20%20%22name%22%3A%20%22Python%20sum%28%29%20Function%22%2C%0D%0A%20%20%20%20%20%20%22description%22%3A%20%22This%20article%20explains%20the%20Python%20sum%28%29%20function%2C%20its%20syntax%2C%20how%20to%20use%20it%20with%20lists%2C%20tuples%2C%20bytes%2C%20floats%2C%20and%20complex%20numbers%2C%20as%20well%20as%20important%20notes%20on%20performance%2C%20memory%20usage%2C%20generators%2C%20precision%2C%20and%20limitations.%22%2C%0D%0A%20%20%20%20%20%20%22inLanguage%22%3A%20%22en-US%22%2C%0D%0A%20%20%20%20%20%20%22about%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%22Python%22%2C%0D%0A%20%20%20%20%20%20%20%20%22Python%20sum%28%29%22%2C%0D%0A%20%20%20%20%20%20%20%20%22Python%20functions%22%2C%0D%0A%20%20%20%20%20%20%20%20%22Iterables%22%2C%0D%0A%20%20%20%20%20%20%20%20%22Lists%22%2C%0D%0A%20%20%20%20%20%20%20%20%22Tuples%22%2C%0D%0A%20%20%20%20%20%20%20%20%22Bytes%22%2C%0D%0A%20%20%20%20%20%20%20%20%22Bytearray%22%2C%0D%0A%20%20%20%20%20%20%20%20%22Integers%22%2C%0D%0A%20%20%20%20%20%20%20%20%22Floating-point%20numbers%22%2C%0D%0A%20%20%20%20%20%20%20%20%22Complex%20numbers%22%2C%0D%0A%20%20%20%20%20%20%20%20%22Generators%22%2C%0D%0A%20%20%20%20%20%20%20%20%22range%28%29%22%2C%0D%0A%20%20%20%20%20%20%20%20%22math.fsum%28%29%22%2C%0D%0A%20%20%20%20%20%20%20%20%22Performance%22%2C%0D%0A%20%20%20%20%20%20%20%20%22Memory%20usage%22%2C%0D%0A%20%20%20%20%20%20%20%20%22TypeError%22%0D%0A%20%20%20%20%20%20%5D%2C%0D%0A%20%20%20%20%20%20%22keywords%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%22Python%20sum%20function%22%2C%0D%0A%20%20%20%20%20%20%20%20%22Python%20sum%28%29%22%2C%0D%0A%20%20%20%20%20%20%20%20%22calculate%20sum%20in%20Python%22%2C%0D%0A%20%20%20%20%20%20%20%20%22sum%20iterable%20start%22%2C%0D%0A%20%20%20%20%20%20%20%20%22sum%20list%20Python%22%2C%0D%0A%20%20%20%20%20%20%20%20%22sum%20tuple%20Python%22%2C%0D%0A%20%20%20%20%20%20%20%20%22sum%20bytes%20Python%22%2C%0D%0A%20%20%20%20%20%20%20%20%22sum%20floats%20Python%22%2C%0D%0A%20%20%20%20%20%20%20%20%22sum%20complex%20numbers%20Python%22%2C%0D%0A%20%20%20%20%20%20%20%20%22math.fsum%22%2C%0D%0A%20%20%20%20%20%20%20%20%22Python%20generator%20sum%22%2C%0D%0A%20%20%20%20%20%20%20%20%22Python%20range%20sum%22%2C%0D%0A%20%20%20%20%20%20%20%20%22Python%20TypeError%20sum%22%2C%0D%0A%20%20%20%20%20%20%20%20%22Python%20sum%20performance%22%2C%0D%0A%20%20%20%20%20%20%20%20%22Python%20memory%20usage%22%0D%0A%20%20%20%20%20%20%5D%2C%0D%0A%20%20%20%20%20%20%22articleSection%22%3A%20%22Python%22%2C%0D%0A%20%20%20%20%20%20%22publisher%22%3A%20%7B%0D%0A%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22Organization%22%2C%0D%0A%20%20%20%20%20%20%20%20%22name%22%3A%20%22centron%22%2C%0D%0A%20%20%20%20%20%20%20%20%22url%22%3A%20%22https%3A%2F%2Fwww.centron.de%2F%22%0D%0A%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%22mainEntityOfPage%22%3A%20%7B%0D%0A%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22WebPage%22%2C%0D%0A%20%20%20%20%20%20%20%20%22%40id%22%3A%20%22https%3A%2F%2Fwww.centron.de%2Fen%2F%22%0D%0A%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%22mentions%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22DefinedTerm%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22sum%28%29%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22description%22%3A%20%22A%20built-in%20Python%20function%20used%20to%20calculate%20the%20sum%20of%20numbers%20in%20an%20iterable.%22%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22DefinedTerm%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22iterable%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22description%22%3A%20%22An%20object%20in%20Python%20whose%20elements%20can%20be%20iterated%20over%2C%20such%20as%20a%20list%2C%20tuple%2C%20bytearray%2C%20range%20object%2C%20or%20generator.%22%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22DefinedTerm%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22start%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22description%22%3A%20%22An%20optional%20start%20value%20of%20the%20Python%20sum%28%29%20function%20that%20is%20added%20to%20the%20sum%20of%20the%20iterable%27s%20elements.%22%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22DefinedTerm%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22list%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22description%22%3A%20%22A%20Python%20data%20type%20that%20stores%20multiple%20values%20in%20an%20ordered%2C%20mutable%20sequence%20and%20can%20be%20passed%20to%20sum%28%29%20if%20its%20elements%20support%20addition.%22%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22DefinedTerm%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22tuple%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22description%22%3A%20%22An%20ordered%2C%20immutable%20Python%20sequence%20whose%20numeric%20elements%20can%20be%20added%20with%20sum%28%29.%22%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22DefinedTerm%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22bytes%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22description%22%3A%20%22A%20Python%20data%20type%20for%20immutable%20byte%20sequences%20whose%20numeric%20values%20can%20be%20summed%20with%20sum%28%29.%22%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22DefinedTerm%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22bytearray%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22description%22%3A%20%22A%20mutable%20Python%20data%20type%20for%20byte%20sequences%20that%20can%20also%20be%20passed%20as%20an%20iterable%20to%20sum%28%29.%22%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22DefinedTerm%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22floating-point%20numbers%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22description%22%3A%20%22Numbers%20with%20decimal%20values%20that%20can%20be%20added%20with%20sum%28%29%2C%20although%20precision%20errors%20may%20occur.%22%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22DefinedTerm%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22complex%20numbers%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22description%22%3A%20%22Numbers%20with%20real%20and%20imaginary%20parts%20that%20can%20be%20added%20with%20Python%27s%20sum%28%29%20function.%22%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22DefinedTerm%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22math.fsum%28%29%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22description%22%3A%20%22A%20Python%20function%20from%20the%20math%20module%20that%20can%20be%20used%20for%20more%20precise%20addition%20of%20floating-point%20numbers.%22%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22DefinedTerm%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22range%28%29%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22description%22%3A%20%22A%20Python%20function%20that%20creates%20number%20ranges%20efficiently%20and%20works%20well%20with%20sum%28%29%20for%20large%20datasets.%22%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22DefinedTerm%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22generator%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22description%22%3A%20%22A%20Python%20object%20that%20produces%20values%20on%20demand%2C%20making%20it%20memory-efficient%20when%20used%20with%20sum%28%29%20for%20large%20datasets.%22%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22DefinedTerm%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22TypeError%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22description%22%3A%20%22A%20Python%20exception%20that%20can%20occur%20when%20incompatible%20or%20non-addable%20data%20types%20are%20processed%20with%20sum%28%29.%22%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%7D%0D%0A%20%20%5D%0D%0A%7D%0D%0A%3C%2Fscript%3E

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren