Python built-in function (--sum)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.