1 l = [1, 2, 3] 2 l.append("four") 3 print len(l) # 4 4 print l[0] # 1 5 print l[1] # 2 6 print l[2] # 3 7 print l[3] # four 8 print l[-1] # four 9 print l[-2] # 3 10 print l[-3] # 2 11 print l[-4] # 1 12 print l # [1, 2, 3, "four"] 13 14 t = (1, 2, 3, "four") 15 l = list(t) 16 print l # [1, 2, 3, "four"] 17 18 try: 19 print l[4] # should raise an exception 20 except IndexError, exc: 21 print "l[4]: failed with argument", exc.index 22 23 try: 24 print l[-5] # should raise an exception 25 except IndexError, exc: 26 print "l[-5]: failed with argument", exc.index 27 28 print 1 in l # True 29 print 4 in l # False 30 print "four" in l # True 31 print "one" in l # False 32 print 1 not in l # False 33 print 4 not in l # True 34 print "four" not in l # False 35 print "one" not in l # True 36 37 print l.index(1) # 0 38 print l.index("four") # 3 39 40 try: 41 print l.index(4) # should raise an exception 42 except ValueError, exc: 43 print "l.index(4): failed to find argument", exc.value 44 45 # Test equality. 46 47 print l == [1, 2, 3] # False 48 print l == [1, 2, 3, "four"] # True 49 50 # Test concatenation. 51 52 a = [1, 2] 53 a += [3, 4] 54 print a # [1, 2, 3, 4] 55 56 b = [5, 6] 57 c = a + b 58 print c # [1, 2, 3, 4, 5, 6] 59 60 # Test removal. 61 62 print c.pop() # 6 63 print c # [1, 2, 3, 4, 5] 64 65 d = [] 66 try: 67 d.pop() # should raise an exception 68 except IndexError, exc: 69 print "d.pop(): failed to access item", exc.index