Using the tuple collection

The tuple is one of the simplest collections available in Python. It is one of the many kinds of Python sequences. A tuple has a fixed number of items. For example, we might work with (x, y) coordinates or (r, g, b) colors. In these cases, the number of elements in each tuple is fixed by the problem domain. We don't want the flexibility of a collection that can vary in length.

Generally, we'll include () around a tuple to set it apart from the surrounding syntax. This isn't always required; Python creates tuple objects implicitly in some common contexts. However, it is always a good idea. If we write an assignment statement like this:

a = 2, 3

This statement will implicitly create a 2-tuple, (2, 3), and assign the object ...

Get Python Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.