Python math.fsum() 方法
定义和用法
math.fsum()
方法会返回可迭代对象(如元组、数组、列表等)中所有项的和,结果为浮点数。
实例
返回元组中所有项的和:
# 导入 math 库 import math # 添加元组中的项 a = (1, 2, 3, 4, 5) # 打印所有项的和 print(math.fsum(a))
语法
math.fsum(iterable)
参数
参数 | 描述 |
---|---|
iterable | 必需。要计算和的序列。如果序列不是数字,它将返回 TypeError。 |
技术细节
返回值: | 浮点数值,表示可迭代对象中所有项的和。 |
---|---|
Python 版本: | 2.6 |