Use these questions to review Python sets after reading the Python Set tutorial. The page begins with creation and uniqueness, then moves through methods, mathematical set operations, membership, frozenset and common mistakes.
set() for an empty set; {} creates an empty dictionary.Try each problem before opening or reading the solution.
my_set = set()
print(type(my_set))<class 'set'>numbers = {10, 20, 30, 40, 50}
print(numbers)The printed order should not be treated as fixed because sets are unordered.
range().numbers = set(range(5, 50, 10))
print(numbers)See range() for more range examples.
names = {'Alex', 'Ronald', 'John'}
print(*names)The display order may differ between runs or environments.
values = [10, 20, 20, 30]
unique_values = set(values)
print(unique_values)Duplicate values are removed when the set is created.
numbers = {1, 1, 2, 2, 3}
print(numbers)
print(len(numbers)){1, 2, 3}
3single = {'Alex'}
print(type(single))<class 'set'>max(), min(), sum() and len() with a numeric set.numbers = {8, 2, 4, 3, 7}
print(max(numbers))
print(min(numbers))
print(sum(numbers))
print(len(numbers))8
2
24
5clear() do? Does it delete the set variable?numbers = {1, 2, 3}
numbers.clear()
print(numbers)
print(type(numbers))set()
<class 'set'>clear() removes all elements but keeps the set object.
count() method?No. A set stores each distinct value at most once, so it does not provide count(). Use membership testing such as value in my_set to check whether a value exists.
my_set = {'a', 'b', 'c'}
print('b' in my_set)items = {'a', 'b', 'c', 'd'}
items.difference_update({'b', 'd'})
print(items)See difference_update().
groups = {frozenset({1, 2}), frozenset({3, 4})}
print(groups)Yes. A frozenset is immutable and hashable, so it can be a set element.
A = {1, 2, 3, 4, 5}
B = {2, 4}
C = {1, 5}
print(A.difference(B, C)){3}Sets are designed for fast membership tests using hashable elements. In CPython, typical membership and add operations are hash-table based and are commonly treated as average constant-time operations, although the Python language does not promise a fixed complexity for every implementation.
union().A = {1, 2, 3}
B = {3, 4, 5}
print(A | B)The | operator is the operator form of union.
A = {1, 2, 3}
B = {3, 4, 5}
A.symmetric_difference_update(B)
print(A)A = {1, 2}
B = {3}
C = {4, 5}
print(A.isdisjoint(B | C))Truepop() is called on an empty set?empty = set()
empty.pop()pop() raises KeyError when the set is empty.
values = [3, 1, 3, 2, 1]
unique_values = list(set(values))
print(unique_values)This removes duplicates but does not preserve the original order. If order matters, a dictionary-based approach such as list(dict.fromkeys(values)) is often more appropriate.
A = {1, 2}
B = {1, 2, 3}
print(A < B)Trueisdisjoint() and issubset()?isdisjoint() is true when there are no common elements. issubset() is true when every element of the first set is also present in the second.
A = {'Alex', 'John'}
B = {'John', 'Ravi'}
merged = A.union(B)
print(merged)original = {1, 2, 3}
copy_set = original.copy()
copy_set.add(4)
print(original)
print(copy_set)See copy().
A = {1, 2, 3, 4}
B = {2, 3, 5}
C = {0, 2, 3}
print(A.intersection(B, C)){2, 3}A = {1, 2, 3}
B = {3, 4}
C = {4, 5}
print(len(A | B | C))5+ or * directly with sets?No. Sets use set-operation operators such as |, &, - and ^. The sequence operators + and * are not supported for sets.
A = {1, 2, 3}
B = {3, 4, 5}
print(not A.isdisjoint(B))TrueA = {1, 2, 3, 4}
B = {2, 3}
print(A.issuperset(B))TrueSee issuperset().
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.