English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python basic tutorial

Python flow control

Fonctions en Python

Types de données en Python

Python file operations

Python objects and classes

Python date and time

Advanced Python knowledge

Python reference manual

Python sum() usage and examples

Python built-in functions

The sum() function adds the items of iterable and returns the total sum.

The syntax of the sum() function is:

sum(iterable, start)

The sum() function adds the items of iterable from left to right and returns the sum starting from start.

sum() parameters

  • iterable-Iterable (list, tuple, dictionary, etc.). The iterable items should be numbers.

  • start(Optional)-Specify the parameter to be added. If this value is not set, the default is 0.

sum() return value

sum() returns the sum of iterable items and adds the total sum starting from start.

Example: The working of Python sum()

numbers = [2.5, 3, 4, -5]
# No start parameter provided
numbers_sum = sum(numbers)
print(numbers_sum)
# start = 10
numbers_sum = sum(numbers, 10)
print(numbers_sum)

Output result

4.5
14.5

If you need to add floating-point numbers with precise accuracy, you should use math.fsum(iterable).

If you need to connect the given iterable items (the items must be strings), you can use the join() method.

'string'.join(sequence)

Visit this page to learn aboutInformation about the Python join() method

Python built-in functions