paul@227 | 1 | l = (1, 2, 3, "four") |
paul@266 | 2 | print len(l) # 4 |
paul@266 | 3 | print l[0] # 1 |
paul@266 | 4 | print l[1] # 2 |
paul@266 | 5 | print l[2] # 3 |
paul@266 | 6 | print l[3] # four |
paul@266 | 7 | print l[-1] # four |
paul@266 | 8 | print l[-2] # 3 |
paul@266 | 9 | print l[-3] # 2 |
paul@266 | 10 | print l[-4] # 1 |
paul@266 | 11 | print l # (1, 2, 3, "four") |
paul@227 | 12 | |
paul@227 | 13 | l = [1, 2, 3, "four"] |
paul@227 | 14 | t = tuple(l) |
paul@266 | 15 | print t # (1, 2, 3, "four") |
paul@266 | 16 | |
paul@266 | 17 | try: |
paul@266 | 18 | print t[4] # should raise an exception |
paul@266 | 19 | except IndexError, exc: |
paul@266 | 20 | print "t[4]: failed with argument", exc.index |
paul@266 | 21 | |
paul@266 | 22 | try: |
paul@266 | 23 | print t[-5] # should raise an exception |
paul@266 | 24 | except IndexError, exc: |
paul@266 | 25 | print "t[-5]: failed with argument", exc.index |
paul@835 | 26 | |
paul@835 | 27 | a, b, c, d = l |
paul@835 | 28 | print a, b, c, d # 1 2 3 "four" |
paul@835 | 29 | |
paul@835 | 30 | try: |
paul@835 | 31 | a, b, c = l |
paul@835 | 32 | except ValueError, exc: |
paul@835 | 33 | print "a, b, c = l: failed with length", exc.value |
paul@857 | 34 | |
paul@857 | 35 | # Add tuples together. |
paul@857 | 36 | |
paul@857 | 37 | m = ("five", 6, 7, 8) |
paul@857 | 38 | n = t + m |
paul@857 | 39 | print n # (1, 2, 3, "four", "five", 6, 7, 8) |
paul@857 | 40 | |
paul@857 | 41 | # Add to list, really testing tuple iteration. |
paul@857 | 42 | |
paul@857 | 43 | o = l + m |
paul@857 | 44 | print o # [1, 2, 3, "four", "five", 6, 7, 8] |
paul@857 | 45 | |
paul@857 | 46 | try: |
paul@857 | 47 | print t + l # should raise an exception |
paul@857 | 48 | except TypeError: |
paul@857 | 49 | print "t + l: failed due to tuple-list addition" |