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
Truthy and falsy values in Python
📖 Scenario: You are building a simple program that checks if different values are considered true or false in Python. This helps in understanding how Python decides whether something is 'truthy' or 'falsy' when used in conditions.
🎯 Goal: Create a list of different values, set a threshold for truthiness, check each value's truthiness using a loop, and print the results.
📋 What You'll Learn
Create a list called values with specific values
Create a variable called threshold with the value True
Use a for loop with variable val to check each value's truthiness
Print the value and whether it is truthy or falsy exactly as shown
💡 Why This Matters
🌍 Real World
Understanding truthy and falsy values helps when writing conditions in programs, such as checking if a user input is empty or if a list has items.
💼 Career
Many programming jobs require writing clear and correct conditional statements. Knowing truthy and falsy values prevents bugs and makes code easier to read.
Progress0 / 4 steps
1
Create the list of values
Create a list called values with these exact items: 0, 1, "" (empty string), "Hello", [] (empty list), and [1, 2] (list with two numbers).
Python
Hint
Remember to include all items exactly in the list, including empty string and empty list.
2
Set the threshold variable
Create a variable called threshold and set it to True.
Python
Hint
Just assign the value True to the variable threshold.
3
Check truthiness of each value
Use a for loop with variable val to go through each item in values. Inside the loop, create an if statement that checks if val is truthy. If it is, print val and the text "is truthy". Otherwise, print val and "is falsy".
Python
Hint
Use if val: to check truthiness. Remember to print exactly as shown.
4
Print the results
Run the program to print each value from values followed by whether it "is truthy" or "is falsy" exactly as in the example.
Python
Hint
Make sure your print statements match the expected output exactly, including spaces.
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