Remember
any() returns True when at least one item is truthy. It returns False for an empty iterable.
any() returns True when at least one item is truthy. It returns False for an empty iterable.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
items = [0, 3]
print(any(items))
print(any([]))Top-level scope
[0, 3]any() returns True when at least one item is truthy. It returns False for an empty iterable.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
items = [0, 3]
print(any(items))
print(any([]))
|
v
DISPLAYED RESULT
True
False
FINAL BINDINGS
items --> object 1 (list)
object 1: [0, 3]Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
items = [0, 3]
print(any(items))
print(any([]))Top-level scope
[0, 3]The example has finished. Compare the final output with the values and references above.
True False
What do you expect this program to print?
items = [0, 3]
print(any(items))
print(any([]))any() FunctionThe any() function in Python is used to check if at least one element in an iterable (like a list, tuple, or set) is True. If it finds even one True value, it immediately returns True.
any(iterable)
iterable – A list, tuple, set, string, dictionary, or any iterable containing values.True – if at least one element is true (non-zero or non-empty)False – if all elements are false (zero, empty, or None)values = [0, False, 5, None]
print(any(values))
True
values = [0, "", False, None]
print(any(values))
False
words = ["", "", "hello"]
print(any(words))
True
Because "hello" is non-empty, it's considered True.
name = ""
email = "example@example.com"
phone = ""
if any([name, email, phone]):
print("At least one field is filled.")
else:
print("All fields are empty.")
At least one field is filled.
True value (short-circuiting).False if no True values are found.any(5) → ❌ Raises TypeErrorany() is often used to simplify conditional checks across multiple values. It helps avoid writing long or chains like if a or b or c.
any() returns True if at least one value is truthy.True.Write a program that checks if any number in a list is greater than 10:
numbers = [2, 5, 8, 12, 4]
result = any(n > 10 for n in numbers)
print("Any number > 10?", result)
Any number > 10? True
Connect what you noticed in the example with the idea below.
any() returns True when at least one item is truthy. It returns False for an empty iterable.
any([]) returns True.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.