paul@206 | 1 | l = [1, 2, 3] |
paul@221 | 2 | l.append("four") |
paul@304 | 3 | print len(l) # 4 |
paul@266 | 4 | print l[0] # 1 |
paul@266 | 5 | print l[1] # 2 |
paul@266 | 6 | print l[2] # 3 |
paul@266 | 7 | print l[3] # four |
paul@266 | 8 | print l[-1] # four |
paul@266 | 9 | print l[-2] # 3 |
paul@266 | 10 | print l[-3] # 2 |
paul@266 | 11 | print l[-4] # 1 |
paul@266 | 12 | print l # [1, 2, 3, "four"] |
paul@227 | 13 | |
paul@227 | 14 | t = (1, 2, 3, "four") |
paul@227 | 15 | l = list(t) |
paul@266 | 16 | print l # [1, 2, 3, "four"] |
paul@266 | 17 | |
paul@266 | 18 | try: |
paul@266 | 19 | print l[4] # should raise an exception |
paul@266 | 20 | except IndexError, exc: |
paul@266 | 21 | print "l[4]: failed with argument", exc.index |
paul@266 | 22 | |
paul@266 | 23 | try: |
paul@266 | 24 | print l[-5] # should raise an exception |
paul@266 | 25 | except IndexError, exc: |
paul@266 | 26 | print "l[-5]: failed with argument", exc.index |
paul@279 | 27 | |
paul@279 | 28 | print 1 in l # True |
paul@279 | 29 | print 4 in l # False |
paul@279 | 30 | print "four" in l # True |
paul@279 | 31 | print "one" in l # False |
paul@279 | 32 | print 1 not in l # False |
paul@279 | 33 | print 4 not in l # True |
paul@279 | 34 | print "four" not in l # False |
paul@279 | 35 | print "one" not in l # True |
paul@282 | 36 | |
paul@282 | 37 | print l.index(1) # 0 |
paul@282 | 38 | print l.index("four") # 3 |
paul@282 | 39 | |
paul@282 | 40 | try: |
paul@282 | 41 | print l.index(4) # should raise an exception |
paul@282 | 42 | except ValueError, exc: |
paul@282 | 43 | print "l.index(4): failed to find argument", exc.value |
paul@460 | 44 | |
paul@460 | 45 | print l == [1, 2, 3] # False |
paul@460 | 46 | print l == [1, 2, 3, "four"] # True |