PCEP-30-02 · Question #255
Consider the following code snippet: 1 w = bool(23) 2 x = bool('') 3 y = bool(' ') 4 z = bool([False]) Which of the variables will contain False?
The correct answer is C. x. x contains False because bool('') evaluates an empty string, and in Python, empty containers and sequences (empty string, list, dict, tuple) are always falsy. Why the distractors are wrong: A (z): bool([False]) evaluates to True - the list contains False, but the list itself is…
Question
Options
- Az
- Bw
- Cx
- Dy
How the community answered
(33 responses)- A15% (5)
- B9% (3)
- C73% (24)
- D3% (1)
Explanation
x contains False because bool('') evaluates an empty string, and in Python, empty containers and sequences (empty string, list, dict, tuple) are always falsy.
Why the distractors are wrong:
- A (z):
bool([False])evaluates toTrue- the list containsFalse, but the list itself is non-empty, and any non-empty collection is truthy. Python checks the container, not its contents. - B (w):
bool(23)isTrue- any non-zero number is truthy in Python. - D (y):
bool(' ')isTrue- a string with a space character is not empty; it has length 1, so it's truthy.
Memory tip: Python's falsy values follow an "empty or nothing" rule - 0, '', [], {}, None, and False itself are all falsy. A single space ' ' is a character, not nothing, so it passes. And remember: bool([False]) tricks many - Python doesn't peek inside the list, it just asks "does this list exist and have items?"
Community Discussion
No community discussion yet for this question.