Intersection() in Python

Intersection() in Python

 

In most probability problems, the concept of the intersection of sets is needed.

The intersection of two given sets is the largest set which contains all the elements that are common to both initial sets.

Intersection() function in Python

Syntax :


set1.intersection(set2, set3,…)


Return Type :

Returns a set containing all the common values from the initial sets.


# intersection() method

#example 1
set1 = {‘P’‘Y’‘T’‘H’‘O’‘N’ }
set2 = {‘C’‘O’‘D’‘I’‘N’‘G’ }
print( set1.intersection(set2) )

#example 2
A = { 2468 , 101214161820 }
B = { 36912151821242730 }
C = { 481216202428323640 }
print( A.intersection(B,C) )


Output :

{ 'O', 'N' }

{ 12 }