PCEP-30-02 practice question 4 of 35

We need to check if at least one element of the iterable object evaluates 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() Correct answer
B: type()
C: bool()
D: all()
Correct Answer:
A. any()
Explanation

The built-in function any() is designed specifically for this purpose. It accepts an iterable (e.g., a list, tuple) as an argument and returns True if at least one of the elements within that iterable is truthy (evaluates to True). It will only return False if all elements are falsy or if the iterable is empty. This function effectively performs a short-circuiting logical OR operation across the elements of the iterable.

Why Incorrect

B. type() This function returns the type of an object. It does not evaluate the truthiness of elements within an iterable.
C. bool() This function converts a single argument to its Boolean value (True or False). It does not operate on an iterable to check its elements.
D. all() This function returns True only if all elements of an iterable are truthy, which is the opposite of the requirement.

References

1. Python Software Foundation. (2024). Python 3.12.3 Documentation, Built-in Functions, any(iterable). Retrieved from https://docs.python.org/3/library/functions.html#any. The documentation states: "Return True if any element of the iterable is true. If the iterable is empty, return False."

2. Python Software Foundation. (2024). Python 3.12.3 Documentation, Built-in Functions, all(iterable). Retrieved from https://docs.python.org/3/library/functions.html#all. This source clarifies the contrasting behavior of all().

3. Van Rossum, G. (2024). The Python Tutorial, Section 5.1.3. Tuples and Sequences. Python Software Foundation. Retrieved from https://docs.python.org/3/tutorial/datastructures.html. This section provides context on iterables, which are the required arguments for the any() function.

Scroll to Top