Home > Article > Backend Development > How to add numbers in a list in python
How do you add up the numbers in the list in Python? Let me introduce you to the sum() function.
sum()
sum() method performs sum calculation on the series.
Syntax
The following is the syntax of the sum() method:
sum(iterable[ , start])
Parameters
iterable -- iterable objects, such as lists, tuples, and sets.
start -- Specify the parameters for addition. If this value is not set, it defaults to 0.
Return value
Return the calculation result.
Example
The following shows an example of using the sum function:
>>>sum([0,1,2]) 3 >>> sum((2, 3, 4), 1) # 元组计算总和后再加 1 10 >>> sum([0,1,2,3,4], 2) # 列表计算总和后再加 2 12
The above is the detailed content of How to add numbers in a list in python. For more information, please follow other related articles on the PHP Chinese website!