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.



Example


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)



Output


Picture showing the output of sets operations in python



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

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250