Exercise: Python

Questions for: Sets

What is the purpose of the set.remove(element) method?
A:
Removes the specified element from the set.
B:
Removes the last element from the set.
C:
Removes all occurrences of the specified element.
D:
Raises a TypeError since sets are immutable.
Answer: A
The remove() method removes the specified element from the set.
Which method is used to add an element to a set?
A:
set.append(element)
B:
set.insert(element)
C:
set.add(element)
D:
set.update(element)
Answer: C
The add() method is used to add a single element to a set.
What is the result of the expression set('python')?
A:
{'p', 'y', 't', 'h', 'o', 'n'}
B:
('p', 'y', 't', 'h', 'o', 'n')
C:
['p', 'y', 't', 'h', 'o', 'n']
D:
('python')
Answer: A
The set() constructor converts a string into a set of its characters.
Ad Slot (Above Pagination)
Quiz