Articles → PYTHON → sets operations in python

sets operations in python

Sets operations

Operation NameSymbolDescription
Union |Returns all elements from set 1 and set 2.
Intersection&Returns the common elements between set 1 and set 2
Difference-Returns element of set 1 that does not exist in set 2
Symmetric Difference^Returns all elements of set 1 and set 2 except the common element.


set1 = {1,2,3,4,5}
set2 = {5,6,7,8}

print("Union of Set 1 and Set 2 is:", set1|set2)
print("Intersection of Set 1 and Set 2 is:", set1&set2)
print("Difference between Set 1 and Set 2 is:", set1-set2)
print("Symmetric Difference between Set 1 and Set 2 is:", set1^set2)


Picture showing the output of sets operations in python

Click to Enlarge

Posted By  -  Karan Gupta
Posted On  -  Friday, October 1, 2021


Your Email Id  
Query/FeedbackCharacters remaining 250