intersection of n sets in python by R4R Team

- Intersection of n-sets give the common item from the n-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}
set3 is {2,6,7}
intersection will be {2}because only '2' is common in all the sets.


program-

set1={1,2,3,4,5,6}
set2={9,8,7,6,5,4,3}
set3={4,5,6,"one","two"}
set4={1,2,3,4,5,6,7,8,9,0}
print("Set1 is",set1)
print("Set2 is",set2)
print("Set3 is",set3)
print("Set4 is",set4)
print("Intersection of all set is")
print(set1 & set2 & set3 & set4)


output-

Set1 is {1, 2, 3, 4, 5, 6}
Set2 is {3, 4, 5, 6, 7, 8, 9}
Set3 is {4, 5, 6, 'one', 'two'}
Set4 is {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Intersection of all set is
{4, 5, 6}


-In this program, we have 4 sets and we find intersection of all by set1 & set2 & set3 & set4 and print that.




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!