Challenge - 5 Problems
Set Membership Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2: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)Attempts:
2 left
๐ก Hint
Check if each fruit is inside the set using the 'in' keyword.
โ Incorrect
The set contains 'banana' but not 'orange', so 'banana' in fruits is True and 'orange' in fruits is False.
โ Predict Output
intermediate2: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)Attempts:
2 left
๐ก Hint
Remember that 3 (int) and '3' (string) are different types.
โ Incorrect
3 is in the set, but the string '3' is not, so the first print is True and the second is False.
โ Predict Output
advanced2:00remaining
Set membership with mutable elements
What error does this code raise?
Python
my_set = {1, 2, [3, 4]}
print(2 in my_set)Attempts:
2 left
๐ก Hint
Sets cannot contain mutable elements like lists.
โ Incorrect
Lists are mutable and cannot be added to a set, so Python raises a TypeError.
โ Predict Output
advanced2: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)Attempts:
2 left
๐ก Hint
Tuples are ordered, so (1, 2) is different from (2, 1).
โ Incorrect
The tuple (1, 2) is in the set, but (2, 1) is not, so the output is True then False.
๐ง Conceptual
expert2:00remaining
Why does 'in' membership test run faster on sets?
Why is checking membership with 'in' faster on a set than on a list?
Attempts:
2 left
๐ก Hint
Think about how sets organize data internally compared to lists.
โ Incorrect
Sets use a hash table which allows them to find elements quickly without checking every item, unlike lists which check each element in order.