The any() function in Python is a built-in function used to check whether at least one element in an iterable is True. If any element evaluates to True, the function returns True. If all elements are False, the function returns False.
The any() function is useful when a program needs to check if any condition in a sequence is satisfied.
any(iterable)
values = [False, False, True]
print(any(values))
Output
True
Explanation
True, so the function returns True.values = [False, False, False]
print(any(values))
Output
False
Explanation
False, so the function returns False.numbers = [0, 0, 5, 0]
print(any(numbers))
Output
True
Explanation
5 is non-zero, the function returns True.numbers = [0, 0, 0]
print(any(numbers))
Output
False
Explanation
0, which is considered False.numbers = [1, 3, 5, 8]
result = any(n % 2 == 0 for n in numbers)
print(result)
Output
True
Explanation
8 is even, the result is True.marks = [35, 42, 60, 75]
failed = any(mark < 40 for mark in marks)
print("Any student failed:", failed)
Output
Any student failed: True
Explanation
35 is less than 40, the result is True.all() and any()| Function | Condition | Result |
|---|---|---|
all() |
All elements must be True | Returns True only if every element is True |
any() |
At least one element must be True | Returns True if any element is True |
Example:
values = [True, False, True]
print(all(values))
print(any(values))
Output:
False
True
any() is a built-in Python function used to check if at least one element in an iterable is True.