Which method removes a specific item from a set?
A
delete() B
discard() C
remove() D
pop()
Analysis & Theory
`remove()` deletes a specific item from a set and raises an error if the item doesn't exist.
What is the difference between `remove()` and `discard()`?
A
`remove()` is faster B
`discard()` returns the deleted item C
`remove()` raises an error if the item is not found; `discard()` does not D
There is no difference
Analysis & Theory
`discard()` does nothing if the item is missing, while `remove()` raises a KeyError.
What will this code output?
s = {1, 2, 3}
s.remove(2)
print(s)
A
{1, 2, 3} B
{1, 3} C
{2, 3} D
Error
Analysis & Theory
`remove(2)` deletes the number 2 from the set.
What happens if you try to `remove()` an item that is not in the set?
A
Nothing happens B
Returns False C
Raises a KeyError D
Adds the item
Analysis & Theory
`remove()` raises a KeyError if the item does not exist.
Which method safely removes an item whether or not it exists in the set?
A
delete() B
remove() C
discard() D
pop()
Analysis & Theory
`discard()` removes the item only if it exists, without error.
What does the `pop()` method do in a set?
A
Removes a specific item B
Removes and returns a random item C
Clears the set D
Raises an error if empty
Analysis & Theory
`pop()` removes and returns an arbitrary (random) item from the set.
What will happen with this code?
s = set(); s.pop()
A
Returns None B
Raises a KeyError C
Returns an empty set D
Removes nothing
Analysis & Theory
Calling `pop()` on an empty set raises a `KeyError`.
What does the `clear()` method do to a set?
A
Deletes one item B
Removes all items C
Sorts the set D
Duplicates the set
Analysis & Theory
`clear()` removes all items from the set, making it empty.
Which of the following methods does NOT exist for sets?
A
remove() B
discard() C
clear() D
delete()
Analysis & Theory
`delete()` is not a valid set method in Python.
What will this code do?
s = {'a', 'b', 'c'}
s.discard('x')
print(s)
A
Raises an error B
Removes 'x' C
Does nothing, set stays the same D
Returns False
Analysis & Theory
`discard('x')` does nothing because 'x' is not in the set — no error raised.