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
    Foundation builder

    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

    Step 1

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

    Apply this step while implementing tuples in real code.

    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.

    Hands-on exercise

    Interview preparation — practice these questions:

    • Tuple vs list?
    • Why are tuples hashable?
    • NamedTuple vs dataclass?

    Summary

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

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