Python Program to Illustrate Different Set Operations

To understand this example, you should have the knowledge of the following Python programming topics:


Python offers a datatype called set whose elements must be unique. It can be used to perform different set operations like union, intersection, difference and symmetric difference.

Source Code

Output

Union of E and N is {0, 1, 2, 3, 4, 5, 6, 8}
Intersection of E and N is {2, 4}
Difference of E and N is {8, 0, 6}
Symmetric difference of E and N is {0, 1, 3, 5, 6, 8}

In this program, we take two different sets and perform different set operations on them. This can equivalently done by using set methods.