Python set operations union, intersection, difference, symmetric_difference & updating the main set

preview_player
Показать описание
We will be using various set operations to get union, intersection , difference and sysmmetric_difference() values using more than one set.
Union
Union method or pipe operator ( | ) takes one or more sets and returns all the elements of set after removing the duplicates.
A={1,2,3}
B={3,4,5}
Print(A | B)
Output
{1, 2, 3, 4, 5}
Using union method
Print(A.union(B))
Intersection
A={1,2,3}
B={3,4,5}
Print(A & B )
Using intersection method
Print(A.intersection(B))
Diference
A={1,2,3}
B={3,4,5}
Print(A – B)
Using difference method
Print(A.difference(B))
Symmetric_difference
A={1,2,3}
B={3,4,5}
Print(A ^ B)
Using summetric_difference method
Print(A.symmetric_difference(B))
Рекомендации по теме