Bird
0
0

What will be the output of the following code?

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

    0, empty string '', and empty list [] are falsy, so their bool() is False. Non-zero numbers, non-empty strings, and non-empty lists are truthy, so bool() is True.
  2. Step 2: Map each value to bool()

    values = [0(False), 1(True), ''(False), 'Python'(True), [] (False), [1, 2](True)]
  3. Final Answer:

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

    Falsy are 0, '', [] = False [OK]
Quick Trick: Empty or zero values are False, others True in bool() [OK]
Common Mistakes:
MISTAKES
  • Assuming all numbers are True
  • Thinking empty strings are True
  • Confusing empty and non-empty lists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes