# Collection Types ''' LISTS - Ordered - Mutable - Can contain any type of data ''' # Empty lis0 = [] # Declaration lis1 = [1,2,3] # Indexing elem1 = lis1[0] ''' TUPLES - Ordered - Immutable - Can contain any type of data - Near-identical in function to lists, more efficient ''' # Empty tup0 = () # Declaration tup1 = (1,2,3) tup1 = 1,2,3 # Indexing elem2 = tup1[0] ''' ARRAYS - Ordered - Mutable - Can contain only one pre-specified type of data - Similar to lists but more efficient ''' ''' SETS - Unordered - Mutable - Contains unique items - Implements most operations for mathematical sets ''' set1 = set(['a','b','c']) set2 = set(['c','d','e']) set3 = set(['a','b']) # Union s4 = set1 | set2 # Intersection s5 = set1 & set2 # Subtraction s6 = set1 - set3 # Subset if set3 <= set1: print "Subset" # Proper Subset if set3 <= set1: print "Proper subset" ''' DICTIONARIES - Set of key-to-value mappings - Mutable - Can contain any type of data as values - Can use many types of data as keys (only tuples work in the realm of collection types) ''' # Empty dict0 = {} # Declaration dict1 = {'a':1, 'b':2, 'c':3} # Indexing elem3 = dict1['b'] # Test for key pair if dict1.has_key('c'): print "Got a C"