- 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)
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'}