Notes
A set is an unordered collection with no duplicate elements.
Create Set Syntax
set_name = {hashable_value,...}
or
set_name = set(iterable)
frozenset() immutable set
Set Methods
intersection()
union()
difference()
symmetric_difference()
Set Examples
set1 = set(["h", "e", "l", "l", "o"]) #create a set called set1
print(set1) #output {'o', 'e', 'h', 'l'}. You can see that there is only one "l" in the set because a set does not allow duplicate elements.
set2 = set("world") #create a set called set2
print(set2) #output {'d', 'o', 'r', 'l', 'w'}. You can see that the order of the letters are not that same as the order of letter you inputted; it is still the same set because the order does not matter in a set.
#Add a new element to set1
set1.add("r") #The element will be added if it is not already in the set
print(set1) #output {'r', 'o', 'e', 'h', 'l'}.
#Check whether an item is an element of a set.
print("r" in set1) #output True
#Check whether an item is not an element of a set.
print("m" not in set1) #output True
#The number of elements in a set
print(len(set1)) #output 5
#Copy a set
set1_copy = set1.copy()
print(set1_copy) #output {'l', 'o', 'r', 'e', 'h'}. The copy of a set may not has the same order as the original set; but since the order does not matter, they are still equal.
#Testing whether two sets are equal
print(set1 == set1_copy) #output True
#Clear a set
set1_copy.clear()
print(set1_copy) #output set()
#Remove an element using discard method
set2.discard("d") #If the specified element is in the set, then it will be removed. If the specified element is not in the set, nothing will happen.
print(set2) #output {'o', 'r', 'l', 'w'}
#Remove an element using remove method
set2.remove("o") #If the specified element is in the set, then it will be removed. If the specified element is not in the set, an error will occur.
print(set2) #output {'r', 'l', 'w'}
#Remove and return an arbitrary element
element_removed = set1.pop()
print(element_removed ) #output r
#Intersection of two sets
print(set1.intersection(set2)) #output {'l'}
print(set1 & set2) #Alternatively we can use the "&" operator to achieve the same result.
#Union of two sets
print(set1.union(set2)) #output {'o', 'r', 'h', 'w', 'e', 'l'}
print(set1 | set2) #Alternatively we can use the "|" operator to achieve the same result.
#Difference of two sets
print(set1.difference(set2)) #output {'o', 'e', 'h'}
print(set1 - set2) #Alternatively we can use the "-" operator to achieve the same result.
#Symmetric_Difference of two sets
print(set1.symmetric_difference(set2)) #output {'h', 'r', 'w', 'e', 'd'}
print(set1 ^ set2) #Alternatively we can use the "-" operator to achieve the same result.
set3 = set(["l","w"])
#Determine whether a set is a subset of another set
print(set3.issubset(set2)) #output True
print(set3 <= set2) #Alternatively we can use the "<=" operator to achieve the same result. output True
#Determine whether a set is a superset of another set
print(set2.issuperset(set3)) #output True
print(set2 >= set3) #Alternatively we can use the ">=" operator to achieve the same result. output True
Notes
A dictionary is an associative array that maps each key to its value. dictionary keys are immutable such as Integers, Floats, Tuples, Strings. etc.
Create a Dictionary
dict_name = {key:value, ...}
or
dict_name = dict([[key,value], ...])
Dictionary Examples
dict1 = {"cake":3.5, "soda":1, "pasta":1.99} #create a dictionary called dict1
#Get the value corresponds to a given key
print(dict1["soda"]) #output 1
#Add a new key-value pair
dict1["bread"] = 0.99
print(dict1) #output {'soda': 1, 'bread': 0.99, 'pasta': 1.99, 'cake': 3.5}
#Number of key-value pairs
print(len(dict1)) #output 4
#Remove a key and its value
del dict1["bread"]
print(dict1) #output {'pasta': 1.99, 'cake': 3.5, 'soda': 1}. If the key given is not in the dictionary then an error will occur.
Dictionary Methods
keys() #return list of keys
values() #return list of values
items() #return list of keys and values as teples