1 def f(d): 2 return d.keys() 3 4 def g(d): 5 for key, value in d.items(): 6 return value 7 8 d = {10 : "a", 20 : "b", "c" : 30, (1, 2) : "d"} 9 print "# d: ", 10 print d 11 print d[10] # a 12 print d[20] # b 13 print d["c"] # 30 14 print d[(1, 2)] # d 15 try: 16 print d[30] # should fail with an exception 17 except KeyError, exc: 18 print "d[30]: key not found", exc.key 19 20 l = f(d) 21 print "# l: ", 22 print l 23 print 10 in l # True 24 print 20 in l # True 25 print "c" in l # True 26 print "d" in l # False 27 print 30 in l # False 28 print (1, 2) in l # True 29 30 l = d.values() 31 print "# l: ", 32 print l 33 print "a" in l # True 34 print "b" in l # True 35 print "c" in l # False 36 print "d" in l # True 37 print 30 in l # True 38 print (1, 2) in l # False 39 40 v = g(d) # either "a" or "b" or 30 or "d" 41 print "# v: ", 42 print v 43 print v == "a" or v == "b" or v == 30 or v == "d" # True 44 print v == 10 or v == 20 or v == "c" or v == (1, 2) # False 45 46 l = d.items() 47 print "# l: ", 48 print l 49 print (10, "a") in l # True 50 print ("c", 30) in l # True 51 print ((1, 2), "d") in l # True 52 print (10, "b") in l # False 53 54 # Try to put a list key in a dictionary. 55 56 try: 57 d[[1, 2]] = "e" 58 print d[[1, 2]] 59 except TypeError: 60 print "d[[1, 2]]: key not appropriate"