This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 7: Tuples

Tuples

face Josiah Wang

Besides lists, Python also provides us with another sequence type called tuples.

First question: How do you pronounce tuple?

The Python Benevolent dictator for life himself has answered the question:

How do you pronounce tuple? Guido von Rossum: 'I pronounce tuple too-pull on Mon/Wed/Fri and tub-pull on Tue/Thu/Sat. On Sunday I don't talk about them. :)'

Tuples are like lists. The main difference is that lists are mutable (can be modified), while tuples are immutable (cannot be changed).

So unlike lists, you cannot add/remove items to/from a tuple once you have created it.

Tuples are useful for representing ordered, fixed-size grouping of elements, like 2D coordinates (x,y) or 3D coordinates (x,y,z).

For example, (5,3) is an entity in its own right, so you cannot replace, remove, or add new members to it. If you want a new vector, e.g. (2,3), you should create a brand new vector rather than modifying the 5 in (5,3) to be 2. Sort of like how you cannot replace Edward in the group Jedward with someone else and still call them Jedward the twins. You have to form a new group with John and the new member (and they will not be twins!)

Jedward

Image credits: Michael Dorausch from Venice, USA / CC BY-SA