Sum () function in Python
The built-in function sum () in Python returns the sum of all numbers in iterable.
Sum function sum ()
sum(iterable, start)
The sum () function starts adding from left to right
iterable
: built-in iterable (like list, string, dict) needs to be summed, usually numbers.start
: value added to the value returned from iterable. The default value is 0.
Value returned from sum ()
The sum () function returns the sum of start
and iterable component items.
For example: How sum () works in Python
numbers = [2.5, 3, 4, -5]
# không truyền tham số start
numbersSum = sum(numbers)
print(numbersSum)
# start = 10
numbersSum = sum(numbers, 10)
print(numbersSum)
Run the program, the result is:
4.5
14.5
See also: Built-in Python functions
4 ★ | 1 Vote
You should read it
May be interested
- __Import __ () function in Python__import __ () is an advanced function called by the import statement in python
- The function hasattr () in Pythonthe hasattr () function in python is used to check if the input object has the property you want to find.
- Filter () function in Pythonhow is the filter () function syntax, what parameters and how does it work? invites you to read the track.
- Int () function in Pythonthe int () function in python returns an integer object from any number or string.
- Wool () function in Pythonhow is the syntax of len () function, what parameters do it have and how is it used? invites you to read the track.
- Getattr () function in Pythonthe getattr () function in python returns the value of the property you want to find, if the object does not have this property the function will return the default value provided.