Working With Iterables
Testing Conditions with all() and any()
Learn how to use any() and all() to test whether any or all elements in an iterable satisfy a condition.
Python's any() function takes an iterable and returns True if at least one element is truthy.
Here, one element is True so any() returns True:
Python
Output
When no element is truthy, any() returns False:
Python
Output
What will be the output?
Python
The all() function returns True only if every element in the iterable is truthy.
Every element is True, so all() returns True:
Python
Output
One False element makes all() return False:
Python
Output
What will be the output?
Python
Both functions work with any list — including a list of booleans built with a for loop.
Check if any number is positive using a for loop:
Python
Output
Check if all numbers are positive the same way:
Python
Output
What will be the output?
Python
What will be the output?
Python
What will be the output?
Python
What will be the output?
Python