''' This program demonstrates sets, tuples and dictionaries. ''' #Both of the lines below create an empty set # Sets contain unordered, unique data points set1 = set() set2 = set([]) print(set1) print(set2) #If we use a sequence type in the set constructor, each individual element of # the sequence type becomes an element of the set set1 = set('Spongebob Squarepants') print(set1) #If we want to add multiple sequence types, we can add them individually using # a loop list1 = ['Sponge','Squid','Star','Sandy'] for _ in list1: set2.add(_) print(set2) #All the standard set operations are availble. print(set1 | set2) # set union print(set1 & set2) # set intersection print(set1 - set2) # Set difference print(set1 ^ set2) #Symmetric difference '''tuples are immutable lists. Once a tuple is constructed, it cannot be changed ''' # To create a tuple we can just put a commas separated list of elements in () t1 = (1,2,3,4) #if we only have one element, we use a trailing comma, or Python will evaluate #the value and create a regular variable. t2 = ("Spongebob",) #tuples support the immutable sequence type operators. print(t1+t2) #concatenation print(t1*3) #replicates contents 3 times ''' tuples are used to pass parameters into functions using a process called tuple packing. The tuple is passed to the function and is unpacked into individual parameter variables at the function ''' t3 = "Squidward", "Clarinet", "Cashier", "Unrecognized Talent" #packing name, instrument, job, complaint = t3 #unpacking ''' dictionaries are a set of key-value pairs. They are accessed using the key instead of a numerical index. To create a dictionary, use {} ''' d1 = {} #empty dictionary d1 = { 'Name':'Spongebob', 'pet':'Gary', 24:25} print(d1) # If we mention a key for the first time, it adds the key-value pair to the dict d1['Friend'] = 'Patrick' print(d1) #If we mention a key that already exists, it updates the value d1[24] = [25, 26, 27] print(d1) #Zipping is the process of taking 2 equal sized lists and creating a dictionary # where the first list forms the keys and the second list forms the corresponding # values d2 = dict(zip(['Name','Major','Home'],['Patrick','Wumbo','Rock'])) print(d2)