In this Python program, we will learn how to perform different set operations Such as union, intersection, difference, and symmetric difference.
Here is the source code of a program to perform different set operations Such as union, intersection, difference, and symmetric difference.
# Program to perform different set operations like we do in mathematics
# define two sets
E = {0, 2, 4, 6, 8};
N = {1, 2, 3, 4, 5};
# set union
print("\nUnion of E and N is",E | N)
# set intersection
print("\nIntersection of E and N is",E & N)
# set difference
print("\nDifference between E and N is",E - N)
# set symmetric difference
print("\nSymmetric difference of E and N is",E ^ N)
Union of E and N is {0, 1, 2, 3, 4, 5, 6, 8}
Intersection of E and N is {2, 4}
Difference between E and N is {0, 8, 6}
Symmetric difference of E and N is {0, 1, 3, 5, 6, 8}
Comments