On this page
tuple
Prev: super | Up: __builtins__ | Next: type
Rel: bool, int, float, complex, str
Ref: docs.python.org โ tuple
Tags: #public #python3 #builtins
Import & Availability
# Built-in (available globally without import)
from builtins import tuple
Overview & Signature
tuple(iterable=(), /)
Built-in immutable sequence.
Common Methods & Attributes
tuple.count(self, value, /)โ Return number of occurrences of value.tuple.index(self, value, start=0, stop=9223372036854775807, /)โ Return first index of value.
Usage
single = (42,) # trailing comma makes it a 1-tuple!
x, *rest = (1, 2, 3, 4)
assert isinstance(single, tuple) and x == 1 and rest == [2, 3, 4]
Gotchas
- Single-element tuples require a trailing comma:
(42)is justint, while(42,)is atuple. - Immutable container, not deep-immutable: A tuple containing a
list(([1],)) is unhashable and its inner list can still be mutated.
REPL Inspect
help(tuple)
Linked from
Related notes
- tuts/python3/__builtins__/__builtins__ Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/super Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/type Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__ links to this page; shared tags: #builtins
- tuts/python3/__builtins__/bool Linked here; shared tags: #builtins
- tuts/python3/__builtins__/complex Linked here; shared tags: #builtins
- tuts/python3/__builtins__/float Linked here; shared tags: #builtins
- tuts/python3/__builtins__/int Linked here; shared tags: #builtins
- tuts/python3/__builtins__/str Linked here; shared tags: #builtins
- tuts/python3/tutorials/core_fundamentals/builtin_data_structures links to this page