PCEP-30-02 practice question 11 of 35

We need to check if all elements of the iterable object evaluate to the truth. What built-in function can you use for this?

Full set: 548 questions

Domain Computer Programming and Python Fundamentals

Answer options
A: any()
B: all() Correct answer
C: bin()
D: bool()
Correct Answer:
B. all()
Explanation

The built-in function all(iterable) is designed specifically for this purpose. It takes one argument, an iterable (like a list or tuple), and returns True if all elements within that iterable evaluate to a Boolean True. If the iterable is empty, it also returns True. It returns False as soon as it encounters the first element that evaluates to False, providing an efficient check. This directly matches the requirement to verify that all elements of an iterable object are truthy.

Why Incorrect

A. any() returns True if at least one element of the iterable is true, not all of them.
C. bin() is unrelated; it converts an integer into its binary string representation.
D. bool() converts a single specified object to its Boolean value (True or False), but it does not operate on an entire iterable. ---

References

1. Python Software Foundation. (2023). Built-in Functions — The Python Standard Library. Python 3.12.0 documentation.

Section: all(iterable)

Details: "Return True if all elements of the iterable are true (or if the iterable is empty). Equivalent to: def all(iterable): for element in iterable: if not element: return False return True"

URL: https://docs.python.org/3/library/functions.html#all

2. Python Software Foundation. (2023). Truth Value Testing — The Python Standard Library. Python 3.12.0 documentation.

Section: Built-in Types

Details: This section explains which values are considered false in Python (e.g., None, False, zero of any numeric type, empty sequences and collections). The all() function uses these rules for evaluation.

URL: https://docs.python.org/3/library/stdtypes.html#truth-value-testing

3. Guttag, J. V. (2016). Introduction to Computation and Programming Using Python, With Application to Understanding Data (2nd ed.). MIT Press.

Chapter 5: Structured Types, Mutability, and Higher-Order Functions

Details: University-level texts on Python programming introduce built-in functions that operate on iterables. The all() function is a standard example of a higher-order function that aggregates a result from a sequence based on a logical condition (truthiness).

Scroll to Top