sum函数作为python的内置函数,顾名思义,可以对迭代器中的所有元素求总和,语法如下:
sum(iterable,start=0)
参数介绍:
iterable — 可迭代对象,如:列表、元组、集合;
start — 指定相加的参数,如果没有设置这个值,默认为0;
返回值 — 返回迭代器中所有元素相加得总和;
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:何以解忧
@Blog(个人博客地址): https://www.codersrc.com/
@File:python_sum.py
@Time:2019/12/11 21:25
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
"""
print(sum([0,1,2])) # 等价 0 + 1 + 2 = 3
print(sum((0,1,2),10)) # 等价 0 + 1 + 2 + 10 = 13
print(sum([0,1,2],20)) # 等价 0 + 1 + 2 + 20 = 23
输出结果:
3
13
23
1.python文件读写open/write/readline/close
转载请注明:猿说Python » python sum函数