You have learned about sets: unordered collections of unique items. You know how to create them, add to them, remove from them, and perform mathematical operations like union and intersection.
Now it is time to test your understanding. This quiz will help you see what has settled into your memory and what needs another look.
Each question is followed by the correct answer and a short explanation. Try to answer before peeking.
🕯️ Magic Note
Sets are one of the most underutilized data structures in Python. Many programmers use lists for everything and miss out on the speed and elegance of sets. If you master sets, you will write faster, cleaner code.
Question 1: Creating an Empty Set
How do you create an empty set in Python?
- ★ Option A: {}
- ★ Option B: set()
- ★ Option C: []
- ★ Option D: empty()
✨ Reveal Answer & Explanation
Correct Answer: B (set())
{} creates an empty dictionary, not a set. [] creates an empty list. empty() does not exist. To create an empty set, you must use the set() constructor.
This is one of the most common beginner mistakes with sets. Remember: curly braces with nothing inside = dictionary. Curly braces with items but no colons = set.
Question 2: Removing Duplicates
What is the output of set([1, 2, 2, 3, 1, 4, 2])?
- ★ Option A: [1, 2, 3, 4]
- ★ Option B: {1, 2, 3, 4}
- ★ Option C: (1, 2, 3, 4)
- ★ Option D: {1, 2, 2, 3, 1, 4, 2}
✨ Reveal Answer & Explanation
Correct Answer: B ({1, 2, 3, 4})
set() converts the list into a set. Sets automatically remove duplicate values. The result is a set containing each unique number only once. The order may vary because sets are unordered.
Note: The output uses curly braces { } because it is a set. It is not a list (no square brackets) and not a tuple (no parentheses).
Question 3: Adding vs Updating
What is the difference between .add() and .update() on a set?
- ★ Option A: .add() adds one item, .update() adds multiple items
- ★ Option B: .add() adds multiple items, .update() adds one item
- ★ Option C: Both do the same thing
- ★ Option D: .update() modifies the set, .add() creates a new set
✨ Reveal Answer & Explanation
Correct Answer: A (.add() adds one item, .update() adds multiple items)
.add(x) adds a single item x to the set. .update(iterable) takes any iterable (list, tuple, set, string) and adds all its items to the set.
Example:
my_set.add(4) adds the number 4.
my_set.update([4, 5, 6]) adds 4, 5, and 6.
Question 4: Union Operator
Given A = {1, 2, 3} and B = {3, 4, 5}, what is A | B?
- ★ Option A: {3}
- ★ Option B: {1, 2, 3, 4, 5}
- ★ Option C: {1, 2, 4, 5}
- ★ Option D: {1, 2, 3, 3, 4, 5}
✨ Reveal Answer & Explanation
Correct Answer: B ({1, 2, 3, 4, 5})
The union operator | combines both sets and removes duplicates. It returns a new set containing all items that appear in either A or B (or both).
Union is like “OR” for sets. The item 3 appears in both, but it is included only once because sets do not allow duplicates.
Question 5: Intersection Operator
Given A = {1, 2, 3, 4} and B = {3, 4, 5, 6}, what is A & B?
- ★ Option A: {3, 4}
- ★ Option B: {1, 2, 5, 6}
- ★ Option C: {1, 2, 3, 4, 5, 6}
- ★ Option D: {}
✨ Reveal Answer & Explanation
Correct Answer: A ({3, 4})
The intersection operator & returns a new set containing only the items that appear in both sets. In this case, 3 and 4 are in both A and B.
Intersection is like “AND” for sets. Only items that exist in both sets survive.
Question 6: Difference Operator
Given A = {1, 2, 3, 4} and B = {3, 4, 5, 6}, what is A – B?
- ★ Option A: {3, 4}
- ★ Option B: {1, 2, 5, 6}
- ★ Option C: {1, 2}
- ★ Option D: {5, 6}
✨ Reveal Answer & Explanation
Correct Answer: C ({1, 2})
The difference operator – returns a new set containing items that are in the first set but not in the second set.
A – B means “items in A that are not in B”. So 1 and 2 are in A but not in B. Items 3 and 4 are removed because they appear in B.
Note that B – A would be different: it returns {5, 6}.
Question 7: Removing an Item Safely
Which method removes an item from a set without causing an error if the item does not exist?
- ★ Option A: .remove()
- ★ Option B: .pop()
- ★ Option C: .discard()
- ★ Option D: .delete()
✨ Reveal Answer & Explanation
Correct Answer: C (.discard())
.remove(x) removes x but raises a KeyError if x is not in the set.
.discard(x) removes x if it exists and does nothing if it does not exist.
.pop() removes and returns an arbitrary item (raises error if set is empty).
.delete() does not exist.
Use .discard() when you are not sure if an item is in the set and you do not want to handle an exception.
Question 8: Set Membership Check
How do you check if the value 5 exists in a set called numbers?
- ★ Option A: numbers[5]
- ★ Option B: numbers.find(5)
- ★ Option C: 5 in numbers
- ★ Option D: numbers.contains(5)
✨ Reveal Answer & Explanation
Correct Answer: C (5 in numbers)
The in operator checks for membership in sets (and lists, tuples, dictionaries, and strings). It returns True if the item is in the set, False otherwise.
Sets are optimized for fast membership testing. Checking in on a set is much faster than checking on a list, especially for large collections.
Question 9: Can a Set Contain a List?
What happens when you try to create a set containing a list, like {[1, 2], [3, 4]}?
- ★ Option A: It creates a set with two lists inside
- ★ Option B: It raises a TypeError
- ★ Option C: It creates a set of the list elements {1, 2, 3, 4}
- ★ Option D: It creates an empty set
✨ Reveal Answer & Explanation
Correct Answer: B (It raises a TypeError)
Set items must be immutable (hashable). Lists are mutable, so they cannot be used as set items. Attempting to do so raises a TypeError: unhashable type: ‘list’.
The same applies to dictionaries and other sets. But tuples (immutable) are fine: {(1, 2), (3, 4)} works perfectly.
Question 10: Removing Duplicates While Preserving Order
You have a list [3, 1, 2, 1, 3, 2, 4]. You want to remove duplicates but keep the first occurrence order [3, 1, 2, 4]. What is the best approach?
- ★ Option A: list(set(my_list))
- ★ Option B: set(my_list)
- ★ Option C: list(dict.fromkeys(my_list))
- ★ Option D: Use a loop with a new list
✨ Reveal Answer & Explanation
Correct Answer: C (list(dict.fromkeys(my_list)))
list(set(my_list)) removes duplicates but does not preserve order because sets are unordered.
set(my_list) returns a set, not a list.
list(dict.fromkeys(my_list)) uses a dictionary (which preserves insertion order from Python 3.7+) to remove duplicates while keeping the original order.
A manual loop also works but is longer and less elegant.
The one-liner list(dict.fromkeys(my_list)) is the Pythonic way to remove duplicates while preserving order.
Mini Challenges (Optional)
Try these small coding challenges to deepen your understanding.
Challenge 1
A = {1, 2, 3}
B = {2, 3, 4}
print(A ^ B)
Challenge 2
names = [“Ali”, “Sara”, “Ali”, “Reza”, “Sara”, “Mina”]
Challenge 3
list1 = [1, 3, 5, 7, 9]
list2 = [1, 2, 3, 4, 5]
✨ Reveal Challenge Answers
Challenge 1 Answer:
A ^ B is symmetric difference (items in either set but not both). Result: {1, 4}.
Challenge 2 Answer:
list(dict.fromkeys(names)) returns [‘Ali’, ‘Sara’, ‘Reza’, ‘Mina’].
Challenge 3 Answer:
set(list1) & set(list2) or set(list1).intersection(list2) returns {1, 3, 5}.
Self-Assessment
After completing this quiz, ask yourself:
- ✓ Can you create an empty set correctly?
- ✓ Do you understand the difference between .add() and .update()?
- ✓ Do you know the operators for union (|), intersection (&), and difference (–)?
- ✓ Can you safely remove an item from a set without worrying about errors?
- ✓ Do you know what can and cannot be inside a set?
💡 If you missed any questions, go back to the Sets lesson (Lesson 12) and review the relevant sections. Each mistake is a signpost pointing to what you need to practice next. That is not failure. That is learning.
⚡ Whisper
A set does not remember order. It does not remember how many times you added the same item. It only remembers uniqueness. Like a quiet mind that filters out repetition and keeps only what is essential. The set says: “I have heard this before. I will not store it again.” This is its strength and its limitation. Use sets when you need uniqueness and speed. Do not use them when order matters. Choose the right tool for the right whisper. That is the mark of a thoughtful programmer.