慕婉清6462132
sum(iterable[, start]) ,iterable为可迭代对象,如:sum([ ], start) #iterable为list列表sum(( ), start ) #iterable为tuple元组......最后的值 = 可迭代对象里面的数相加的值 + start的值start默认为0,如果不写就是0,为0时可以不写即sum()的参数最多为两个,其中第一个必须为iterable,例如:>>> sum([1, 2, 3,], 4)10>>> sum((1, 2), 3)6如果你写成sum([1,2,3]),start就是默认值 0 >>> sum([1, 2, 3])6>>> sum([ ], 2)2>>> sum(( ), )0>>> sum([1, 2] , 0)3当然iterable为dictionary字典时也是可以的:>>> sum({1: 'b', 7: 'a'})8>>> sum({1:'b', 7:'a'}, 9)17下面这些写法目前是不被接受的(以list为例,其他iterable同理):一、>>> sum([1,2],[3,4])Traceback (most recent call last): File "<pyshell#115>", line 1, in <module> sum([1,2],[3,4])TypeError: can only concatenate list (not "int") to list二、>>> sum(4,[1,2,3])Traceback (most recent call last): File "<pyshell#116>", line 1, in <module> sum(4,[1,2,3])TypeError: 'int' object is not iterable三、>>> sum()Traceback (most recent call last): File "<pyshell#117>", line 1, in <module> sum()TypeError: sum expected at least 1 arguments, got 0四、>>> sum(,2)SyntaxError: invalid syntax五、>>> sum(1,3)Traceback (most recent call last): File "<pyshell#112>", line 1, in <module> sum(1,3)TypeError: 'int' object is not iterable附其官方解释:>>> help(sum)Help on built-in function sum in module builtins:sum(...) sum(iterable[, start]) -> value Return the sum of an iterable of numbers (NOT strings) plus the value of parameter 'start' (which defaults to 0). When the iterable is empty, return start.