Recall & Review
beginner
How do you add a single element to a set in Python?
Use the
add() method. For example, my_set.add(5) adds the element 5 to my_set.Click to reveal answer
beginner
What method removes an element from a set and raises an error if the element is not present?
The
remove() method removes an element but raises a KeyError if the element is not found.Click to reveal answer
intermediate
What is the difference between
discard() and remove() methods in sets?discard() removes an element if it exists but does nothing if it doesn't. remove() raises an error if the element is missing.Click to reveal answer
beginner
How can you add multiple elements to a set at once?
Use the
update() method with an iterable. For example, my_set.update([1, 2, 3]) adds 1, 2, and 3 to my_set.Click to reveal answer
beginner
What happens if you try to add a duplicate element to a set?
Nothing changes because sets only keep unique elements. Adding a duplicate does not add a new element.
Click to reveal answer
Which method adds a single element to a set without error if the element already exists?
✗ Incorrect
The
add() method adds an element and does nothing if it already exists, so no error occurs.Which method removes an element from a set but does NOT raise an error if the element is missing?
✗ Incorrect
discard() removes the element if present but does nothing if it is not, so no error is raised.What will happen if you call
remove() on an element not in the set?✗ Incorrect
remove() raises a KeyError if the element is not found in the set.How do you add multiple elements to a set at once?
✗ Incorrect
update() takes an iterable and adds all its elements to the set.If you add a duplicate element to a set, what happens?
✗ Incorrect
Sets only keep unique elements, so duplicates are ignored.
Explain how to add and remove elements from a Python set, including how to handle missing elements safely.
Think about methods that add one or many elements, and methods that remove with or without errors.
You got /5 concepts.
Describe what happens when you add duplicate elements to a set and why sets behave this way.
Consider the nature of sets as collections of unique items.
You got /3 concepts.