Lesson 6
Dealing with Sequences of Objects
Chapter 7: Tuples
Tuples
Besides list
s, Python also provides us with another sequence type called tuple
s.
First question: How do you pronounce tuple
?
The Python Benevolent dictator for life himself has answered the question:
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!)