Python all () function

all () function is used to determine all the elements can be given iteration parameters in the whether iterable are TRUE, if it is to return True, otherwise False.

In addition to the elements is 0, empty, None, False outside are considered True.

Function is equivalent to:

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True

grammar

The following is the syntax all () method:

all(iterable)

parameter

  • iterable - tuple or list.

return value

If all elements of iterable not 0, '', False or iterable is empty, all (iterable) Returns True, otherwise return False;

Note: empty tuple, an empty list returns a value of True, pay special attention here.

Examples

The following example demonstrates the use of all () method:

All >>> ([ ' A ' , ' B ' , ' C ' , ' D ' ])   # list list, the element is empty or not 0 
True
 >>> All ([ ' A ' , ' B ' , ' ' , ' D ' ])    # list list, there is an empty element 
False
 ([0, 1,2,. 3]) All >>>           # listing list, there is an element of the 0 
False
   
 >>> All (( ' A ' , 'b', 'C ' , ' D ' ))   # tuple tuple, or elements not empty 0 
True
 >>> All (( ' A ' , ' B ' , ' ' , ' D ' ))    # tuple tuple, there is a empty element 
False
 >>> All ((0,. 1, 2,. 3))           # tuple tuple, element 0 is the presence of a 
False
   
 >>> All ([])              # empty list 
True
 >>> All (( ))              # empty tuple 
True

 

Guess you like

Origin www.cnblogs.com/shengguorui/p/11294431.html