How to add numbers in a list in python

爱喝马黛茶的安东尼
Release: 2019-06-20 15:00:49
Original
33976 people have browsed it

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!