Python Glossary Русский

all Python 3Built-in functionsterm

tuple

types

An immutable ordered sequence of items. Hashable (if its items are) — it can be used as a dictionary key.

Syntax

tuple(iterable)

It is the comma, not the parentheses, that makes a tuple: (1) is just a number, while a one-element tuple is written (1,). Immutability is shallow — a list stored inside can still be mutated, and such a tuple is no longer hashable, so using it as a dict key raises TypeError.

Examples

print(tuple('abc'))    # → ('a','b','c')
print(tuple([1,2,3]))  # → (1,2,3)
t = (1,2,3)
a,b,c = t
print(a,b,c)  # → 1 2 3
print(tuple(zip([1,2],[3,4])))  # → ((1,3),(2,4))

See also

Open in the full glossarydocs.python.orgReport an inaccuracy