Introduction
Sets in Python are collections of unique elements that support various operations such as union, difference, and intersection. The intersection of two sets is a set containing elements that are common to both sets. This tutorial will guide you through creating a Python program that demonstrates how to perform the intersection of two sets.
Example:
- Set 1:
{'apple', 'banana', 'cherry'}
- Set 2:
{'cherry', 'date', 'elderberry'}
- Intersection:
{'cherry'}
(element common to both Set 1 and Set 2)
Problem Statement
Create a Python program that:
- Takes two sets as input.
- Performs the intersection of the two sets.
- Displays the resulting set after the intersection operation.
Solution Steps
- Create Two Sets: Initialize two sets with some elements.
- Perform the Intersection of the Two Sets: Use the
intersection()
method or the&
operator to find the intersection of the two sets. - Display the Intersection Set: Use the
print()
function to display the resulting set after the intersection operation.
Python Program
# Python Program to Perform Intersection of Two Sets
# Author: https://www.rameshfadatare.com/
# Step 1: Create two sets with elements
set1 = {'apple', 'banana', 'cherry'}
set2 = {'cherry', 'date', 'elderberry'}
# Step 2: Perform the intersection of the two sets using the intersection() method
intersection_set = set1.intersection(set2)
# Step 3: Display the resulting intersection set
print("The intersection of set1 and set2 is:", intersection_set)
# Alternatively, you can use the & operator to perform the intersection
intersection_set_alternative = set1 & set2
print("The intersection of set1 and set2 using & operator is:", intersection_set_alternative)
Explanation
Step 1: Create Two Sets with Elements
- Two sets,
set1
andset2
, are created with elements:{'apple', 'banana', 'cherry'}
and{'cherry', 'date', 'elderberry'}
, respectively. Sets are defined using curly braces{}
.
Step 2: Perform the Intersection of the Two Sets Using the intersection() Method
- The
intersection()
method is used to find the intersection ofset1
andset2
. The resulting set,intersection_set
, contains elements that are common to bothset1
andset2
.
Step 3: Display the Resulting Intersection Set
- The
print()
function is used to display the resulting set after the intersection operation.
Alternative: Use the & Operator
- The
&
operator is an alternative way to perform the intersection of two sets. The result will be the same as using theintersection()
method.
Output Example
Example Output:
The intersection of set1 and set2 is: {'cherry'}
The intersection of set1 and set2 using & operator is: {'cherry'}
(Note: The order of elements in the output may vary because sets are unordered collections.)
Additional Examples
Example 1: Intersection of Two Sets with Some Common Elements
# Sets with some common elements
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
# Perform intersection
intersection_set = set1.intersection(set2)
print("Intersection of set1 and set2:", intersection_set)
Output:
Intersection of set1 and set2: {3, 4}
Example 2: Intersection of Disjoint Sets (No Common Elements)
# Disjoint sets (no common elements)
set1 = {'red', 'blue'}
set2 = {'green', 'yellow'}
# Perform intersection
intersection_set = set1.intersection(set2)
print("Intersection of disjoint sets:", intersection_set)
Output:
Intersection of disjoint sets: set()
Example 3: Intersection of an Empty Set and a Non-Empty Set
# One empty set and one non-empty set
set1 = set()
set2 = {'alpha', 'beta', 'gamma'}
# Perform intersection
intersection_set = set1.intersection(set2)
print("Intersection of an empty set and a non-empty set:", intersection_set)
Output:
Intersection of an empty set and a non-empty set: set()
Example 4: Intersection of Sets with All Elements in Common
# Sets with all elements in common
set1 = {'apple', 'banana', 'cherry'}
set2 = {'apple', 'banana', 'cherry'}
# Perform intersection
intersection_set = set1.intersection(set2)
print("Intersection of sets with all elements in common:", intersection_set)
Output:
Intersection of sets with all elements in common: {'apple', 'banana', 'cherry'}
Conclusion
This Python program demonstrates how to perform the intersection of two sets using both the intersection()
method and the &
operator. The intersection operation is useful for identifying elements that are common to both sets. Understanding how to perform this operation is essential for tasks involving set theory and data management in Python.