Python Set Practice Questions with Answers

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.

Before you start: a Python set is an unordered, mutable collection of unique, hashable elements. Use set() for an empty set; {} creates an empty dictionary.

Try each problem before opening or reading the solution.

1. Create an empty set and check its type.

my_set = set()
print(type(my_set))
<class 'set'>

2. Create a set containing five integers.

numbers = {10, 20, 30, 40, 50}
print(numbers)

The printed order should not be treated as fixed because sets are unordered.

3. Create a set from values produced by range().

numbers = set(range(5, 50, 10))
print(numbers)

See range() for more range examples.

4. Unpack a set while printing its elements.

names = {'Alex', 'Ronald', 'John'}
print(*names)

The display order may differ between runs or environments.

5. Create a set from a list.

values = [10, 20, 20, 30]
unique_values = set(values)
print(unique_values)

Duplicate values are removed when the set is created.

6. What happens when duplicate values are supplied to a set?

numbers = {1, 1, 2, 2, 3}
print(numbers)
print(len(numbers))
{1, 2, 3}
3

7. Create a set containing one element and check its type.

single = {'Alex'}
print(type(single))
<class 'set'>

8. Use 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
5

9. What does clear() 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.

10. Does a set have a 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)

11. Can you read the first, last or third element of a set by position?

No. Sets do not record positional indexes, so indexing and slicing are invalid. Use a list or tuple when position matters.

my_set = {'a', 'b', 'c'}
print(my_set[0])

This raises TypeError.

12. How can you remove several elements from a set at once?

items = {'a', 'b', 'c', 'd'}
items.difference_update({'b', 'd'})
print(items)

See difference_update().

13. Can a frozenset be stored inside a set?

groups = {frozenset({1, 2}), frozenset({3, 4})}
print(groups)

Yes. A frozenset is immutable and hashable, so it can be a set element.

14. Find the elements in set A that are absent from both B and C.

A = {1, 2, 3, 4, 5}
B = {2, 4}
C = {1, 5}
print(A.difference(B, C))
{3}

15. Why are sets commonly used for membership testing?

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.

16. Get the union without calling union().

A = {1, 2, 3}
B = {3, 4, 5}
print(A | B)

The | operator is the operator form of union.

17. Update a set with the symmetric difference of itself and another set.

A = {1, 2, 3}
B = {3, 4, 5}
A.symmetric_difference_update(B)
print(A)

See symmetric_difference_update().

18. How can you check whether A has no elements in common with B or C?

A = {1, 2}
B = {3}
C = {4, 5}
print(A.isdisjoint(B | C))
True

19. What happens when pop() is called on an empty set?

empty = set()
empty.pop()

pop() raises KeyError when the set is empty.

20. Convert a list with duplicates to a set and then back to a list.

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.

21. Check whether A is a strict subset of B.

A = {1, 2}
B = {1, 2, 3}
print(A < B)
True

22. What is the difference between isdisjoint() 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.

23. Merge two sets while removing duplicate values.

A = {'Alex', 'John'}
B = {'John', 'Ravi'}
merged = A.union(B)
print(merged)

24. Copy a set so changes to the copy do not change the original set.

original = {1, 2, 3}
copy_set = original.copy()
copy_set.add(4)
print(original)
print(copy_set)

See copy().

25. Remove an element without raising an error when the element is missing.

items = {'a', 'b'}
items.discard('x')
print(items)

discard() does nothing if the element is absent; remove() would raise KeyError.

26. Find the intersection of three sets.

A = {1, 2, 3, 4}
B = {2, 3, 5}
C = {0, 2, 3}
print(A.intersection(B, C))
{2, 3}

27. Count the number of unique elements across three sets.

A = {1, 2, 3}
B = {3, 4}
C = {4, 5}
print(len(A | B | C))
5

28. Can you use + or * directly with sets?

No. Sets use set-operation operators such as |, &, - and ^. The sequence operators + and * are not supported for sets.

29. Check whether two sets contain at least one common element.

A = {1, 2, 3}
B = {3, 4, 5}
print(not A.isdisjoint(B))
True

30. How do you check whether one set is a superset of another?

A = {1, 2, 3, 4}
B = {2, 3}
print(A.issuperset(B))
True

See issuperset().

31. What is the difference between a mutable set and an immutable frozenset?

A regular set is mutable: elements can be added or removed. A frozenset is immutable and hashable, so it can be used as a dictionary key or as an element of another set.




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer