Python notes - Tuples

Contents   Installing Python   Variables   Input/Output   if   while   Functions - def   Scopes (local/global)   Function parameters   Lists (arrays)   Dictionaries   Tuples   Files   Libraries   Hints, tips  

A tuple (pronounced too- pull, some folks say) is a collection of related data, rather like a list, but not as flexible. A tuple cannot be modified after it is created. We enclose a tuple in (...). In an assignment, we can omit the (...), as in:

t = (1, 6)          # a tuple  
s = 3, 4            #another one

print s             #  (3, 4)
print s[0]          #  3
n = s[0]            # ok
s[0] = 5            # NO - can't modify a tuple
s.append(33)        # NO     ""    ""
if s == t:          # compare   (these two are not equal)
    print "equal"
else:
    print "not"     # the use of ==  for lists is tricky - look at the lists page
Tuples are stored as values, not as references (unlike lists)

s = t              # copies 1, 6   into   s

Contents   Installing Python   Variables   Input/Output   if   while   Functions - def   Scopes (local/global)   Function parameters   Lists (arrays)   Dictionaries   Tuples   Files   Libraries   Hints, tips