A set in Python is an unordered collection of unique items. This Python set exercise aims to help you learn and practice set operations.
Also Read:
This Python set exercise contains 16 coding questions, each with a provided solution.
- Practice and solve various set operations, manipulations, and set functions.
- All questions are tested on Python 3.
- Use Online Code Editor to solve exercise questions.
- Read the complete guide on Python Sets to solve this exercise.
As you complete each question, you will become more familiar with the Python set. Please let us know if you have any alternative solutions; they will help other developers.
Table of contents
- Exercise 1: Perform Basic Set Operations
- Exercise 2: Union of Sets
- Exercise 3: Intersection of Sets
- Exercise 4: Difference of Sets
- Exercise 5: Symmetric Difference
- Exercise 6: Add a list of Elements to a Set
- Exercise 7: Set Difference Update
- Exercise 8: Remove Items From Set Simultaneously
- Exercise 9: Check Subset
- Exercise 10: Check Superset
- Exercise 11: Set Intersection Check
- Exercise 12: Set Symmetric Difference Update
- Exercise 13: Set Intersection Update
- Exercise 14: Find Common Elements in Two Lists
- Exercise 15: Frozen Set
- Exercise 16: Count Unique Words
Exercise 1: Perform Basic Set Operations
- Create a Set: Create a set named fruits containing “apple”, “banana”, “mango”, and “orange”.
- Add Element: Add “grape” to the fruits set.
- Remove Element: Remove “banana” from the fruits set.
- Discard Element: Try to discard “mango” from the fruits set.
Expected Output:
1. After creating the set: {'apple', 'orange', 'mango', 'banana'}
2. After adding 'grape': {'orange', 'banana', 'grape', 'apple', 'mango'}
3. After removing 'banana': {'orange', 'grape', 'apple', 'mango'}
4. After discarding 'mango': {'orange', 'grape', 'apple'}
+ Hint
- You can create a set by enclosing elements in curly braces
{}
or by using theset()
constructor. - Adding an element: Use the
add()
method. - Removing an element: Use the
remove()
method to delete an element. - Discarding an element: Use the
discard()
method to delete an element. This method will not raise an error if the element is not found.
+ Show Solution
Exercise 2: Union of Sets
Find the union of set1
and set2
. Write a Python program to return a new set with unique items from both sets by removing duplicates.
Given:
set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}
Code language: Python (python)
Expected Output:
{70, 40, 10, 50, 20, 60, 30}
+ Hint
You can use either the union()
method or the |
operator for set union.
+ Show Solution
Note: The union()
method (or the |
operator) returns a new set containing all unique elements from both set1
and set2
.
Exercise 3: Intersection of Sets
Find the intersection of set1
and set2
. Write a code to return a new set containing only the elements that are common to both set1
and set2
Given:
set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}
Code language: Python (python)
Expected Output:
{40, 50, 30}
+ Hint
You can use either the intersection()
method or the &
operator for set intersection.
+ Show Solution
Exercise 4: Difference of Sets
Find the difference (set1 - set2
). Write a code to returns a new set containing elements that are present in set1
but not in set2
.
Given:
set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}
Code language: Python (python)
Expected Output:
{10, 20}
+ Hint
You can use either the difference()
method or the -
operator for set difference.
+ Show Solution
Exercise 5: Symmetric Difference
Find the symmetric difference of set1
and set2
. Write a code to returns a new set containing elements that are unique to either set1
or set2
, but not in both. It’s like finding the union and then removing the intersection.
Given:
set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}
Code language: Python (python)
Expected Output:
{20, 70, 10, 60}
+ Hint
You can use either the symmetric_difference()
method or the ^
operator for symmetric difference.
+ Show Solution
Exercise 6: Add a list of Elements to a Set
Given a Python list, write a program to add all of its elements into a given set.
Given:
sample_set = {"Yellow", "Orange", "Black"}
sample_list = ["Blue", "Green", "Red"]
Code language: Python (python)
Expected output:
Note: Set is unordered.
{'Green', 'Yellow', 'Black', 'Orange', 'Red', 'Blue'}
Show Hint
Use the update()
method of a set.
Show Solution
Exercise 7: Set Difference Update
Given two Python sets, write a program to update the first set with only the items that are unique to it (i.e., not present in the second set).
Given:
set1 = {10, 20, 30}
set2 = {20, 40, 50}
Code language: Python (python)
Expected output:
set1 {10, 30}
Show Hint
Use the difference_update()
method of a set.
Show Solution
Exercise 8: Remove Items From Set Simultaneously
Write a Python program to remove items 10, 20, 30 from the following set at once.
Given:
set1 = {10, 20, 30, 40, 50}
Code language: Python (python)
Expected output:
{40, 50}
Show Hint
Use the difference_update()
method of a set.
Show Solution
Exercise 9: Check Subset
Check if set1
is a subset of set2
. Write a code to return True
if every element in the subset_set
is also present in the main_set
.
Given:
subset_set = {10, 20}
main_set = {10, 20, 30, 40}
Code language: Python (python)
Expected Output:
True
+ Hint
Use an issubset()
method of set.
+ Show Solution
Exercise 10: Check Superset
Check if main_set = {10, 20, 30, 40}
is a superset of subset_set = {10, 20}
.
Given:
set1 = {10, 20}
set2 = {10, 20, 30, 40}
Code language: Python (python)
Expected Output:
True
+ Hint
Sets have an issuperset()
method.
+ Show Solution
Exercise 11: Set Intersection Check
Write a code to check if two sets have any elements in common. If yes, display the common elements
Given:
set1 = {10, 20, 30, 40, 50}
set2 = {60, 70, 80, 90, 10}
Code language: Python (python)
Expected output:
Two sets have items in common {10}
Show Hint
- Use the
isdisjoint()
method check if sets has a common elements - If above condition is true then use the
intersection()
method to display common elements
Show Solution
Exercise 12: Set Symmetric Difference Update
Write a program to update set1
by adding items from set2
that are not common to both sets.
Given:
set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}
Code language: Python (python)
Expected output:
{70, 10, 20, 60}
Show Hint
Use the symmetric_difference_update()
method of a set.
Show Solution
Exercise 13: Set Intersection Update
Write a code to remove items from set1
that are not present in set2
.
Given:
set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}
Code language: Python (python)
Expected output:
{40, 50, 30}
Show Hint
Use the intersection_update()
method of a set.
Show Solution
Exercise 14: Find Common Elements in Two Lists
list1 = [10, 20, 30, 40]
and list2 = [30, 40, 50, 60]
, find the common elements using sets.
Given:
list1 = [10, 20, 30, 40]
list2 = [30, 40, 50, 60]
Code language: Python (python)
Expected Output:
{40, 30}
+ Hint
Convert both lists to sets, then use the set intersection operation.
+ Show Solution
Exercise 15: Frozen Set
Create a frozen set from a list.
Given:
my_list = [10, 20, 30]
Code language: Python (python)
Expected Output:
({10, 20, 30})
+ Hint
Use the frozenset()
constructor. Remember that frozen sets are immutable, meaning their contents cannot be changed after creation.
+ Show Solution
Exercise 16: Count Unique Words
Write a code to count the number of unique words in the given a sentence.
Given:
sentence = "dog is a simple animal dogs is selfless animal"
Code language: Python (python)
Expected Output:
Number of unique words: 7
+ Hint
- First, split the sentence into words.
- Then, convert the list of words into a set to get unique words.
- Finally, find the length of the set. Remember to handle case sensitivity (e.g., “Dog” vs. “dog”) if necessary by converting words to lowercase.