Intersection () Python function

Python Methods and Functions

Examples :

 Input: Let set A = {2, 4, 5, 6} and set B = {4, 6, 7, 8} Output: {4,6} Explanation: Taking the common elements in both the sets, we get {4,6} as the intersection of both the sets. 

Syntax :

set1.intersection (set2, set3, set4….)
In parameters, any number of sets can be given

Return value:

The intersection () function returns a set, which has the intersection of all sets (set1, set2, set3 ...) with set1.
It returns a copy of set1 only if no parameter is passed.

Below is the implementation of the above approach in Python3:

# Python3 program for the intersection () function

 

set1 = { 2 , 4 , 5 6

set2 = { 4 , 6 , 7 , 8

set3 = { 4 , 6 , 8 }

 
# merging two sets

print ( "set1 intersection set2:" , set1.intersection (set2))

   # combining three sets

print ( "set1 intersection set2 intersection set3:" , set1.intersection (set2, set3))

Exit :

 set1 intersection set2: {4, 6} set1 intersection set2 intersection set3: {4, 6} 

Practical applications :
Most probabilistic problems require the concept of intersection of sets.





Tutorials