Challenge - 5 Problems
Truthy and Falsy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a conditional check with different values
What is the output of this Python code snippet?
Python
values = [0, 1, '', 'Hello', [], [1, 2], None, True, False] results = [bool(v) for v in values] print(results)
Attempts:
2 left
💡 Hint
Remember that empty containers and zero are falsy, while non-empty containers and non-zero numbers are truthy.
✗ Incorrect
In Python, 0, empty strings '', empty lists [], None, and False are falsy values. All others are truthy.
❓ Predict Output
intermediate2:00remaining
Result of if statement with different falsy values
What will be printed by this code?
Python
test_values = [None, 0, '', [], False] for v in test_values: if v: print('Truthy') else: print('Falsy')
Attempts:
2 left
💡 Hint
Check which values are considered false in Python conditions.
✗ Incorrect
All values in the list are falsy in Python, so the else branch runs each time.
❓ Predict Output
advanced2:00remaining
Output of logical operations with truthy and falsy values
What is the output of this code?
Python
a = [] b = 'Python' print(a or b) print(a and b)
Attempts:
2 left
💡 Hint
Remember how 'or' and 'and' return one of their operands, not just True or False.
✗ Incorrect
For 'or', Python returns the first truthy value or the last value if none are truthy. For 'and', it returns the first falsy value or the last value if all are truthy.
❓ Predict Output
advanced2:00remaining
Length of dictionary after filtering falsy values
What is the length of the dictionary after this code runs?
Python
data = {'a': 0, 'b': 1, 'c': '', 'd': 'text', 'e': None}
filtered = {k: v for k, v in data.items() if v}
print(len(filtered))Attempts:
2 left
💡 Hint
Only keep items where the value is truthy.
✗ Incorrect
Values 0, '', and None are falsy and filtered out. Only 'b':1 and 'd':'text' remain.
🧠 Conceptual
expert2:00remaining
Understanding the behavior of custom objects in boolean context
Consider this class and code. What will be the output?
Python
class MyObj: def __bool__(self): return False obj = MyObj() if obj: print('Truthy') else: print('Falsy')
Attempts:
2 left
💡 Hint
The __bool__ method controls the truthiness of an object.
✗ Incorrect
The __bool__ method returns False, so the object is falsy in boolean context.