Bird
Raised Fist0
Pythonprogramming~10 mins

Truthy and falsy values in Python - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the variable is truthy.

Python
if [1]:
    print("It's truthy!")
Drag options to blanks, or click blank then click option'
ANone
B0
CFalse
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or None which are falsy and won't enter the if block.
2fill in blank
medium

Complete the code to print 'Empty' if the list is falsy (empty).

Python
my_list = []
if not [1]:
    print('Empty')
Drag options to blanks, or click blank then click option'
Alen(my_list)
Bmy_list
Cmy_list[0]
DTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(my_list) which is 0 but not directly the list variable.
Using my_list[0] which causes an error if list is empty.
3fill in blank
hard

Fix the error in the code to check if the string is falsy.

Python
text = ''
if [1]:
    print('Not empty')
else:
    print('Empty')
Drag options to blanks, or click blank then click option'
Atext
Btext == None
Clen(text) > 0
Dtext is False
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text' alone works but the question asks to fix the error, so explicit length check is better.
Comparing string to None or False is wrong.
4fill in blank
hard

Fill both blanks to create a dictionary of words with their lengths, only for words longer than 3 characters.

Python
words = ['cat', 'house', 'dog', 'elephant']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword.startswith('e')
Dword == 'dog'
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.startswith('e') filters only words starting with 'e', not length.
Using word == 'dog' filters only 'dog', not length.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys, values as word lengths, only for words with length greater than 4.

Python
words = ['apple', 'bat', 'carrot', 'dog']
result = { [1]: [2] for w in words if [3] }
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
Clen(w) > 4
Dw.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using w.lower() instead of w.upper() for keys.
Filtering words with length less than or equal to 4.

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

  1. 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 {}.
  2. 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.
  3. Final Answer:

    An empty list [] -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    if not x: -> Option A
  4. 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

  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]
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

  1. 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.
  2. 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.
  3. Final Answer:

    Comparing with False misses some falsy values -> Option C
  4. 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

  1. Step 1: Understand filtering truthy values

    To keep only truthy values, we filter with if x which keeps values that behave like True.
  2. 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 ''.
  3. Final Answer:

    filtered = [x for x in data if x] -> Option D
  4. 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
  • Using 'is not False' misses other falsy values