Home  >  Article  >  Backend Development  >  How to add numbers in a list in python

How to add numbers in a list in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-20 15:00:4933989browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn