PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
  • Quizzes
  • Code Editor
Home » Python Exercises » Python Set Exercise with Solutions

Python Set Exercise with Solutions

Updated on: May 31, 2025 | 30 Comments

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:

  • Python Sets
  • Python Set Quiz

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

  1. Create a Set: Create a set named fruits containing “apple”, “banana”, “mango”, and “orange”.
  2. Add Element: Add “grape” to the fruits set.
  3. Remove Element: Remove “banana” from the fruits set.
  4. 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 the set() 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
# 1. Create a Set
fruits = {"apple", "banana", "mango", "orange"}
print("1. After creating the set:", fruits)

# 2. Add Element
fruits.add("grape")
print("2. After adding 'grape':", fruits)

# 3. Remove Element
fruits.remove("banana")
print("3. After removing 'banana':", fruits)

# 4. Discard Element
fruits.discard("mango")
print("4. After discarding 'mango':", fruits)Code language: Python (python)

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
set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}
union_set = set1.union(set2)
print("Union of set1 and set2:", union_set)Code language: Python (python)

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
set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}
intersection_set = set1.intersection(set2)
print("Intersection of set1 and set2:", intersection_set)Code language: Python (python)

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
set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}
difference_set = set1.difference(set2)
print("3. Difference (set1 - set2):", difference_set)Code language: Python (python)

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
set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}
symmetric_difference_set = set1.symmetric_difference(set2)
print("Symmetric Difference:", symmetric_difference_set)Code language: Python (python)

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
sample_set = {"Yellow", "Orange", "Black"}
sample_list = ["Blue", "Green", "Red"]

sample_set.update(sample_list)
print(sample_set)Code language: Python (python)

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
set1 = {10, 20, 30}
set2 = {20, 40, 50}

set1.difference_update(set2)
print(set1)Code language: Python (python)

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
set1 = {10, 20, 30, 40, 50}
set1.difference_update({10, 20, 30})
print(set1)Code language: Python (python)

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
# 1. Check Subset
subset_set = {10, 20}
main_set = {10, 20, 30, 40}

is_subset = subset_set.issubset(main_set)
print(f"Is {subset_set} a subset of {main_set}? {is_subset}")Code language: Python (python)

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
subset_set = {10, 20}
main_set = {10, 20, 30, 40}
is_superset = main_set.issuperset(subset_set)
print(f"Is {main_set} a superset of {subset_set}? {is_superset}")Code language: Python (python)

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
set1 = {10, 20, 30, 40, 50}
set2 = {60, 70, 80, 90, 10}

if set1.isdisjoint(set2):
  print("Two sets have no items in common")
else:
  print("Two sets have items in common")
  print(set1.intersection(set2))Code language: Python (python)

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
set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}

set1.symmetric_difference_update(set2)
print(set1)Code language: Python (python)

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
set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}

set1.intersection_update(set2)
print(set1)Code language: Python (python)

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
list1 = [10, 20, 30, 40]
list2 = [30, 40, 50, 60]

set_list1 = set(list1)
set_list2 = set(list2)

common_elements = set_list1.intersection(set_list2)
print(f"List 1: {list1}")
print(f"List 2: {list2}")
print(f"Common elements using sets: {common_elements}")Code language: Python (python)

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
my_list = [10, 20, 30]
frozen_set = frozenset(my_list)
print(f"Original list: {my_list}")
print(f"Created frozen set: {frozen_set}")Code language: Python (python)

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.
+ Show Solution
sentence = "dog is a simple animal dogs is selfless animal"

# Convert the sentence to lowercase and split it into words
words = sentence.lower().split()

# Convert the list of words to a set to get unique words
unique_words = set(words)

# Count the number of unique words
unique_word_count = len(unique_words)

print(f"Original sentence: '{sentence}'")
print(f"Number of unique words: {unique_word_count}")Code language: Python (python)

Filed Under: Python, Python Basics, Python Exercises

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

Image

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Python Python Basics Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ
Exercises
Quizzes

Loading comments... Please wait.

In: Python Python Basics Python Exercises
TweetF  sharein  shareP  Pin

  Python Exercises

  • All Python Exercises
  • Basic Exercise for Beginners
  • Input and Output Exercise
  • Loop Exercise
  • Functions Exercise
  • String Exercise
  • Data Structure Exercise
  • List Exercise
  • Dictionary Exercise
  • Set Exercise
  • Tuple Exercise
  • Date and Time Exercise
  • OOP Exercise
  • File Handling Exercise
  • Python JSON Exercise
  • Random Data Generation Exercise
  • NumPy Exercise
  • Pandas Exercise
  • Matplotlib Exercise
  • Python Database Exercise

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

All Python Topics

Python Basics Python Exercises Python Quizzes Python Interview Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2025 pynative.com

Advertisement