M.Sc AI Practical Assignment Solution 3
Question:
Write a Python program to Illustrate Different Set Operations.
Code:
# Set creation
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
# Union
union_set = set1.union(set2)
print("Union of set1 and set2:", union_set)
# Intersection
intersection_set = set1.intersection(set2)
print("Intersection of set1 and set2:", intersection_set)
# Difference
difference_set = set1.difference(set2)
print("Difference of set1 and set2:", difference_set)
# Symmetric Difference
symmetric_difference_set = set1.symmetric_difference(set2)
print("Symmetric difference jof set1 and set2:", symmetric_difference_set)
# Subset check
subset_check = set1.issubset(set2)
print("Subset check (set1 is a subset of set2):", subset_check)
# Superset check
superset_check = set1.issuperset(set2)
print("Superset check (set1 is a superset of set2):", superset_check)
# Disjoint check
disjoint_check = set1.isdisjoint(set2)
print("Disjoint check (set1 and set2 are disjoint):", disjoint_check)
Output:
Union of set1 and set2: {1, 2, 3, 4, 5, 6, 7, 8} Intersection of set1 and set2: {4, 5} Difference of set1 and set2: {1, 2, 3} Symmetric difference of set1 and set2: {1, 2, 3, 6, 7, 8} Subset check (set1 is a subset of set2): False Superset check (set1 is a superset of set2): False Disjoint check (set1 and set2 are disjoint): False