Exercise: Python

Questions for: Sets

What is the result of the expression set('programming') - set('python')?
A:
{'r', 'g', 'a', 'm', 'i'}
B:
{'p', 'y', 't'}
C:
{'r', 'o', 'm', 'i', 'n'}
D:
{'p', 'r', 'o', 'g', 'a', 'm', 'i', 'n'}
Answer: A
The '-' operator performs the set difference, giving elements that are in the first set but not in the second.
What is the result of the expression set('abc') ^ set('cde')?
A:
{'a', 'b', 'c', 'd', 'e'}
B:
{'a', 'b'}
C:
{'c'}
D:
{'a', 'b', 'd', 'e'}
Answer: D
The '^' operator performs the symmetric difference, giving elements that are unique to each set.
What is the purpose of the set.isdisjoint(other_set) method?
A:
Checks if two sets are equal.
B:
Checks if two sets have no elements in common.
C:
Checks if one set is a subset of another.
D:
Checks if one set is a superset of another.
Answer: B
The isdisjoint() method checks if two sets have no elements in common.
Which method is used to remove and return the last element from a set?
A:
set.pop()
B:
set.remove_last()
C:
set.delete_last()
D:
set.discard_last()
Answer: A
The pop() method removes and returns an arbitrary element, which is the last element in a set.
What is the result of the expression set([1, 2, 3]) & set([2, 3, 4])?
A:
{1, 2, 3, 4}
B:
{2, 3}
C:
{}
D:
{1, 4}
Answer: B
The '&' operator performs the intersection of two sets, giving elements common to both sets.
Ad Slot (Above Pagination)
Quiz