You are a guest. Restricted access. Read more

Tuples

Tuples are just like lists, but you can't change their values. Again, each value is numbered starting from zero, for easy reference.


months = ('January','February','March','April','May')
print months

Note we use parentheses rather than square brackets.

Sometime tuples are used to associate some values in pairs. In this example we made a list with pairs. Each pair is tuple (so cannot be changed).


stop=1
attention=2
go=3
pairs = [(stop, 'red'), (attention, 'yellow'), (go, 'green')]
print pairs

Here we associated integer values with strings. How we can work with such compound object? As usual, one can loop over each item (pair) and print it:


stop=1
attention=2
go=3
pairs = [(stop, 'red'), (attention, 'yellow'), (go, 'green')]
for j in pairs:
     print j         # we print pairs
     print j[0],j[1] # print each value in the pair

python/collections/tuples.txt · Last modified: 2013/04/14 16:52 (external edit)
Back to top
CC Attribution-Share Alike 3.0 Unported
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0