Sets — Practice Questions
50 questions. Try each one yourself before checking the answer.
Create colors = {"red", "green", "blue"} and print it along with its type().
Create numbers = {1, 2, 2, 3, 3, 3, 4} and print it. What happened to the duplicates?
Given numbers = [1, 2, 2, 3, 3, 3, 4] (a list), convert it to a set to remove duplicates, then print the result.
Start with fruits = {"apple", "banana"}. Add "cherry" using .add() and print the set.
Start with fruits = {"apple", "banana", "cherry"}. Remove "banana" using .remove(), then try to .discard() an item that doesn't exist ("mango") without it raising an error.
Print the number of items in letters = {"a", "b", "c", "a", "b"}.
Given allowed = {"admin", "editor", "viewer"}, print whether "editor" and "guest" are each in the set.
Print each item of numbers = {10, 20, 30} using a for loop.
Create an empty set correctly (not with {}, which creates a dict), add three items to it, and print it.
Create numbers = {5, 1, 3, 2, 4} and print it. Explain in a comment why the printed order might not match insertion order.
Given set1 = {1, 2, 3} and set2 = {3, 4, 5}, print their union using both the | operator and .union().
Using the same set1 and set2, print their intersection using both & and .intersection().
Using the same set1 and set2, print set1 - set2 and set2 - set1, and explain why they differ.
Using the same set1 and set2, print their symmetric difference using both ^ and .symmetric_difference().
Given small = {1, 2} and big = {1, 2, 3, 4}, print small.issubset(big) and big.issuperset(small).
Given original = {1, 2, 3}, create copy = original.copy(), add an item to original, and show copy is unaffected. Then .clear() the original.
Given set1 = {1, 2, 3} and set2 = {3, 4, 5}, use .update() to merge set2's items into set1 in place, and print the result.
Given numbers = {10, 20, 30}, call .pop() once and print both the removed item and the remaining set. Explain in a comment why you can't predict which item gets removed.
Given set1 = {1, 2, 3} and set2 = {4, 5, 6}, print whether they're disjoint (share no elements) using .isdisjoint().
Given word = "mississippi", print the set of its unique characters, and how many unique characters it has.
Given alice_friends = {"Bob", "Carol", "Dave"} and bob_friends = {"Carol", "Eve", "Frank"}, print their mutual friends.
Given monday_visitors = {"u1", "u2", "u3"} and tuesday_visitors = {"u2", "u3", "u4"}, print visitors who came on EITHER day, and visitors who came on BOTH days.
Given required_skills = {"python", "sql", "git"} and candidate_skills = {"python", "git", "excel"}, print which required skills the candidate is MISSING.
Given a list tags = ["python", "web", "python", "backend", "web", "api"], print the unique set of tags and how many unique tags there are.
Given team_a = {"Alice", "Bob"} and team_b = {"Bob", "Carol"}, print whether the two teams share ANY member using .isdisjoint(), phrased as a friendly boolean has_overlap.
Given two lists of email addresses, list1 and list2, print the emails that appear in BOTH lists using sets, then print them as a sorted list.
Given cart_items = {"bread", "milk"} and available_items = {"bread", "milk", "eggs", "butter"}, print whether the whole cart can be fulfilled using .issubset().
Ask the user to enter 5 words one at a time, storing them in a set, then print the number of DISTINCT words entered.
The following code crashes with a TypeError. Find and fix the bug.
tags = {"python", "web", ["backend"]}
print(tags)Given scores = {88, 92, 75, 92, 88, 100}, print the sorted unique scores as a comma-separated string.
Given two sentences, use sets built from .split() to find the words that appear in BOTH sentences (ignoring case).
Given text = "the quick brown fox jumps over the lazy dog", print how many words appear EXACTLY once, by checking each distinct word's .count() in the full split word list.
Create a frozenset from {1, 2, 3}, demonstrate that it supports normal set operations like &, but raises an error if you try .add() to it.
Given numbers = [4, 3, 2, 7, 8, 2, 3, 1] where every number appears twice except one, find that single unique number using symmetric_difference chained in a loop (not by counting).
Given set_a = {1, 2, 3}, set_b = {2, 3, 4}, and set_c = {3, 4, 5}, find the elements common to ALL THREE sets, chaining &.
Given permissions_needed = {"read", "write"} and a list of user permission sets, find which users (by index) can perform the required action, using .issubset() in a loop.
Given a list of 100,000 numbers, demonstrate why checking membership with in on a set is dramatically faster than on the equivalent list, explaining the underlying reason in a comment.
Given today_attendance = {"Alice", "Bob", "Carol"} and yesterday_attendance = {"Bob", "Carol", "David"}, print students who attended ONLY ONE of the two days (not both).
Given matrix_rows = [{1, 2, 3}, {2, 3, 4}, {3, 4, 5}] (each representing a row's distinct values), find the value common to ALL rows using a loop that intersects them one by one.
The following code is supposed to find items unique to set1 only, but the operator is backwards, producing the wrong answer. Find and fix the bug.
set1 = {"apple", "banana", "cherry"}
set2 = {"banana", "cherry", "date"}
unique_to_set1 = set2 - set1
print(unique_to_set1)Build a "Common Interests Finder". Ask two users to each enter 5 hobbies (comma-separated in one line, split manually), then print their shared and unique hobbies.
Build a "Plagiarism Overlap Checker". Given two texts, compute what percentage of the first text's unique words also appear in the second text.
Build a "Tag-Based Content Filter". Given a list of articles (each a (title, tag_set) tuple), print titles of articles that have AT LEAST ONE tag from a user's interested_tags set.
Build an "Attendance Overlap Tracker". Given attendance sets for 3 days, print students who attended EVERY day, and students who attended at least one day but not all.
Build a "Unique Visitor Counter". Ask the user to enter visitor IDs one at a time until they type "done", then print the total entries made and the number of DISTINCT visitors.
Explain why sets require their elements to be hashable, connecting it back to why tuples (but not lists) can go inside a set — demonstrate with both a working and a failing example.
Explain when to choose a set over a list or tuple, with one code example demonstrating each collection being the "right" choice for its own use case.
Demonstrate that converting a list to a set to remove duplicates does NOT preserve the original order, then show the fix (from the Lists module) that does preserve order.
A classic interview puzzle: "given two lists, find elements that are in exactly one of them, not both." Solve it using set operations and explain which operator does this in one step.
Build a "Set Mastery Report" — the most complete demonstration in this module. Given team_python = {"Alice", "Bob", "Carol", "Dave"} and team_java = {"Carol", "Dave", "Eve", "Frank"}, print: members in either team, members in both teams, members exclusive to each team, members in exactly one team, whether team_python is a subset of the combined team, and the total distinct member count.
Still stuck on something?
Book a free 1-on-1 session and we'll work through it together.
Book a Free Session