Python Tutorial 0/52 lessons ~6 min read Lesson 18

    Tuples

    Tuples are immutable ordered sequences — perfect for fixed records, dict keys, and function return values.

    Course progress0%
    Focus
    8 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Tuples are immutable ordered sequences — perfect for fixed records, dict keys, and function return values.

    Understanding the topic

    Core concepts to understand:

    • (1, 2, 3) or 1, 2, 3.
    • Immutable → hashable → usable as dict keys.
    • Tuple unpacking: x, y = point.
    • NamedTuple for self-documenting tuples.

    Syntax reference

    Visual flow / code:

    python
    point = (3, 4)
    x, y = point # unpack
    # As dict key
    distances = {(0, 0): 0, (3, 4): 5}
    # Return multiple values
    def divmod_(a, b):
    return a // b, a % b
    q, r = divmod_(17, 5)
    # NamedTuple
    from typing import NamedTuple
    class Point(NamedTuple):
    x: int
    y: int
    p = Point(1, 2)
    print(p.x)

    Execution workflow

    1Tuples Workflow
    1 / 4

    (1, 2, 3) or 1,…

    (1, 2, 3) or 1, 2, 3.

    Real-world use

    NamedTuples (or dataclasses) replace plain tuples in modern Python — self-documenting, IDE-friendly, still immutable.

    Best practices

    • Use tuples for fixed-shape records.
    • Prefer NamedTuple/dataclass over raw tuples in APIs.
    • Tuple unpacking > index access.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionTuple vs list?+

    Answer

    Tuples are immutable fixed-size sequences, while lists are mutable and resizable. Use tuples for heterogeneous records, multiple return values, and as dict keys or set elements when their contents are hashable. Note that a tuple's immutability is shallow: a mutable object inside it can still be changed.

    Follow-up

    Can you modify a list that is inside a tuple?
    2IntermediateQuestionWhy are tuples hashable?+

    Answer

    Tuples are hashable because they are immutable, so their hash can stay stable, and the hash is computed from the hashes of their items. Your premise needs a small correction: a tuple is hashable only if all its elements are hashable, so hash((1, [2])) raises TypeError. Hashability is what lets tuples be dict keys.
    3IntermediateQuestionNamedTuple vs dataclass?+

    Answer

    NamedTuple (or collections.namedtuple) creates an immutable tuple subclass with named fields that still unpacks and indexes like a tuple. A dataclass is a regular mutable class by default with generated __init__, __repr__, and __eq__, and supports defaults, methods, and frozen=True for immutability. Choose NamedTuple for lightweight immutable records that must behave as tuples, and dataclass for richer data classes.

    Follow-up

    How do you make a dataclass hashable?

    Summary

    In summary: Immutable, hashable, fast. NamedTuple > raw tuple for clarity.

    Ready to mark this lesson complete?Track your journey across the entire course.