0
0
Pythonprogramming~10 mins

Set membership testing 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 5 is in the set.

Python
numbers = {1, 3, 5, 7}
if 5 [1] numbers:
    print("Found 5")
Drag options to blanks, or click blank then click option'
Ain
Bnot in
Ccontains
Dinside
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'not in' instead of 'in'.
Using 'contains' which is not a Python keyword.
Using 'inside' which is not valid syntax.
2fill in blank
medium

Complete the code to check if 'apple' is NOT in the set.

Python
fruits = {'banana', 'orange', 'grape'}
if 'apple' [1] fruits:
    print("Apple is missing")
Drag options to blanks, or click blank then click option'
Ahas
Bin
Ccontains
Dnot in
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'in' instead of 'not in'.
Using 'contains' or 'has' which are not Python keywords.
3fill in blank
hard

Fix the error in the code to correctly test membership.

Python
colors = {'red', 'blue', 'green'}
if 'yellow' [1] colors:
    print("Yellow found")
else:
    print("Yellow not found")
Drag options to blanks, or click blank then click option'
Ain
Bnot in
Cinside
Dcontains
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'inside' or 'contains' which cause syntax errors.
Using 'not in' when checking for presence.
4fill in blank
hard

Fill both blanks to create a set of squares for numbers greater than 3.

Python
squares = {x[1]2 for x in range(1, 7) if x [2] 3}
Drag options to blanks, or click blank then click option'
A**
B+
C>
D<
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '+' instead of '**' for squaring.
Using '<' instead of '>' for filtering.
5fill in blank
hard

Fill all three blanks to create a set of uppercase words longer than 4 letters.

Python
result = {word[1] for word in words if len(word) [2] 4 and word.isalpha() [3] True}
Drag options to blanks, or click blank then click option'
A.upper()
B>
C==
D<
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '+' or other operators instead of '.upper()'.
Using '<' instead of '>' for length check.
Using '=' instead of '==' for comparison.