- Home
- Course Detail
regularpython@gmail.com
You are now watching:
Python Sets / of Python Set Operators
Set1 = {1, 2, 3, 4, 5} Set2 = {3, 4, 5, 6, 7} # key in s�������# containment check if 2 in Set1: print("value found") else: print("value not found") # ******************************************************************** # # key not in set # non-containment check if 2 not in Set1: print("value not found") else: print("value found") # ******************************************************************** # # set1 == set2 # set1 is equivalent to set2 Set1 = {1, 2, 3, 4, 5} # Set2 = {3, 4, 5, 6, 7} Set2 = {1, 2, 3, 4, 5} if Set1 == Set2: print("set1 and set2 are equal") else: print("set1 and set2 are not equal") # ******************************************************************** # # set1 != set2 # set1 is not equivalent to set2 Set1 = {1, 2, 3, 4, 5} Set2 = {3, 4, 5, 6, 7} if Set1 != Set2: print("set1 and set2 are not equal") else: print("set1 and set2 are equal") # ******************************************************************** # # # set1 <= set2 # set1 is subset of set2 Set1 = {1, 2, 3, 4, 5} Set2 = {3, 4, 5, 6, 7} # Set1 = {4, 5} Set1 = {3, 4, 5, 6, 7} if Set1 <= Set2: print("set1 is subset of set2") else: print("set1 is not subset of set2") # ******************************************************************** # # # set1 < set2 # set1 is proper subset of set2 Set1 = {1, 2, 3, 4, 5} Set2 = {3, 4, 5, 6, 7} Set1 = {3,4} if Set1 < Set2: print("set1 is proper subset of set2") else: print("set1 is not proper subset of set2") # ******************************************************************** # # # set1 >= set2 # set1 is superset of set2 Set1 = {1, 2, 3, 4, 5} # Set2 = {3, 4, 5, 6, 7} # Set2 = {1, 2, 3, 4, 5} Set2 = {4, 5} if Set1 >= Set2: print("set1 is superset of set2") else: print("set1 is not superset of set2") # ******************************************************************** # # set1 > set2 # set1 is proper superset of set2 Set1 = {1, 2, 3, 4, 5} # Set2 = {3, 4, 5, 6, 7} Set2 = {4, 5} if Set1 > Set2: print("set1 is proper superset of set2") else: print("set1 is not proper superset of set2") # ******************************************************************** # # set1 | set2 # the union of set1 and set2 Set1 = {1, 2, 3, 4, 5} Set2 = {3, 4, 5, 6, 7} print(Set1 | Set2) # ******************************************************************** # # set1 & set2 # the intersection of set1 and set2 Set1 = {1, 2, 3, 4, 5} Set2 = {3, 4, 5, 6, 7} print(Set1 & Set2) # ******************************************************************** # # set1 – set2 # the set of elements in set1 but not set2 Set1 = {1, 2, 3, 4, 5} Set2 = {3, 4, 5, 6, 7} print(Set2 - Set1)