Remember
all() tests whether every item is truthy. It returns True for an empty iterable.
all() tests whether every item is truthy. It returns True 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 = [3, 1]
print(all(items))
print(all([]))Top-level scope
[3, 1]all() tests whether every item is truthy. It returns True for an empty iterable.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
items = [3, 1]
print(all(items))
print(all([]))
|
v
DISPLAYED RESULT
True
True
FINAL BINDINGS
items --> object 1 (list)
object 1: [3, 1]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 = [3, 1]
print(all(items))
print(all([]))Top-level scope
[3, 1]The example has finished. Compare the final output with the values and references above.
True True
What do you expect this program to print?
items = [3, 1]
print(all(items))
print(all([]))all() FunctionThe all() function in Python checks whether all elements in an iterable are true. If they are, it returns True. Otherwise, it returns False.
all(iterable)
iterable – A list, tuple, set, dictionary, or any iterable object.True – If all elements in the iterable are true (or the iterable is empty)False – If any element is falsevalues = [True, True, True]
print(all(values))
True
values = [True, False, True]
print(all(values))
False
numbers = [1, 2, 3, 0]
print(all(numbers))
False
0 is considered False in Python.
print(all([]))
True
This might seem strange, but in logic, a universal statement about an empty set is considered true.
nums = [5, 8, 12, 3]
result = all(n > 0 for n in nums)
print(result)
True
inputs = ["name", "email", ""]
print(all(inputs))
False
This is useful in form validation — one empty string makes all() return False.
TypeErrorall() checks a condition itself — it only evaluates the truthiness of elementsall() is often used with list comprehensions or generator expressions to verify conditions across a dataset.
all() returns True only if every element is true0, False, "", [], etc., are considered falseWrite a program that takes a list of ages and prints "All adults" if all are 18 or older.
ages = [22, 34, 19, 45]
if all(age >= 18 for age in ages):
print("All adults")
else:
print("Not all are adults")
Connect what you noticed in the example with the idea below.
all() tests whether every item is truthy. It returns True for an empty iterable.
all([]) returns False.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.