0% found this document useful (0 votes)
4 views2 pages

Set Operations in Python Explained

Uploaded by

MadhavRao
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Set Operations in Python Explained

Uploaded by

MadhavRao
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

A = {1, 2, 3, 4}

B = {3, 4, 5, 6}

# Using '|' operator

res1 = A | B

print("using '|':", res1)

# Using union() method

res2 = [Link](B)

print("using union():",res2)

A = {1, 2, 3, 4}

B = {3, 4, 5, 6}

# Using '&' operator

res1 = A & B

print("using '&':",res1)

# Using intersection() method

res2 = [Link](B)

print("using intersection():",res2)

A = {1, 2, 3, 4}

B = {3, 4, 5, 6}

# Using '-' operator

res1 = A - B

print("using '-':", res1)

# Using difference() method

res2 = [Link](B)

print("using difference():", res2)


4. A = {1, 2, 3, 4}

B = {3, 4, 5, 6}

# Using '^' operator

res1 = A ^ B

print("using '^':", res1)

# Using symmetric_difference() method

res2 = A.symmetric_difference(B)

print("using symmetric_difference():", res2)

5. # Program to perform different set operations like in mathematics

# define three sets

E = {0, 2, 4, 6, 8};

N = {1, 2, 3, 4, 5};

# 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)

You might also like