英文文档:

sum(iterable[, start])

Sums start and the items of an iterable from left to right and returns the total. start defaults to 0. The iterable‘s items are normally numbers, and the start value is not allowed to be a string.

说明:

  1. 函数功能是对可迭代类型进行求和。要求:① 接收对象是可迭代类型。② 可迭代对象所有元素类型是数值型。

# 传入可迭代对象
>>> sum((1,2,3,4))
10
>>> sum([1,2,3,4])
10
>>> sum(range(10))
45 # 元素类型必须是数值型
>>> sum((1.5,2.5,3.5,4.5))
12.0
>>> sum((complex(1,-1),complex(2,-2)))
(3-3j) >>> sum((1,2,3,''))
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
sum((1,2,3,''))
TypeError: unsupported operand type(s) for +: 'int' and 'str'

  2. 可以传入一个可选参数start,表示求和前的初始化值,如果传入空的可迭代数据类型,则返回初始值。

>>> sum((1,2,3,4),2)
12
>>> sum((1,2,3,4),-10)
0 # 传入空可迭代对象,直接返回初始值
>>> sum([],2)
2

Python内置函数(62)——sum的更多相关文章

  1. Python内置函数(62)——exec

    英文文档: exec(object[, globals[, locals]]) This function supports dynamic execution of Python code. obj ...

  2. Python内置函数(7)——sum

    英文文档: sum(iterable[, start]) Sums start and the items of an iterable from left to right and returns ...

  3. Python | 内置函数(BIF)

    Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...

  4. python内置函数

    python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...

  5. Python 内置函数笔记

    其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...

  6. 【转】python 内置函数总结(大部分)

    [转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...

  7. python内置函数,匿名函数

    一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...

  8. python 内置函数总结(大部分)

    python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...

  9. Python之路(第八篇)Python内置函数、zip()、max()、min()

    一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回t ...

随机推荐

  1. Jmeter函数助手

    1.__Random:产生0-10之间的随机数[__RadomString:随机生成字符函数同__Random]

  2. Windows phone 8.1之数据绑定(Data Binding)

    学习Winphone8.1的时候经常需要对Slider进行数据绑定后使之视觉化,方便调节Slider的值. 数据绑定分为源(Source)和目标(Target),Source一般分为两种,其他控件的数 ...

  3. maya cmds pymel selectType() 选择类型切换

    maya cmds pymel selectType() 选择类型切换 import maya.cmds as cmds cmds.selectType( polymeshFace = True ) ...

  4. 更新mysql驱动5.1-47 Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEY

    今天在更新mysql驱动后运行程序突然报如下错误: java.sql.SQLException: Generated keys not requested. You need to specify S ...

  5. ./graldew bash: ./gradlew: No such file or directory

    使用gradlew的项目,可以使用./gradlew assembelDebug 使用本地gradle编译的项目,并且配置了环境变量,可以使用gradle assembleDebug直接编译包

  6. Android的自定义View及View的绘制流程

    目标:实现Android中的自定义View,为理清楚Android中的View绘制流程“铺路”. 想法很简单:从一个简单例子着手开始编写自定义View,对ViewGroup.View类中与绘制View ...

  7. VS2017简单使用

    1. 2.删除下面的文件 3.点击属性 4.改为否 不使用预编译头 万能头文件自己导入网上有教程

  8. webpack学习--安装

    webpack需要在node环境运行,可以去node官网进行下载安装包:http://nodejs.cn/download/ 1.打开cmd命令窗口,运行node -v 2.全局安装webpack:n ...

  9. linux android开发环境搭建

    android开发环境搭建的一些有用链接:1.sdk manager的国内服务器http://www.cnblogs.com/huangjacky/p/4077982.html2.常见问题的解决htt ...

  10. thinkphp 单图上传组建成数组然后追加到一个字段

    //上传的数组字段 $note1 = input('note1'); $note2 = input('note2'); $note3 = input('note3'); $note4 = input( ...