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 in python

Python intersection()

In Python, the intersection() method is used to find the intersection of two or more sets. The intersection of sets contains the elements that are common to all the sets being intersected.

Intersection() function in Python

Syntax :


set1.intersection(set2, set3,…)


Return Type :

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

Run
# 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 = { 2, 4, 6, 8 , 10, 12, 14, 16, 18, 20 }
B = { 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 }
C = { 4, 8, 12, 16, 20, 24, 28, 32, 36, 40 }
print( A.intersection(B,C) )
Output :

{ 'O', 'N' }
{ 12 }

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription