Home >Backend Development >Python Tutorial >sum function sum in python

sum function sum in python

高洛峰
高洛峰Original
2016-10-19 13:45:003906browse

sum is a very practical function in python, but you must pay attention to its use. The first time I used it, I used it like this:

s = sum(1,2,3)

The result was tragic La

In fact, the parameter of sum() is a list

For example:

sum([1,2,3])

sum(range(1,11))

There is also a more interesting usage

a = range(1,11)

b = range(1,10)

c = sum([item for item in a if item in b])

print c

Output:

45


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