Enter 5 numbers in two sets and find the intersection update of the first set in Python
Sets - Question 6
In this question, we will see how to enter 5 numbers in two sets and find the intersection update of the first set in Python programming. To know more about sets click on the sets lesson.
Q6) Write a program in Python to enter 5 numbers in two sets and find the intersection update of the first set, that means update Set A with numbers which are common in both sets and display the Set A on the screen.
Example:- Set A = {1, 2, 3, 4, 5} Set B = {1, 6, 2, 7, 3} Set a with common numbers from both sets {1, 2, 3}
Program
a=set()
b=set()
print('Enter 5 numbers in Set a')
for i in range(5):
a.add(int(input()))
print('Enter 5 numbers in Set b')
for i in range(5):
b.add(int(input()))
# Update Set a with common numbers from both sets
a.intersection_update(b)
print('Set a with common numbers from both sets')
print(a)
Output
Enter 5 numbers in Set a 1 2 3 4 5 Enter 5 numbers in Set b 1 6 2 7 3 Set a with common numbers from both sets {1, 2, 3}