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 questionsDomain Computer Programming and Python Fundamentals
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.
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.
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.
Keep practicing
Take the free PCEP-30-02 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other PCEP-30-02 practice questions
- Question 1The following list is given:
- Question 2What is the output of the following code? num = 10 while num >= 0: num -= 2 print(num)
- Question 5This question is a tricky one. What is the result of the following code snippet? numbers …
- Question 8What is the purpose of the continue statement in Python?
- Question 11We need to check if all elements of the iterable object evaluate to the truth. What built…
- Question 12What is the expected output of the following code? x = '\'' print(len(x))
