Notes

`any()` Function in Python [ English ]

< Prev Next >

1. Introduction

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.

2. Syntax

any(iterable)

Parameter

3. Return Value

4. Basic Example

values = [False, False, True]

print(any(values))

Output

True

Explanation

5. Example with All False Values

values = [False, False, False]

print(any(values))

Output

False

Explanation

6. Example with Numbers

numbers = [0, 0, 5, 0]

print(any(numbers))

Output

True

Explanation

7. Example with All Zero Values

numbers = [0, 0, 0]

print(any(numbers))

Output

False

Explanation

8. Example with Conditions

numbers = [1, 3, 5, 8]

result = any(n % 2 == 0 for n in numbers)

print(result)

Output

True

Explanation

9. Practical Example

marks = [35, 42, 60, 75]

failed = any(mark < 40 for mark in marks)

print("Any student failed:", failed)

Output

Any student failed: True

Explanation

10. Difference Between 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

11. Summary

< Prev Next >