Python Program to Illustrate Different Set Operations – 2024

Hey guys, in this blog we will see a Python Program to Illustrate Different Set Operations.

Code

# Python Program to Illustrate Different Set Operations

# define two sets
E = {0, 1, 2, 3, 4, 6, 8};
N = {1, 2, 3, 4, 5, 7};

# set union
print("Union of E and N is",E | N)

# set intersection
print("Intersection of E and N is",E & N)

# set difference
print("Difference of E and N is",E - N)

# set symmetric difference
print("Symmetric difference of E and N is",E ^ N)

Output

Union of E and N is {0, 1, 2, 3, 4, 5, 6, 7, 8}
Intersection of E and N is {1, 2, 3, 4}
Difference of E and N is {0, 8, 6}
Symmetric difference of E and N is {0, 5, 6, 7, 8}
  • Defining two sets E and N.
  • Union of E and N using ‘|’ operator.
  • Intersection of E and N using ‘&’ operator.
  • Difference of E and N using ‘-‘ operator.
  • Symmetric Difference of E and N using ‘^’ operator.

Check out our other python programming examples

Leave a Reply

Your email address will not be published. Required fields are marked *