All sample questions / Python
What does print(any([])) print? Python Lists, Tuples and Slicing
Block 4, Lists, Tuples and Slicing, question 9 of the Python sample
print(any([]))
What does this print?
Show the answer and why each option is right or wrong
Answer, A. False
- A. False
- Correct. any() reports whether at least one element of the iterable is truthy, and an empty iterable has no elements at all, so the answer is False.
- B. ValueError
- Assumes any() requires at least one element to operate on and raises when given an empty iterable.
- C. True
- Confuses any() with all(), assuming an empty iterable vacuously satisfies the condition any() is checking for.
- D. None
- Assumes an empty iterable produces no boolean result at all instead of a definite False.
any() and all() are defined so that they always work on empty iterables without raising an error, they just disagree on what an empty iterable implies. any() finds no truthy element to point to, so it returns False, while all() has no falsy element to point to, so it returns True.
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.