0
0
Pythonprogramming~10 mins

Truthy and falsy values in Python - Interactive Code Practice

Choose your learning style9 modes available
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.