The set.intersection() method in Python returns a new set containing only the elements that are common to all given sets. It compares two or more sets and keeps only shared values. The original sets remain unchanged.

Example: In this example, common elements between two sets are found using intersection().
a = {1, 2, 3}
b = {2, 3}
print(a.intersection(b))
Output
{2, 3}
Explanation: a.intersection(b) returns elements present in both sets, only 2 and 3 are common.
Syntax
set1.intersection(set2, set3, ...)
- Parameters: One or more sets to compare. If no argument is given, it returns a copy of the original set.
- Return Value: Returns a new set containing common elements.
Examples
Example 1: In this example, the intersection of two and three sets is calculated.
a = {2, 4, 5, 6}
b = {4, 6, 7, 8}
c = {4, 6, 8}
print(a.intersection(b))
print(a.intersection(b, c))
Output
{4, 6}
{4, 6}
Explanation:
- a.intersection(b) returns common elements of a and b.
- a.intersection(b, c) returns elements common to all three sets.
Example 2: Here, the & operator is used as a shortcut for intersection.
a = {2, 4, 5, 6}
b = {4, 6, 7, 8}
c = {1, 0, 12}
print(a & b)
print(a & c)
print(a & b & c)
Output
{4, 6}
set()
set()
Explanation:
- a & b returns common elements of both sets.
- a & c returns set() because no elements match.
- a & b & c returns common elements among all three sets.
Example 3: In this example, intersection() is compared with symmetric_difference().
a = {2, 4, 5, 6}
b = {4, 6, 7, 8}
print(a.intersection(b))
print(a.symmetric_difference(b))
Output
{4, 6}
{2, 5, 7, 8}
Explanation:
- a.intersection(b) returns common elements.
- a.symmetric_difference(b) returns elements not common in both sets.
- Intersection keeps shared values, while symmetric difference keeps non-shared values.