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

sum function sum in python

高洛峰
Release: 2016-10-19 13:45:00
Original
3905 people have browsed it

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


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