0
0
Pythonprogramming~20 mins

Truthy and falsy values in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Truthy and Falsy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[True, True, False, True, False, True, False, True, False]
B[False, True, True, True, False, True, False, True, False]
C[False, True, False, True, False, True, False, True, False]
D[False, True, False, True, True, True, False, True, False]
Attempts:
2 left
💡 Hint
Remember that empty containers and zero are falsy, while non-empty containers and non-zero numbers are truthy.
Predict Output
intermediate
2: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')
A
Falsy
Falsy
Truthy
Falsy
Falsy
B
Truthy
Falsy
Falsy
Falsy
Falsy
C
Falsy
Truthy
Falsy
Falsy
Falsy
D
Falsy
Falsy
Falsy
Falsy
Falsy
Attempts:
2 left
💡 Hint
Check which values are considered false in Python conditions.
Predict Output
advanced
2: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)
A
[]
[]
B
Python
[]
C
Python
Python
D
[]
Python
Attempts:
2 left
💡 Hint
Remember how 'or' and 'and' return one of their operands, not just True or False.
Predict Output
advanced
2: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))
A2
B3
C4
D5
Attempts:
2 left
💡 Hint
Only keep items where the value is truthy.
🧠 Conceptual
expert
2: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')
AFalsy
BTruthy
CTypeError
DNo output
Attempts:
2 left
💡 Hint
The __bool__ method controls the truthiness of an object.