Python: sum() function
sum() function
The sum() function is used to get the sum of all items in an iterable.
Version:
(Python 3.2.5)
Syntax:
sum(iterable[, start])
Parameter:
Name | Description | Required / Optional |
---|---|---|
iterable | An iterable (list, tuple, dict etc) whose item's sum is to be computed. | Required |
start | A value that is added to the return value. | Optional |
Return value:
The sum of the given iterable.
Example: Python sum()
num = [3.5, 5, 2, -5]
# start parameter is not provided
numSum = sum(num)
print(numSum)
# start = 15
numSum = sum(num, 15)
print(numSum)
Output:
5.5 20.5
Pictorial Presentation:
Pictorial Presentation:
Python Code Editor:
Test your Python skills with w3resource's quiz
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python/built-in-function/sum.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics