All sample questions / Python

What does print(all([1, 2, 0])) print? Python Lists, Tuples and Slicing

Block 4, Lists, Tuples and Slicing, question 19 of the Python sample

print(all([1, 2, 0]))

What does this print?

  1. A. None
  2. B. False
  3. C. True
  4. D. 0
Show the answer and why each option is right or wrong

Answer, B. False

A. None
Assumes encountering a falsy element makes all() report that there is no valid answer instead of a definite False.
B. False
Correct. all() requires every element to be truthy, and the 0 in this list is falsy, so the overall result is False even though the other two elements are truthy.
C. True
Checks only that the list itself is non empty instead of checking the truthiness of each individual element.
D. 0
Assumes all() returns the first falsy element it finds instead of a plain boolean.

all() short circuits as soon as it finds a single falsy element and returns False at that point, regardless of how many truthy elements came before or after it. A single 0 anywhere in the iterable is enough to make the whole result False.

The answer above is what the CPython interpreter printed when the snippet was run, not what someone expected it to do.

This question comes from a bank of 600 Python interview questions, all built the same way, sold as one PDF with the full answer key.

Get the 600 Python questions, 9 euros

Or SQL, Python, pandas and NumPy together, 1,636 questions, 19 euros

Or play the free Python sample, twenty questions in the browser, also as a free PDF.

Previous question Next question

Did this save you a lookup? Buy me a coffee on Ko-fi.