如下所示:#thebasicways=0forxinrange(10):s+=x#therightways=sum(range(10))#thebasicways=1forxinrange(1,10):s*=x#theo
如下所示:
# the basic way
s = 0
for x in range(10):
s += x
# the right way
s = sum(range(10))
# the basic way
s = 1
for x in range(1, 10):
s *= x
# the other way
from operator import mul
reduce(mul, range(1, 10))
以上这篇在python中实现对list求和及求积就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
python list 求和 求积