0
0
Pythonprogramming~20 mins

Why sets are used in Python - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Set Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output 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)
A{1, 2}
B{5, 6}
C{3, 4}
D{1, 2, 3, 4, 5, 6}
Attempts:
2 left
๐Ÿ’ก Hint
The & operator finds common elements in both sets.
โ“ Predict Output
intermediate
2:00remaining
Why 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))
A5
B8
C4
D7
Attempts:
2 left
๐Ÿ’ก Hint
Sets only keep unique items, no repeats.
โ“ Predict Output
advanced
2:00remaining
Set difference operation output
What does this code print?
Python
x = {10, 20, 30, 40}
y = {30, 40, 50, 60}
print(x - y)
A{10, 20}
B{30, 40}
C{50, 60}
D{10, 20, 30, 40, 50, 60}
Attempts:
2 left
๐Ÿ’ก Hint
The - operator removes elements of y from x.
โ“ Predict Output
advanced
2:00remaining
Using 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)
ATrue\nTrue
BTrue\nFalse
CFalse\nTrue
DFalse\nFalse
Attempts:
2 left
๐Ÿ’ก Hint
Sets allow fast membership checks.
๐Ÿง  Conceptual
expert
2:00remaining
Why use sets instead of lists for membership tests?
Which is the main reason sets are preferred over lists for checking if an item exists?
ALists can only store numbers, sets can store any type.
BSets keep elements in order, lists do not.
CLists use less memory than sets.
DSets allow faster membership tests than lists.
Attempts:
2 left
๐Ÿ’ก Hint
Think about speed when checking if something is inside.