Challenge - 5 Problems
Set Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediateOutput of set operations
What is the output of this Python code using sets?
Python
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print(a & b)Attempts:
2 left
๐ก Hint
The & operator finds common elements in both sets.
โ Incorrect
The & operator returns the intersection of two sets, which are elements present in both. Here, 3 and 4 are common.
โ Predict Output
intermediateWhy sets remove duplicates
What will be the output of this code?
Python
numbers = [1, 2, 2, 3, 4, 4, 4, 5] unique = set(numbers) print(len(unique))
Attempts:
2 left
๐ก Hint
Sets only keep unique items, no repeats.
โ Incorrect
The list has duplicates but the set keeps only unique values: 1,2,3,4,5, so length is 5.
โ Predict Output
advancedSet difference operation output
What does this code print?
Python
x = {10, 20, 30, 40}
y = {30, 40, 50, 60}
print(x - y)Attempts:
2 left
๐ก Hint
The - operator removes elements of y from x.
โ Incorrect
x - y returns elements in x that are not in y, which are 10 and 20.
โ Predict Output
advancedUsing sets to check membership efficiently
What is the output of this code?
Python
items = ['apple', 'banana', 'cherry'] item_set = set(items) print('banana' in item_set) print('orange' in item_set)
Attempts:
2 left
๐ก Hint
Sets allow fast membership checks.
โ Incorrect
'banana' is in the set so True, 'orange' is not so False.
๐ง Conceptual
expertWhy use sets instead of lists for membership tests?
Which is the main reason sets are preferred over lists for checking if an item exists?
Attempts:
2 left
๐ก Hint
Think about speed when checking if something is inside.
โ Incorrect
Sets use a special method to check membership quickly, usually faster than lists which check one by one.
