Python Sets Practice Questions with Solutions

Sets are unordered collections of unique elements in Python. They automatically remove duplicate values and are useful for membership testing, mathematical set operations, and storing unique data. In this Python Sets Practice Questions with Solutions set, you’ll learn how to create sets, add and remove elements, perform set operations, and solve beginner-friendly Python programs.


1. Python Program to Create and Print a Set

Difficulty: Foundation

Problem Statement

Write a Python program to create a set of five colors and print it.

Python Solution

colors = {"Red", "Blue", "Green", "Yellow", "Black"}

print(colors)

Sample Output

{'Blue', 'Green', 'Yellow', 'Black', 'Red'}

Explanation

A set stores unique values inside curly braces {}. The order of elements may vary.

Concepts Covered

  • Creating Sets
  • Set Syntax

2. Python Program to Add an Element to a Set

Difficulty: Foundation

Problem Statement

Write a Python program to add "Python" to a set of programming languages.

Python Solution

languages = {"Java", "C++", "JavaScript"}

languages.add("Python")

print(languages)

Sample Output

{'Java', 'Python', 'C++', 'JavaScript'}

Explanation

The add() method inserts a new element into a set.

Concepts Covered

  • add()

3. Python Program to Remove an Element from a Set

Difficulty: Foundation

Problem Statement

Write a Python program to remove "Orange" from a set.

Python Solution

fruits = {"Apple", "Orange", "Banana", "Mango"}

fruits.remove("Orange")

print(fruits)

Sample Output

{'Apple', 'Banana', 'Mango'}

Explanation

The remove() method deletes the specified element from the set.

Concepts Covered

  • remove()

4. Python Program to Check Whether an Element Exists in a Set

Difficulty: Foundation

Problem Statement

Write a Python program to check whether "Python" exists in a set.

Python Solution

courses = {"Python", "Java", "SQL"}

if "Python" in courses:
    print("Course Found")
else:
    print("Course Not Found")

Sample Output

Course Found

Explanation

The in operator checks whether an element exists in a set.

Concepts Covered

  • Membership Operator

5. Python Program to Find the Union of Two Sets

Difficulty: Practice

Problem Statement

Write a Python program to find the union of two sets.

Python Solution

set1 = {1, 2, 3}
set2 = {3, 4, 5}

print(set1.union(set2))

Sample Output

{1, 2, 3, 4, 5}

Explanation

The union() method returns all unique elements from both sets.

Concepts Covered

  • union()

6. Python Program to Find the Intersection of Two Sets

Difficulty: Practice

Problem Statement

Write a Python program to find the common elements between two sets.

Python Solution

set1 = {10, 20, 30, 40}
set2 = {30, 40, 50, 60}

print(set1.intersection(set2))

Sample Output

{40, 30}

Explanation

The intersection() method returns only the common elements.

Concepts Covered

  • intersection()

7. Python Program to Find the Difference Between Two Sets

Difficulty: Practice

Problem Statement

Write a Python program to find the elements present in the first set but not in the second.

Python Solution

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

print(set1.difference(set2))

Sample Output

{1, 2}

Explanation

The difference() method returns elements that exist only in the first set.

Concepts Covered

  • difference()

8. Python Program to Remove Duplicate Values from a List

Difficulty: Challenge

Problem Statement

Write a Python program to remove duplicate values from a list using a set.

Python Solution

numbers = [10, 20, 30, 20, 40, 10]

unique_numbers = list(set(numbers))

print(unique_numbers)

Sample Output

[40, 10, 20, 30]

Explanation

Converting a list into a set automatically removes duplicate values.

Concepts Covered

  • set()
  • Removing Duplicates

9. Python Program to Find Symmetric Difference Between Two Sets

Difficulty: Challenge

Problem Statement

Write a Python program to find the elements that exist in either set but not in both.

Python Solution

set1 = {1, 2, 3}
set2 = {3, 4, 5}

print(set1.symmetric_difference(set2))

Sample Output

{1, 2, 4, 5}

Explanation

The symmetric_difference() method returns elements that are unique to each set.

Concepts Covered

  • symmetric_difference()

10. Python Program to Clear All Elements from a Set

Difficulty: Challenge

Problem Statement

Write a Python program to remove all elements from a set.

Python Solution

languages = {"Python", "Java", "C++"}

languages.clear()

print(languages)

Sample Output

set()

Explanation

The clear() method removes every element from the set, leaving it empty.

Concepts Covered

  • clear()

Chapter Summary

After completing this chapter, you have learned:

  • Creating sets
  • add()
  • remove()
  • Membership testing
  • union()
  • intersection()
  • difference()
  • symmetric_difference()
  • clear()
  • Removing duplicate values using sets

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top