0
0
Pythonprogramming~20 mins

Set membership testing in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Set Membership Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of set membership test with strings
What is the output of this Python code?
Python
fruits = {'apple', 'banana', 'cherry'}
print('banana' in fruits)
print('orange' in fruits)
AFalse\nTrue
BTrue\nTrue
CTrue\nFalse
DFalse\nFalse
Attempts:
2 left
๐Ÿ’ก Hint
Check if each fruit is inside the set using the 'in' keyword.
โ“ Predict Output
intermediate
2:00remaining
Set membership with numbers and types
What will this code print?
Python
numbers = {1, 2, 3, 4}
print(3 in numbers)
print('3' in numbers)
ATrue\nTrue
BFalse\nFalse
CFalse\nTrue
DTrue\nFalse
Attempts:
2 left
๐Ÿ’ก Hint
Remember that 3 (int) and '3' (string) are different types.
โ“ Predict Output
advanced
2:00remaining
Set membership with mutable elements
What error does this code raise?
Python
my_set = {1, 2, [3, 4]}
print(2 in my_set)
ATypeError
BSyntaxError
CKeyError
DNo error, prints True
Attempts:
2 left
๐Ÿ’ก Hint
Sets cannot contain mutable elements like lists.
โ“ Predict Output
advanced
2:00remaining
Set membership with nested tuples
What is the output of this code?
Python
nested_set = {(1, 2), (3, 4)}
print((1, 2) in nested_set)
print((2, 1) in nested_set)
AFalse\nTrue
BTrue\nFalse
CTrue\nTrue
DFalse\nFalse
Attempts:
2 left
๐Ÿ’ก Hint
Tuples are ordered, so (1, 2) is different from (2, 1).
๐Ÿง  Conceptual
expert
2:00remaining
Why does 'in' membership test run faster on sets?
Why is checking membership with 'in' faster on a set than on a list?
ASets use hashing to check membership in constant time, while lists check each item one by one.
BSets store elements in sorted order, so membership uses binary search.
CSets convert elements to strings before checking membership, making it faster.
DSets use multiple threads internally to check membership faster.
Attempts:
2 left
๐Ÿ’ก Hint
Think about how sets organize data internally compared to lists.