Enter 5 numbers in two sets and merge then in a new set in Python
Sets - Question 3
In this question, we will see how to enter 5 numbers in two sets and find their union set in Python programming. To know more about sets click on the sets lesson.
Q3) Write a program in Python to enter 5 numbers in two sets and find their union set, that means merge them in a new set without adding duplicate numbers and display it on the screen.
Example:-
Set A = {1, 2, 3, 4, 5}
Set B = {1, 6, 2, 7, 3}
New set c having all the numbers from both sets
{1, 2, 3, 4, 5, 6, 7}
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()))
# Creating a new set having all the numbers from both sets
c=a.union(b)
print('New set c having all the numbers from both sets')
print(c)
Output
Enter 5 numbers in Set a
1
2
3
4
5
Enter 5 numbers in Set b
1
6
2
7
3
New set c having all the numbers from both sets
{1, 2, 3, 4, 5, 6, 7}