0
0
Pythonprogramming~10 mins

Membership operators (in, not in) 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 'apple' is in the list.

Python
fruits = ['apple', 'banana', 'cherry']
if 'apple' [1] fruits:
    print('Found apple!')
Drag options to blanks, or click blank then click option'
Anot in
Bcontains
Cin
Dhas
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'not in' instead of 'in'.
Using 'contains' which is not a Python operator.
2fill in blank
medium

Complete the code to check if 'dog' is NOT in the list.

Python
animals = ['cat', 'bird', 'fish']
if 'dog' [1] animals:
    print('Dog is not here!')
Drag options to blanks, or click blank then click option'
Acontains
Bnot in
Cin
Dhas
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'in' instead of 'not in'.
Using 'contains' which is not a Python operator.
3fill in blank
hard

Fix the error in the code to check membership correctly.

Python
colors = ['red', 'green', 'blue']
if 'yellow' [1] colors:
    print('Yellow is in the list')
else:
    print('Yellow is not in the list')
Drag options to blanks, or click blank then click option'
Ain
Bnot in
Cinside
Dhas
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'not in' when the logic requires 'in'.
Using invalid operators like 'inside' or 'has'.
4fill in blank
hard

Fill both blanks to create a dictionary of words and their lengths only if length is greater than 3.

Python
words = ['sun', 'moon', 'star', 'sky']
lengths = {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>'.
Using 'word' instead of 'len(word)'.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values only if value is not in the list.

Python
values = ['a', 'b', 'c']
result = { [1]: [2] for [3] in values if [2] not in ['b', 'c']}
Drag options to blanks, or click blank then click option'
Av.upper()
Bv
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'k' as loop variable when it is not defined.
Mixing keys and values incorrectly.