Beginner LevelPython
How do any() and all() evaluate booleans across iterables with short-circuiting?
❓ Interview Prompt: Explain `any()` (returns True if at least one item is truthy) vs `all()` (returns True ONLY if all items are truthy), and short-circuit evaluation.
1. 'any(iterable)':
Returns 'True' if at least one element in the iterable is truthy. Returns 'False' if iterable is empty.
2. 'all(iterable)':
Returns 'True' only if ALL elements in the iterable are truthy. Returns 'True' if iterable is empty.
Short-Circuit Evaluation:
Both functions stop evaluating elements the instant the result is determined (e.g. 'any()' stops on the first 'True' value).
Code Implementation Example
python
scores = [88, 92, 75, 95, 60]
# Check if ALL scores are passing (>= 60)
all_passed = all(s >= 60 for s in scores) # True
# Check if ANY score is perfect (== 100)
has_perfect = any(s == 100 for s in scores) # False💡Evaluating collections with short-circuiting boolean functions any() and all().
🎯 Interview Key Takeaway
Use all() to check if every element satisfies a condition and any() to check if at least one element satisfies it.
#any#all#Booleans#Short-Circuit#Beginner
Back to all Python Questions→