What a tuple is

A tuple is a sequence data type that can contain elements of different data types. Tuples are different from lists because tuples are immutable. If you want to change the contents of a tuple, you must create a new tuple that has the new content you want.

Warning If a tuple contains a mutable element (such as a list or dictionary), the mutable element can be modified. However, some of the obvious ways of changing the mutable element will cause errors because Python will attempt to modify the tuple. We recommend that you avoid changing mutable elements in tuples.

Tuples don't include a lot of special methods, the way strings and lists do. But tuples do include methods that support standard Python operators. For example, you can add tuples, check whether an item is inside a tuple, slice elements in a tuple, compare a tuple to other data, find a tuple's length, and so on.

Many Python functions, such as the time and date functions, return tuples.

Was this article helpful?

0 0

Post a comment