intersection of two sets in python by R4R Team

- Intersection of two sets give the common item of both the sets.
- set intersection in python is done by two ways, first is by intersection() function and second is by '&' operator.

Syntax-

set1.intersection(set2)

Example
set1 is {1,2,3}
set2 is {2,3,5}
then intersection will be {2,3} // common items


program-

print("Enter item in set1")
set1=set(input().split())
print("Enter item in set2")
set2=set(input().split())

print("Set1 is",set1)
print("Set2 is",set2)
print("Intersection of both set is")
print(set1.intersection(set2))
print(set2.intersection(set1))
print(set1 & set2)


output-

Enter item in set1
1 2 4 5 one two three
Enter item in set2
3 4 6 three four
Set1 is {'three', 'one', '5', '1', '2', 'two', '4'}
Set2 is {'3', 'three', 'four', '6', '4'}
Intersection of both set is
{'three', '4'}
{'three', '4'}
{'three', '4'}


-In this program, we take a input of two sets i.e. set1 & set2 then we find the intersection of both the sets by intersection() function and '&' operator.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!