Python Set Beginner Project: union(), intersection(), difference(), symmetric_difference()

Python-Set-Beginner-Project
Credit: Canva

Overview

Python Set is one of the built-in Data Types in Python. Some of the most common and beginner-level methods are union(), intersection(), difference(), etc. This focuses on the key operations and highlights the practical approach, making it appealing for beginners for Python set projects.

What’s Next!

Let’s jump into the Python Set Beginner Project. In which we will explore a couple of Python projects with Python Set operations like union(), intersection(), difference(), and symmetric_difference(). This project provides clear explanations to help you understand and apply these set methods.

Table of Contents

Python Set Methods:

  1. union() – This method returns a set of unique elements from both sets.

  2. intersection() – This method returns a set of common elements from both sets.

  3. difference() – This method returns a set of unique elements that are present only in the original set but not in another set.

  4. symmetric_difference() – This method returns a set of elements that are present in either of the sets but not in both sets.

  5. issubset() – This method checks if the other set (2nd set) contains all elements of the original set (1st set). It returns Python Boolean value, if it contains all elements, returns True, otherwise False.

  6. issuperset() – This method checks if the original set (1st set) contains all elements of the other set (2nd set). It returns Python Boolean value, if it contains all elements, returns True, otherwise False.

Project 1: Set Membership Checker

Create a program that takes a set of elements and checks if a specific element is a member of the set using the in operator. The program should also demonstrate adding and removing elements using the add() and remove() methods.

				
					city = {'Delhi', 'Tokyo', 'Paris', 'London'}
if 'London' in city:
  print('London is present in the set.')
else:
  print('London is not present in the set.')

city.add('Mumbai')
print('After adding Mumbai:',city)

city.remove('Delhi')
print('After removing Delhi:',city)
				
			

Explanation: The program starts with a set. Next conditional statement is used with the keyword ‘in’ to check the availability of the element in the set. Later to add an element in the set, use the add() method and to remove, use the remove() method.

Now, let’s see the same question with Python def

				
					def set_membership_checker(set):
  if 'Delhi' in set:
    return 'Delhi is present in the set.'
  else:
    return 'Delhi is not present in the set.'

city = {'Delhi', 'Tokyo', 'Paris', 'London'}

city.add('Mumbai')
city.remove('Delhi')

print(set_membership_checker(city))
print('After adding Mumbai:',city)
print('After removing Delhi:',city)
				
			

Explanation: First create a function set_membership_checker and pass the parameter set. Then define the conditional statement with the ‘in’ keyword to check the membership of the element in the set. Outside the function, a set is defined and passed to the parameter value. Also, to add an element in the set, use the add() method, and to remove it, use the remove() method. Finally using the print() function, the output is displayed.

Project 2: Set Union and Intersection

Write a program that takes two sets of elements and finds the union and intersection of these sets using the union() and intersection() methods. The program should display the results of these operations.

				
					cities1 = {'Delhi', 'Tokyo', 'Paris', 'London'}
cities2 = {'Mumbai', 'Delhi', 'Paris', 'New York'}

union_cities = cities1.union(cities2)
intersection_cities = cities1.intersection(cities2)

print('Union of both sets:',union_cities)
print('Intersection of both sets:',intersection_cities)
				
			

Explanation: The program starts with two sets, at first applied union() method, returns the unique elements from both sets and the next intersection() method is applied.to find the common elements from both sets.

Now, let’s see the same question with Python def

				
					def set_union_intersection(cities1, cities2):
  union_cities = cities1.union(cities2)
  intersection_cities = cities1.intersection(cities2)
  return union_cities, intersection_cities

cities1 = {'Delhi', 'Tokyo', 'Paris', 'London'}
cities2 = {'Mumbai', 'Delhi', 'Paris', 'New York'}

union_cities, intersection_cities = set_union_intersection(cities1, cities2)

print('Union of both sets:',union_cities)
print('Intersection of both sets:',intersection_cities)
				
			

Explanation: The program starts with the function set_union_intersection() and passes the parameters cities1 and cities2. Next, the union() and intersection() methods are applied to the set to get the unique and common elements respectively.

Finally, outside the function, two sets were defined and passed as values to the parameters. Finally, the output is displayed using the print() function.

Project 3: Set Difference Analyzer

Develop a program that takes two sets and finds the difference between them using the difference() method. The program should display the elements that are in the first set but not in the second.

				
					cities1 = {'Delhi', 'Tokyo', 'Paris', 'London'}
cities2 = {'Mumbai', 'Delhi', 'Paris', 'New York'}

difference_set = cities1.difference(cities2)

print('Difference of cities1 with cities2:', difference_set)
				
			

Explanation: The program starts with two sets. Next the difference() method is applied to return the unique elements present in only cities1 but not in cities2.

Now, let’s see the same question with Python def

				
					def difference_set(cities1, cities2):
  difference_set = cities1.difference(cities2)
  return difference_set

cities1 = {'Delhi', 'Tokyo', 'Paris', 'London'}
cities2 = {'Mumbai', 'Delhi', 'Paris', 'New York'}

difference_set = difference_set(cities1, cities2)

print('Difference of cities1 with cities2:', difference_set)
				
			

Explanation: The program starts with the function difference_set() and passes the parameters cities1 and cities2. Next the difference() method is applied to return the unique elements present in only cities1 but not in cities2.

Finally, outside the function, two sets were defined and passed as values to the parameters. Finally, the output is displayed using the print() function.

Project 4: Set Difference Analyzer

Create a program that takes two sets and finds the symmetric difference using the symmetric_difference() method. The program should display the elements that are in either of the sets but not in both.

				
					cities1 = {'Delhi', 'Tokyo', 'Paris', 'London'}
cities2 = {'Mumbai', 'Delhi', 'Paris', 'New York'}

symmetric_difference_set = cities1.symmetric_difference(cities2)

print('Symmetric difference of cities1 with cities2:', symmetric_difference_set)
				
			

Explanation: The program starts with two sets. Next, the symmetric_difference() method is applied to the set to get the elements that are present in either of the sets but not in both sets. It means that only those elements will be considered which are not common in both sets.

Finally, the output is displayed using the print() function.

Now, let’s see the same question with Python def

				
					def symmetric_difference_finder(cities1, cities2):
  symmetric_difference_set = cities1.symmetric_difference(cities2)
  return symmetric_difference_set

cities1 = {'Delhi', 'Tokyo', 'Paris', 'London'}
cities2 = {'Mumbai', 'Delhi', 'Paris', 'New York'}

symmetric_difference_set = symmetric_difference_finder(cities1, cities2)

print('Symmetric difference of cities1 with cities2:', symmetric_difference_set)
				
			

Explanation: The program starts with the function symmetric_difference_finder() and passes the parameters cities1 and cities2. Next, the symmetric_difference() method is applied to the set to get only those elements that are not common in both sets.

Finally, outside the function, two sets were defined and passed as values to the parameters. Finally, the output is displayed using the print() function.

Project 5: Subset and Superset Checker

Write a program that takes two sets and checks if one set is a subset or superset of the other using the issubset() and issuperset() methods. The program should display the results of these checks.

				
					cities1 = {'Delhi', 'Tokyo', 'Paris', 'London'}
cities2 = {'Mumbai', 'Delhi', 'Paris', 'New York', 'Tokyo', 'London'}

is_superset = cities1.issuperset(cities2)
print('Is cities1 a superset of cities2:', is_superset)

is_subset = cities1.issubset(cities2)
print('Is cities1 a subset of cities2:', is_subset)
				
			

Explanation: The program starts with two sets. Next, the issuperset() method is applied to check if the cities1 set contains all elements of the cities2 set. Another method issubset() is applied to check if cities2 contains all elements of the cities1 set. Both methods return the Python Boolean values i.e., True and False.

Now, let’s see the same question with Python def

				
					def issuperset_issubset(cities1, cities2):
  is_superset = cities1.issuperset(cities2)
  is_subset = cities1.issubset(cities2)
  return is_superset, is_subset

cities1 = {'Delhi', 'Tokyo', 'Paris', 'London'}
cities2 = {'Mumbai', 'Delhi', 'Paris', 'New York', 'Tokyo', 'London'}

is_superset, is_subset = issuperset_issubset(cities1, cities2)

print('Is cities1 a superset of cities2:', is_superset)
print('Is cities1 a subset of cities2:', is_subset)
				
			

Explanation: The program starts with the function issuperset_issubset() and passes the parameters cities1 and cities2.

Next, the issuperset() method is applied to check if the cities1 set contains all elements of the cities2 set. Another method issubset() is applied to check if cities2 contains all elements of the cities1 set. Both methods return the Python Boolean values i.e., True and False.

Finally, outside the function, two sets were defined and passed as values to the parameters. Finally, the output is displayed using the print() function.

Recent Articles