''' 20210928 Robin Dawes Creating and using sets ''' # Create a set and initialize it. # Note that we use { } here, just as for dictionaries cheerful = {'Kim', 'Pat', 'Ricky', 'Terry', 'Parker', 'Brady', 'Jay'} # Create an empty set artistic = set() # Add some new elements one at a time using "add" artistic.add('Gabriel') artistic.add('Skyler') # Add new elements in batches using "update" # The argument to "update" can be any iterable object artistic.update( {'Ricky', 'Shay'}) # Note that we are attempting to add an element that is already in the set ("Shay") # This does not cause an error # The element does not get duplicated - sets contain no duplicates artistic.update( ['Shay', 'Pat'] ) # printing a set all at once print(cheerful) print(artistic) # iterating through a set for n in cheerful: print(n) # removing an element print("\nRemove Terry") # trying to remove an element that is not there will cause an error cheerful.remove('Terry') print(cheerful) # discarding an element print("\nDiscard Parker and Keagan (who?)") cheerful.discard('Parker') # trying to discard an element that is not there will NOT cause an error cheerful.discard('Keagan') print(cheerful) # create the union of two sets print("\nUnion") everyone = cheerful.union(artistic) print(everyone) # create the intersection of two sets print("\nIntersection") people_in_both = cheerful.intersection(artistic) print(people_in_both) # create the symmetric difference of two sets print("\nSymmetric Difference") one_but_not_both = cheerful.symmetric_difference(artistic) print(one_but_not_both) # test to see if one set is a subset of another set print("\nSubset") painters = {'Skyler', 'Ricky'} print(painters) if painters.issubset(artistic): print ("painters is a subset of artistic") else: print ("painters is not a subset of artistic") # there are many more set operations!