tuple is a container for a fixed sequence
of data objects. The name comes from the Latin suffix for multiples:
Mathematicians commonly consider ordered pairs;
for instance, most analytical geometry is done with Cartesian
), an ordered
pair, double, or 2-tuple.
An essential ingredient here is that a
tuple has a fixed and known number of elements.
For example a 2-dimensional geometric point might have a
. A 3-dimensional point might be a
. The size of the
tuple can't change without fundamentally
redefining the problem we're solving.
tuple is an immutable sequence of Python
objects. Since it is a sequence, all of the common operations to
sequences apply. Since it is immutable, it cannot be changed. Two common
questions that arise are how to expand a
and how to remove objects from a
When someone asks about changing an element inside a
tuple, either adding, removing or updating, we
have to remind them that the
list, covered in
Chapter 14, Lists
, is for dynamic sequences of elements.
tuple is generally applied when the number of
elements is fixed by the nature of the problem. For example,
2-dimensional geometry, or a 4-part internet address, or a
Red-Green-Blue color code. We don't change
create new ones.
tuple processing even pervades the way
functions are defined. We can have positional parameters collected into
tuple, something we'll cover in the section called “Advanced Parameter Handling For Functions”.