Meaning
Creates a tuple containing the given elements; tuples are immutable sequences often used for fixed collections of items.
Primary Function
Data structure creation
Communicative Purpose
Defines an immutable ordered collection of values.
Pattern
(element1, element2, element3)
Core Structure
(..., ..., ...)
Função primária
Data structure creation
Propósito comunicativo
Defines an immutable ordered collection of values.
Situações de gatilho
When you need to group related values that should not change, e.g., coordinates, RGB colors, returning multiple values from a function.
Contextos
General Python code, any script or library; common in function returns, configuration constants.
Padrão
(element1, element2, element3)
Estrutura central
(..., ..., ...)
Slots de substituição
element1: any, element2: any, element3: any
Colocados típicos
- tuple unpacking
- dict keys
- function returns
Substituições comuns
- list literal [1
- 2
- 3] for mutable sequences
- collections.namedtuple for structured data
Erros comuns
Attempting to modify tuple elements; forgetting trailing comma for singleton tuple; confusing with list syntax
Similar / contraste
list literal [1, 2, 3] (mutable, ordered); set literal {1, 2, 3} (unordered, unique); frozenset (immutable set)
Interferências
Coming from JavaScript: may overuse tuples where arrays are expected → use lists for mutable sequences; Coming from C: may confuse tuple with struct → tuples are immutable and accessed by index, while structs have named fields.
Família do chunk
- tuple unpacking
- tuple slicing
- tuple methods
Nuance
When NOT to use this: when mutability is needed; Performance implications: tuples have lower memory overhead than lists; Non-obvious boundary conditions: singleton tuple requires a trailing comma (x,), empty tuple is ().
Efeito pragmático
Provides lightweight immutable grouping; enables use as dict keys and set members.
Dica de memória
Imagine a carton of eggs: fixed number, ordered, cannot change.
Nota
None.
Upgrade path
Use collections.namedtuple or dataclasses for structured records with field names.
Log in to save chunks.