Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q4 of 15
Python - Data Types as Values
What will be the output of the following code?
values = [0, 1, '', 'hello', [], [1]]
result = [bool(v) for v in values]
print(result)
A[False, False, False, False, False, False]
B[True, True, True, True, True, True]
C[False, True, False, True, False, True]
D[True, False, True, False, True, False]
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate each value's truthiness

    0, empty string '', and empty list [] are falsy; 1, 'hello', and [1] are truthy.
  2. Step 2: Convert each value to bool

    bool(0) = False, bool(1) = True, bool('') = False, bool('hello') = True, bool([]) = False, bool([1]) = True.
  3. Final Answer:

    [False, True, False, True, False, True] -> Option C
  4. Quick Check:

    Falsy = 0, '', [] [OK]
Quick Trick: Empty containers and zero are falsy, others truthy [OK]
Common Mistakes:
MISTAKES
  • Assuming non-empty list is falsy
  • Confusing empty string as truthy
  • Mixing up bool conversion results

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes