Tuples

Tuples are a fixed sequence of arbitrary objects

  • Size and contents are fixed at time of creation.
  • Immutable
  • There are no methods, but the following functions are useful
     list(s)            # Convert a sequence to a list
     tuple(s)           # Convert a sequence to a tuple 

Other useful tuple properties

  • Assignment to multiple values
     t = (4,5,6)
     x,y,z = t       # Expands contents into variables 
  • Multiple function return values
  • Can be used as dictionary keys
     d = { }
     d[1,2,3] = "Foo"    # Same as d[(1,2,3)] = "Foo"
     d[1,0,2] = "Bar"    # Same as d[(1,0,2)] = "Bar"
<<< O'Reilly OSCON 2000, Introduction to Python, Slide 40
July 17, 2000, beazley@cs.uchicago.edu
>>>