English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
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() returns the sum of iterable items and adds the total sum starting from start.
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