Introduction
Sets in Python are collections of unique elements, and they support various operations such as union, intersection, and difference. The difference between two sets is a set containing elements that are in the first set but not in the second set. This tutorial will guide you through creating a Python program that demonstrates how to perform the difference of two sets.
Example:
- Set 1:
{'apple', 'banana', 'cherry'} - Set 2:
{'cherry', 'date', 'elderberry'} - Difference:
{'apple', 'banana'}(elements in Set 1 that are not in Set 2)
Problem Statement
Create a Python program that:
- Takes two sets as input.
- Performs the difference operation on the two sets.
- Displays the resulting set after the difference operation.
Solution Steps
- Create Two Sets: Initialize two sets with some elements.
- Perform the Difference of the Two Sets: Use the
difference()method or the-operator to find the difference between the two sets. - Display the Difference Set: Use the
print()function to display the resulting set after the difference operation.
Python Program
# Python Program to Perform Difference 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 difference of the two sets using the difference() method
difference_set = set1.difference(set2)
# Step 3: Display the resulting difference set
print("The difference of set1 and set2 is:", difference_set)
# Alternatively, you can use the - operator to perform the difference
difference_set_alternative = set1 - set2
print("The difference of set1 and set2 using - operator is:", difference_set_alternative)
Explanation
Step 1: Create Two Sets with Elements
- Two sets,
set1andset2, are created with elements:{'apple', 'banana', 'cherry'}and{'cherry', 'date', 'elderberry'}, respectively. Sets are defined using curly braces{}.
Step 2: Perform the Difference of the Two Sets Using the difference() Method
- The
difference()method is used to find the difference betweenset1andset2. The resulting set,difference_set, contains elements that are inset1but not inset2.
Step 3: Display the Resulting Difference Set
- The
print()function is used to display the resulting set after the difference operation.
Alternative: Use the – Operator
- The
-operator is an alternative way to perform the difference of two sets. The result will be the same as using thedifference()method.
Output Example
Example Output:
The difference of set1 and set2 is: {'apple', 'banana'}
The difference of set1 and set2 using - operator is: {'apple', 'banana'}
(Note: The order of elements in the output may vary because sets are unordered collections.)
Additional Examples
Example 1: Difference of Two Sets with Some Common Elements
# Sets with some common elements
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
# Perform difference
difference_set = set1.difference(set2)
print("Difference of set1 and set2:", difference_set)
Output:
Difference of set1 and set2: {1, 2}
Example 2: Difference of Disjoint Sets (No Common Elements)
# Disjoint sets (no common elements)
set1 = {'red', 'blue'}
set2 = {'green', 'yellow'}
# Perform difference
difference_set = set1.difference(set2)
print("Difference of disjoint sets:", difference_set)
Output:
Difference of disjoint sets: {'red', 'blue'}
Example 3: Difference of an Empty Set and a Non-Empty Set
# One empty set and one non-empty set
set1 = set()
set2 = {'alpha', 'beta', 'gamma'}
# Perform difference
difference_set = set1.difference(set2)
print("Difference of an empty set and a non-empty set:", difference_set)
Output:
Difference of an empty set and a non-empty set: set()
Example 4: Difference in Reverse Order
# Perform difference in reverse order (set2 - set1)
reverse_difference_set = set2.difference(set1)
print("Difference of set2 and set1:", reverse_difference_set)
Output:
Difference of set2 and set1: {'date', 'elderberry'}
Conclusion
This Python program demonstrates how to perform the difference of two sets using both the difference() method and the - operator. The difference operation is useful for identifying elements that are present in one set but not in another. Understanding how to perform this operation is essential for tasks involving set theory and data management in Python.