Tag: Efault represents shel log rom Lex ace function numbers
English documents:
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 bes a string.
Description
1. The function function is to sum the types of iterations that can be iterated. Requirement: The ① receive object is an iterative type. ② an Iterative object all element types are numeric.
#passing in an iterative object>>> sum ((1,2,3,4))10>>> sum ([1,2,3,4])10>>> sum (Range (10))45#element type must be numeric>>> sum ((1.5,2.5,3.5,4.5))12.0>>> sum ((Complex (1,-1), complex (2,-2)))(3-3j)>>> sum ('4')) Traceback (most recent call last): File"<pyshell#11>", Line 1,inch<module>sum ((,'4') ) typeerror:unsupported operand type (s) for+:'int' and 'Str'
2. You can pass in an optional argument start, which represents the initialization value before summing, and returns the initial value if an empty, iterated data type is passed in.
>>> sum ((1,2,3,4), 2)12>>> sum ((1,2,3,4), -10) 0# Pass in an empty iterator object and return the initial value directly >>> sum ([],2)2
Python built-in function (--sum)