Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What does it mean when a value is truthy in Python?
A truthy value is any value that Python treats as True when used in a condition, like in an if statement. For example, non-zero numbers and non-empty strings are truthy.
Click to reveal answer
beginner
What is a falsy value in Python?
A falsy value is any value that Python treats as False in a condition. Examples include 0, '' (empty string), [] (empty list), None, and False itself.
Click to reveal answer
beginner
Which of these values is falsy in Python? A) 1 B) '' C) 'False' D) [1, 2]
The correct answer is B) ''. An empty string is falsy. The others are truthy because 1 is non-zero, 'False' is a non-empty string, and [1, 2] is a non-empty list.
Click to reveal answer
intermediate
How does Python decide if a value is truthy or falsy in an if statement?
Python uses the built-in bool() function internally to check the truthiness of a value. If bool(value) returns True, the value is truthy; if it returns False, the value is falsy.
Click to reveal answer
beginner
Give two examples of truthy and two examples of falsy values in Python.
Which of the following is considered falsy in Python?
A''
B1
C'False'
D[0]
✗ Incorrect
An empty string ('') is falsy. 'False' is a non-empty string, so it is truthy.
What does Python use internally to check if a value is truthy or falsy?
Aint()
Bbool()
Cfloat()
Dstr()
✗ Incorrect
Python uses the bool() function to determine the truthiness of a value.
Which of these values is truthy?
AFalse
BNone
C[1, 2, 3]
D0
✗ Incorrect
Non-empty lists like [1, 2, 3] are truthy.
Is the number -5 truthy or falsy in Python?
ANone of the above
BFalsy
CDepends on context
DTruthy
✗ Incorrect
Any non-zero number, including negative numbers, is truthy.
Which of these is NOT a falsy value in Python?
A' ' (a space)
B[]
C0.0
DNone
✗ Incorrect
A string with a space (' ') is non-empty, so it is truthy.
Explain what truthy and falsy values are in Python and give examples of each.
Think about how Python treats values in conditions like if statements.
You got /4 concepts.
How does Python determine if a value is truthy or falsy when used in a condition?
Consider what happens inside an if statement.
You got /3 concepts.
Practice
(1/5)
1. Which of the following values is considered falsy in Python?
easy
A. An empty list []
B. A non-empty string 'hello'
C. The integer 5
D. A non-empty dictionary {'key': 'value'}
Solution
Step 1: Understand falsy values in Python
Falsy values are those that behave like False in conditions. Common falsy values include None, 0, empty sequences like [], '', and empty collections like {}.
Step 2: Check each option
An empty list [] is an empty list, which is falsy. Options B, C, and D are non-empty and thus truthy.
Final Answer:
An empty list [] -> Option A
Quick Check:
Empty list is falsy = A [OK]
Hint: Empty containers and zero are falsy, others are truthy [OK]
Common Mistakes:
Thinking non-empty collections are falsy
Confusing zero with non-zero numbers
Assuming all strings are falsy
2. Which of the following is the correct way to check if a variable x is falsy in Python?
easy
A. if not x:
B. if x == False:
C. if x is False:
D. if x = False:
Solution
Step 1: Understand Python falsy check syntax
To check if a value is falsy, use if not x: which tests if x behaves like False in a boolean context.
Step 2: Analyze each option
if x == False: compares x to False but misses other falsy values like 0 or ''. if x is False: checks identity with False, which is too strict. if x = False: has a syntax error (= instead of ==). if not x: is the correct idiomatic way.
Final Answer:
if not x: -> Option A
Quick Check:
Use if not x: to check falsy [OK]
Hint: Use 'if not x:' to test falsy values in Python [OK]
Common Mistakes:
Using assignment '=' instead of comparison '=='
Checking identity with 'is False' instead of truthiness
Comparing directly to False misses other falsy values
3. What will be the output of the following code?
values = [0, 1, '', 'Python', [], [1, 2]]
result = [bool(v) for v in values]
print(result)
medium
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]
Solution
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.
[False, True, False, True, False, True] -> Option B
Quick Check:
Falsy are 0, '', [] = False [OK]
Hint: Empty or zero values are False, others True in bool() [OK]
Common Mistakes:
Assuming all numbers are True
Thinking empty strings are True
Confusing empty and non-empty lists
4. The following code is intended to print "Falsy" if val is falsy, but it always prints "Truthy". What is the error?
val = 0
if val == False:
print("Falsy")
else:
print("Truthy")
medium
A. Using '==' instead of 'is' for comparison
B. The variable 'val' should be converted to bool first
C. Comparing with False misses some falsy values
D. No error; the code works correctly
Solution
Step 1: Understand comparison with False
Using val == False only matches if val equals exactly False or behaves equal to it. But some falsy values like 0 compare equal to False, so this seems correct here.
Step 2: Check why it prints "Truthy"
Actually, 0 == False is True, so it should print "Falsy". If it prints "Truthy", likely the code is different or the question expects the explanation that comparing with False is not reliable for all falsy values like empty containers or None.
Final Answer:
Comparing with False misses some falsy values -> Option C
Quick Check:
Use 'if not val:' to catch all falsy [OK]
Hint: Use 'if not val:' to catch all falsy values, not '== False' [OK]
Common Mistakes:
Thinking '==' always works for falsy check
Using 'is' instead of '==' incorrectly
Not realizing empty containers are falsy but not equal to False
5. You want to filter out all falsy values from a list data = [0, 1, '', 'text', [], [1], None]. Which code snippet correctly creates a new list with only truthy values?
hard
A. filtered = [x for x in data if x is not False]
B. filtered = [x for x in data if x == True]
C. filtered = [x for x in data if bool(x) == False]
D. filtered = [x for x in data if x]
Solution
Step 1: Understand filtering truthy values
To keep only truthy values, we filter with if x which keeps values that behave like True.
Step 2: Analyze each option
filtered = [x for x in data if x] uses if x, which is correct. filtered = [x for x in data if x == True] checks x == True, which excludes truthy but not exactly True values (like 1). filtered = [x for x in data if bool(x) == False] keeps falsy values (bool(x) == False). filtered = [x for x in data if x is not False] excludes only False but keeps other falsy values like 0 or ''.
Final Answer:
filtered = [x for x in data if x] -> Option D
Quick Check:
Use 'if x' to filter truthy values [OK]
Hint: Use list comprehension with 'if x' to keep truthy values [OK]
Common Mistakes:
Using '== True' excludes some truthy values
Filtering with 'bool(x) == False' keeps falsy values