the first sacrifice English Documents:
-
all
(
iterable Span class= "Sig-paren")
-
Return true
If all elements of the iterable was true (or if the iterable is empty). Equivalent to:
def all (iterable): for element in iterable : if not element: return false return true
-
any
(
iterable)
-
Return True
If any element of the iterable is true. If the iterable is empty, return False
. Equivalent to:
Any(iterable): iterableelementFalse
Again on the function information table
Function prototypes |
All (iterable) |
Parameter interpretation |
Iterable |
An object can be iterated, and the parameter cannot be empty, but the iterable can be empty. |
return value |
<class ' bool ' > True or False. |
Function description |
Returns True when all elements in the iterable are true (or iterable is empty). |
Function prototypes |
Any (iterable) |
Parameter interpretation |
Iterable |
An object can be iterated, and the parameter cannot be empty, but the iterable can be empty. |
return value |
<class ' bool ' > True or False. |
Function description |
Returns True when an element in the iterable is true. Returns False if Iterable is empty. |
From the official documentation, we know that all (iterable) is equivalent to the following code:
1 def All (iterable): 2 for inch iterable: 3 if not element: 4 return False 5 return True
Any (iterable) is equivalent to this code:
1 def Any (iterable): 2 for inch iterable: 3 if element: 4 return True 5 return False
That is, when given a non-nullable iterator object :
For The all function, returns trueif the element is all true, otherwise false . That is, as long as the existence of false elements to return false .
For The Any function, returns truewhenever there is an element that is true . The element that does not exist for true returns false.
Also, when an iterator object is empty , the all function returns True, and the Any function returns False. This is because the iterable is empty when the traversal is not executed and jumps directly to line 5 to execute the return.
Example 1:all function Various parameters of various results
1>>> All (['a', (2,4), 1,true])#list is true2 True3>>> All (['a', (), 1,true])#empty tuple in list element4 False5>>> All (['a', (2,4), 0,true])#There are 06 False7>>> All (['a', (2,4), 3,false])#have false8False
Example 2:any function Various parameters of various results
1>>> any (['a', (2,4), 3, True]) 2 True3>>> any (['a', (2,4), 3, False])4 True5>>> any (['a', (), 3, False]) 6 True7>>> any (["', (), 0,false]) 8 False9>>> any ('a', (), 3, False))Ten True One>>> any ("', (), 0,false)) AFalse
Example 3:all function parameter is empty and iterable is empty
1>>>All ()2 Traceback (most recent):3File"<pyshell#20>", Line 1,inch<module>4 All ()5 Typeerror:all () takes exactly one argument (0 given)6>>>All ([])7 True8>>>All (())9 TrueTen>>>All ({}) One True A>>> All ("')#null list, truple, Dict, str are true - True
Example 4:any function parameter is empty and iterable is empty
1>>>Any ()2 Traceback (most recent):3File"<pyshell#37>", Line 1,inch<module>4 Any ()5 Typeerror:any () takes exactly one argument (0 given)6>>>Any ([])7 False8>>>Any (())9 FalseTen>>>Any ({}) One False A>>> any ("") -False
Example 5:iterable does not traverse when empty
1 >>> p=12 >>> i=[]3 for in I: # i is empty do not traverse 4 print (p)567 >>>
If you like this article please have a nice and go. If there is an imperfect place please also indicate in the comment area. Questions are welcome in the comments section.
Python3 built-in functions--all and any