Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python - Set Exercises – Complete Guide for Beginners

 Python sets are powerful data structures used to store unique values. They are widely used in real-world programming for removing duplicates, performing mathematical operations, and efficiently managing data collections.

Practicing set exercises helps you master:

  • Adding and removing items
  • Set operations (union, intersection, difference)
  • Looping through sets
  • Working with real-world data problems

In this tutorial, you will find beginner to advanced Python set exercises with solutions.


Exercise 1: Create a Set

Task

Create a set containing 5 fruits and print it.

Solution

fruits = {"apple", "banana", "orange", "mango", "grapes"}

print(fruits)

Exercise 2: Add an Item to a Set

Task

Add "pineapple" to the set.

Solution

fruits = {"apple", "banana", "orange"}

fruits.add("pineapple")

print(fruits)

Exercise 3: Add Multiple Items

Task

Add "kiwi" and "papaya" to the set.

Solution

fruits = {"apple", "banana"}

fruits.update(["kiwi", "papaya"])

print(fruits)

Exercise 4: Remove an Item

Task

Remove "banana" from the set.

Solution

fruits = {"apple", "banana", "orange"}

fruits.remove("banana")

print(fruits)

Exercise 5: Safe Removal

Task

Remove "mango" safely (no error if not found).

Solution

fruits = {"apple", "banana", "orange"}

fruits.discard("mango")

print(fruits)

Exercise 6: Loop Through a Set

Task

Print all items in a set.

Solution

colors = {"red", "green", "blue"}

for color in colors:
    print(color)

Exercise 7: Find Union of Two Sets

Task

Combine two sets of numbers.

Solution

a = {1, 2, 3}
b = {3, 4, 5}

result = a.union(b)

print(result)

Exercise 8: Intersection of Sets

Task

Find common elements between two sets.

Solution

a = {1, 2, 3, 4}
b = {3, 4, 5, 6}

result = a.intersection(b)

print(result)

Exercise 9: Difference Between Sets

Task

Find items in set A not in set B.

Solution

a = {1, 2, 3, 4}
b = {3, 4}

result = a.difference(b)

print(result)

Exercise 10: Symmetric Difference

Task

Find items that are not common in both sets.

Solution

a = {1, 2, 3}
b = {3, 4, 5}

result = a.symmetric_difference(b)

print(result)

Exercise 11: Check if Item Exists

Task

Check if "apple" is in the set.

Solution

fruits = {"apple", "banana", "orange"}

if "apple" in fruits:
    print("Apple is available")

Exercise 12: Count Items in a Set

Task

Find number of items in a set.

Solution

fruits = {"apple", "banana", "orange"}

print(len(fruits))

Exercise 13: Remove Duplicates from a List

Task

Convert list into a set to remove duplicates.

Solution

numbers = [1, 2, 2, 3, 4, 4, 5]

unique_numbers = set(numbers)

print(unique_numbers)

Exercise 14: Find Common Elements in Lists

Task

Find common values using sets.

Solution

list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]

result = set(list1) & set(list2)

print(result)

Exercise 15: Check Subset

Task

Check if one set is a subset of another.

Solution

a = {1, 2}
b = {1, 2, 3, 4}

print(a.issubset(b))

Exercise 16: Check Superset

Task

Check if a set contains another set.

Solution

a = {1, 2, 3, 4}
b = {2, 3}

print(a.issuperset(b))

Exercise 17: Check Disjoint Sets

Task

Check if two sets have no common items.

Solution

a = {1, 2}
b = {3, 4}

print(a.isdisjoint(b))

Exercise 18: Create a Set from User Input

Task

Take input and store in a set.

Solution

items = set()

for i in range(3):
    item = input("Enter item: ")
    items.add(item)

print(items)

Exercise 19: Square Numbers Using Set

Task

Create a set of squared numbers.

Solution

numbers = {1, 2, 3, 4, 5}

squares = {x * x for x in numbers}

print(squares)

Exercise 20: Filter Even Numbers

Task

Keep only even numbers.

Solution

numbers = {1, 2, 3, 4, 5, 6, 7, 8}

even = {x for x in numbers if x % 2 == 0}

print(even)

Real-World Practice Problem 1: Website Visitors

Task

Combine visitors from two days.

day1 = {"Alice", "Bob", "Charlie"}
day2 = {"Bob", "David"}

all_visitors = day1 | day2

print(all_visitors)

Real-World Practice Problem 2: Student Comparison

Task

Find students in both courses.

python = {"Alice", "Bob", "Charlie"}
data = {"Bob", "Emma"}

common = python & data

print(common)

Common Mistakes in Set Exercises

Mistake 1: Using index

set = {1, 2, 3}
print(set[0])  # Error

Mistake 2: Using + operator

set1 + set2  # Not allowed

Mistake 3: Expecting order

Sets are unordered collections.


Best Practices

  • Use sets to remove duplicates
  • Prefer discard() for safe removal
  • Use comprehensions for cleaner code
  • Use set operations for comparisons
  • Avoid indexing sets

Quick Summary

TaskMethod
Add itemadd()
Add multipleupdate()
Remove itemremove()/discard()
Combine setsunion()
Common itemsintersection()
Differencedifference()
Loop setfor loop
Remove duplicatesset()

Conclusion

Practicing Python set exercises helps you master one of the most efficient data structures in Python. Sets are widely used in real-world applications such as data analysis, filtering duplicates, user management, and mathematical computations.

By practicing these exercises regularly, you will gain strong confidence in handling sets and improve your overall Python programming skills.

Keep practicing and try creating your own set problems for better understanding.




Post a Comment

0 Comments