0
0
Pythonprogramming~10 mins

List length and membership test 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 print the length of the list.

Python
numbers = [1, 2, 3, 4, 5]
print(len([1]))
Drag options to blanks, or click blank then click option'
Alist
Bnumbers
Clen
D5
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Putting the number 5 inside len() instead of the list variable.
Using the word 'list' instead of the list variable name.
2fill in blank
medium

Complete the code to check if the number 3 is in the list.

Python
fruits = ['apple', 'banana', 'cherry']
if [1] in fruits:
    print('Found it!')
Drag options to blanks, or click blank then click option'
A'3'
B'banana'
C3
D'cherry'
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Checking for the number 3 instead of a fruit name.
Using quotes incorrectly or missing quotes around the string.
3fill in blank
hard

Fix the error in the code to correctly check if 'cat' is in the list.

Python
animals = ['dog', 'cat', 'bird']
if 'cat' [1] animals:
    print('Cat found!')
Drag options to blanks, or click blank then click option'
Ain
Bnot in
Cis in
Dcontains
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'is in' or 'contains' which cause syntax errors.
Using 'not in' which checks the opposite condition.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, but only for words longer than 3 letters.

Python
words = ['sun', 'moon', 'star', 'sky']
lengths = { [1] : [2] for word in words if len(word) > 3 }
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
Cword.upper()
Dword.lower()
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using word.upper() or word.lower() as keys instead of the original word.
Using the word variable as value instead of its length.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys, their lengths as values, but only include words with length greater than 3.

Python
words = ['tree', 'sky', 'river', 'sun']
result = { [1] : [2] for [3] in words if len([3]) > 3 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Cword
Ditem
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using different variable names inconsistently.
Not converting words to uppercase for keys.
Using the wrong variable in the condition.