本页内容
上一节: Python3_func_ord 下一节: Python_func_bytearray

Python sum() 函数

Python 内置函数

描述

sum() 方法对序列进行求和计算。

语法

以下是 sum() 方法的语法:


示例

sum(iterable[, start])

参数

  • iterable -- 可迭代对象,如:列表、元组、集合。
  • start -- 指定相加的参数,如果没有设置这个值,默认为0。

返回值

返回计算结果。

以下展示了使用 sum 函数的实例:


示例

>>>sum([0,1,2])
3
>>> sum((2, 3, 4), 1)        # 元组计算总和后再加 1
10
>>> sum([0,1,2,3,4], 2)      # 列表计算总和后再加 2
12

Python 内置函数

上一节: Python3_func_ord 下一节: Python_func_bytearray
此页面最后编辑于2022年8月17日 (星期三) 21:49。