t = (12,-1) print type(t) print isinstance(t,tuple) print len(t) t = (12,"monty",True,-1.23e6) print t[1] print t[-1] t[-2:] # get the last two elements, return as a tuple x = (True) ; print type(x) x = (True,) ; print type(x) type(()), len(()) type((,)) t[2] = False t[0:2], False, t[3:] ## the above it ## not what we wanted... need to concatenate t[0:2] + False + t[3:] y = t[0:2] + (False,) + t[3:] ; print y t*2 v = [1,2,3] ; print len(v), type(v) v[0:2] v = ["eggs","spam",-1,("monty","python"),[-1.2,-3.5]] len(v) v[0] ="green egg" v[1] += ",love it." v[-1] v[-1][1] = None ; print v v = v[2:] ; print v # let's make a proto-array out of nested lists vv = [ [1,2], [3,4] ] print len(vv) determinant = vv[0][0]*vv[1][1] - vv[0][1]*vv[1][0] print determinant v = [1,2,3] v.append(4) v.append([-5]) ; print v v = v[:4] w = ['elderberries', 'eggs'] v + w v.extend(w) ; print v v.pop() print v v.pop(0) ; print v ## pop the first element v = [1,3, 2, 3, 4, 'elderberries'] v.sort() ; print v v.sort(reverse=True) ; print v v.index(4) ## lookup the index of the entry 4 v.index(3) v.count(3) v.insert(0,"it's full of stars") ; print v v.remove(1) ; print v a = ['cat', 'window', 'defenestrate'] for x in a: print x, len(x) for i,x in enumerate(a): print i, x, len(x) for x in a: print x, x = range(4) ; print x total = 0 for val in range(4): total += val print "By adding " + str(val) + \ " the total is now " + str(total) total = 0 for val in range(1,10,2): total += val print "By adding " + str(val) + \ " the total is now " + str(total) a = ['Mary', 'had', 'a', 'little', 'lamb'] for i in range(len(a)): print i, a[i] {1,2,3,"bingo"} print type({1,2,3,"bingo"}) print type({}) print type(set()) set("spamIam") a = set("sp"); b = set("am"); print a ; print b c = set(["a","m"]) c == b "p" in a "ps" in a q = set("spamIam") a.issubset(q) a | b q - (a | b) q & (a | b) # this is pretty volitile...wont be the same # order on all machines for i in q & (a | b): print i, q.remove("a") q.pop() print q.pop() print q.pop() print q.pop() q.pop() # number 1...you've seen this d = {"favorite cat": None, "favorite spam": "all"} # number 2 d = dict(one = 1, two=2,cat = 'dog') ; print d # number 3 ... just start filling in items/keys d = {} # empty dictionary d['cat'] = 'dog' d['one'] = 1 d['two'] = 2 d # number 4... start with a list of tuples mylist = [("cat","dog"), ("one",1),("two",2)] print dict(mylist) dict(mylist) == d d = {"favorite cat": None, "favorite spam": "all"} d = {'favorites': {'cat': None, 'spam': 'all'}, \ 'least favorite': {'cat': 'all', 'spam': None}} print d['least favorite']['cat'] phone_numbers = {'family': [('mom','642-2322'),('dad','534-2311')],\ 'friends': [('Billy','652-2212')]} for group_type in ['friends','family']: print "Group " + group_type + ":" for info in phone_numbers[group_type]: print " ",info[0], info[1] # this will return a list, but you dont know in what order! phone_numbers.keys() phone_numbers.values() for group_type in phone_numbers.keys(): print "Group " + group_type + ":" for info in phone_numbers[group_type]: print " ",info[0], info[1] groups = phone_numbers.keys() groups.sort() for group_type in groups: print "Group " + group_type + ":" for info in phone_numbers[group_type]: print " ",info[0], info[1] for group_type, vals in phone_numbers.iteritems(): print "Group " + group_type + ":" for info in vals: print " ",info[0], info[1] phone_numbers['co-workers'] phone_numbers.has_key('co-workers') print phone_numbers.get('co-workers') phone_numbers.get('friends') == phone_numbers['friends'] print phone_numbers.get('co-workers',"all alone") # add to the friends list phone_numbers['friends'].append(("Marsha","232-1121")) print phone_numbers ## billy's number changed phone_numbers['friends'][0][1] = "532-1521" phone_numbers['friends'][0] = ("Billy","532-1521") ## I lost all my friends preparing for this Python class phone_numbers['friends'] = [] # sets this to an empty list ## remove the friends key altogether print phone_numbers.pop('friends') print phone_numbers del phone_numbers['family'] print phone_numbers phone_numbers.update({"friends": [("Billy's Brother, Bob", "532-1521")]}) print phone_numbers