any and all in python3 1

any and all in python3

# Here all the iterables are True so all 
# will return True and the same will be printed 
print (all([True, True, True, True])) 
  
# Here the method will short-circuit at the  
# first item (False) and will return False. 
print (all([False, True, True, False])) 
  
# This statement will return False, as no 
# True is found in the iterables 
print (all([False, False, False])) 

Here is what the above code is Doing:
1. The all() function takes an iterable (list, tuple, dictionary etc.) as an argument.
2. The iterable is passed to the iter() function which returns an iterator.
3. The iterator is passed to the next() function which returns the next item from the iterator.
4. The next() function is called repeatedly until StopIteration is raised.
5. If all items in the iterable are true, all() returns True.
6. If any item in the iterable is false, all() returns False.
7. If the iterable is empty, all() returns True.

Similar Posts