Python function any () and all ()

Source: Internet
Author: User

Any (iterable)

All (iterable)

Any () differs from the all () function, any is arbitrary, and all is all.

Version: This function is suitable for version 2.5 and above, compatible with Python3 version.

Any (iterable) Description: Parameter iterable: can iterate object;

If all values of iterable are 0, ' ' or false, then the result is false, and if there is a value in all elements that is not 0, ' ' or false, the result is true

The function is equivalent to:

def any (iterable):

For element in iterable:

If element:

Return False

Return True

Example:

>>> any ([' A ', ' B ', ' C ', ' d '])  #列表list, the elements are not empty or 0true>>> any ([' A ', ' B ', ', ' d '])  #列表list, There is an empty element true>>> any ([0, ', ', False])  #列表list, the element is all 0, ',falsefalse>>> any (' A ', ' B ', ' C ', ' d '))  #元组tuple, the elements are not empty or 0true>>> any ((' A ', ' B ', ', ' d '))  #元组tuple, there is an empty element true>>> any (0, ', false))  #元组tuple, the element is all 0, "', Falsefalse >>> any ([]) # empty list false>>> any (()) # Empty tuple false

  

All (iterable)

If all elements of iterable are not 0, ', false, or iterable are empty, all (iterable) returns True, otherwise false; The function is equivalent to:

defall(iterable):

     for element  in iterable:          if not element:              return False      return TrueExample:
>>> all ([' A ', ' B ', ' C ', ' d '])  #列表list, the elements are not empty or 0true>>> all ([' A ', ' B ', ', ' d '])  #列表list, There is an empty element false>>> all ([0, 3])  #列表list, there is an element of 0 false  >>> all ((' A ', ' B ', ' C ', ' d '))  #元组tuple, the elements are not empty or 0true>>> all ((' A ', ' B ', ', ' d ')  #元组tuple, there is an empty element false>>> all (0, 1, 2, 3))  #元组tuple, there is an element of 0 false  >>> all ([]) # Empty list true>>> All (()) # Empty tuple True

Note: Empty tuple, empty list return value is true, here is to pay special attention to

Python function any () and all ()

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.