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 a2 = a * 2 61 print a2 # [1, 2, 3, 4, 1, 2, 3, 4] 62 63 # Test removal. 64 65 print c.pop() # 6 66 print c # [1, 2, 3, 4, 5] 67 68 d = [] 69 try: 70 d.pop() # should raise an exception 71 except IndexError, exc: 72 print "d.pop(): failed to access item", exc.index 73 74 # Test insertion. 75 76 e = [] 77 78 try: 79 e.insert(1, 1) # should raise an exception 80 except IndexError, exc: 81 print "e.insert(1, 1): failed to insert at index", exc.index 82 83 e.insert(0, 1) 84 print e # [1] 85 86 try: 87 e.insert(2, 1) # should raise an exception 88 except IndexError, exc: 89 print "e.insert(2, 1): failed to insert at index", exc.index 90 91 e.insert(1, 2) 92 print e # [1, 2] 93 e.insert(0, 3) 94 print e # [3, 1, 2] 95 96 # Test reversal. 97 98 e.reverse() 99 print e # [2, 1, 3]