In this Python set program, we will find the intersection of two sets using Python with the help of the below-given steps.
What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.
Intersection of two sets: The intersection is common elements between the two sets.
Intersection of two sets:
Steps to solve the program
1. Create two sets using {}.
2. Add some elements in the sets.
3. Get the intersection of sets using the intersection() function.
4. Print the output.
a = {1,2,4,5}
b = {7,8,9,1}
print("Original set1: ",a)
print("Original set2: ",b)
print("Intersection of a and b: ",a.intersection(b))
Output :
Original set1: {1, 2, 4, 5}
Original set2: {8, 9, 1, 7}
Intersection of a and b: {1}
Python program to find the difference of two sets.
Python program to find the symmetric difference of two sets.
Python program to show if one set is a subset of another set.
Python program to check if two sets are disjoint.
Python program to convert a list to a set.
Python program to convert a set to a list.