# How to find the intersection, union, difference and symmetric difference set of the set in Python.

Time：2020-11-23

In fact, intersection, union, difference and symmetric difference sets are also special operations of sets.

A & B: the common element of two sets of intersection table, equivalent to A. intersection (b)

A | B: Union table all elements of two sets, equivalent to A. Union (b)

A-B: elements of difference table belonging to a but not to B, equivalent to A. difference (b)

A ^ B: the non common elements of two sets, equivalent to a.symmetric_ difference(b)

The code is as follows:

Find the intersection of sets

``````#Define two sets
x = set("abc")
y = set("cdef")
#Set intersection
result = x & y
print(result)
#Use intersection () to find the intersection
result = x.intersection(y)
print(result)``````

Operation results:

Find the union of sets

``````x = set("abc")
y = set("cdef")
#Use "|" for union
result = x | y
print(result)
#Union() method for union
result = x.union(y)
print(result)``````

Operation results:

Find the difference set of the set

``````x = set("abc")
y = set("cdef")
#Use "-" to find the difference set (only belongs to x, not to y)
result = x - y
print(result)
#Use difference() to find the difference set
result = x.difference(y)
print(result)``````

Operation results:

Find the symmetric difference set of a set

``````x = set("abc")
y = set("cdef")
#Use ^ to find symmetric difference sets (two sets are not common elements)
result = x ^ y
print(result)
#Using symmetric_ Difference() for symmetric difference set
result = x.symmetric_difference(y)
print(result)``````

Operation results:

## Inventory 2021 interview high frequency questions: brush Java brain map, successfully scored the US group, byte, trembling sound, tiktok and so on 7 offer!

In 2021, we can easily obtain offers from five major manufacturers. In fact, these credits are due to the work of Ali teamJava core mind map, Xiaobian shares it here today and hopes to help more partners get more high paying offers; This article is divided into two parts Ali Java mind map Core summary […]