Exercise 209

Write a program in Python language that returns the intersection and the union of the following three sets:

A = {11, 21, 5, 7, 43, 32, 13, 9}
B = {2, 19, 11, 33, 7, 25, 5, 4}
C = {45,27,11,5,7,22,14,1}

 

Solution

A = {11, 21, 5, 7, 43, 32, 13, 9}
B = {2, 19, 11, 33, 7, 25, 5, 4}
C = {45,27,11,5,7,22,14,1}

# Intersection of the three sets via the intersection() method
I = A.intersection(B.intersection(C))
print(I) # output: {11, 5, 7}

# union of the three sets via the union() method
U = A.union(B.union(C))
print(U) # output: {1, 2, 4, 5, 7, 9, 11, 13, 14, 19, 21, 22, 25, 27, 32, 33, 43, 45}

Younes Derfoufi
my-courses.net

Leave a Reply