Python标准库:内置函数sum(iterable[, start])
2015-05-03 09:37
525 查看
本函数用来计算可迭代对象iterable的和,然后以这个结果再加上start的值。参数start用来指定相加的参数,如果没有设置这个值,默认是0值。要计算和的序列一般是数字类型,并且开始参数要设置为数字类型。其它有些情况之下,使用别的计算和的方式会更好,比如计算字符串的和使用’’.join(sequence);或者计算浮点数的和使用math.fsum();或者计算多序列的和使用itertools.chain()。
例子:
结果输出如下:
range(0, 10) = 45
16
17
10
10
蔡军生 QQ:9073204 深圳
例子:
#sum() l = range(10) print(l, '=', sum(l)) print(sum([2, 5, 8], 1)) print(sum([2, 5, 8], 2)) print(sum((2, 3, 4), 1)) print(sum(set([2, 3, 4]), 1))
结果输出如下:
range(0, 10) = 45
16
17
10
10
蔡军生 QQ:9073204 深圳
相关文章推荐
- [转载]Python标准库:内置函数sum(iterable[, start])
- Python标准库:内置函数sum(iterable[, start])
- Python标准库:内置函数enumerate(iterable, start=0)
- Python标准库:内置函数enumerate(iterable, start=0)
- Python标准库:内置函数slice(stop) slice(start, stop[, step])
- Python标准库:内置函数filter(function, iterable)
- Python标准库:内置函数slice(stop) slice(start, stop[, step])
- Python标准库:内置函数sorted(iterable[, key][, reverse])
- Python标准库:内置函数filter(function, iterable)
- Python标准库:内置函数tuple([iterable])
- Python标准库:内置函数sorted(iterable[, key][, reverse])
- Python标准库:内置函数frozenset([iterable])
- Python标准库:内置函数frozenset([iterable])
- Python标准库:内置函数list([iterable])
- Python标准库:内置函数dict(iterable, **kwarg)
- Python标准库:内置函数list([iterable])
- Python标准库:内置函数slice(stop) slice(start, stop[, step])
- Python标准库:内置函数dict(iterable, **kwarg)
- Python标准库:内置函数all(iterable)
- Python标准库:内置函数all(iterable)