1.2.23 · D1Introduction to Programming (Python)

Foundations — Tuples — immutability, use cases

1,748 words8 min readBack to topic

Before you can understand that sentence, you need to understand what a box, a name, a position, and "glued shut" actually are. This page builds each one from nothing, in the order they depend on each other. Never skip ahead — every idea here is used by the next.


Symbol 0 — a value

Picture one thing sitting on a table. That's a value. It has no name yet — it's just there.

The parent page mixes many values together ((255, 128, 0)), so we must first be crystal-clear on what one of them is.


Symbol 1 — a name and the = sign

When you write:

point = (3, 4)

the = is not the "equals" from maths. In maths = asks a question ("are these the same?"). In Python = gives a command: "take the thing on the right, and make the name on the left point at it."

Figure — Tuples — immutability, use cases

Why does the parent topic need this? Because immutability is a rule about the value (the box), not the name (the tag). Later we'll rebind the tag freely while the box stays frozen — you can't see why that isn't a contradiction unless you already separate tag from box.


Symbol 2 — ordered and the index

An ordered collection means each item has a fixed position, and positions are numbered starting from 0.

Figure — Tuples — immutability, use cases

Look at the figure: three boxes in a row, tagged 0, 1, 2 underneath (never 1, 2, 3). The little [ ] after a name is the "reach into position" tool.

Why does the topic need this? "Ordered" and "index" are how the parent says point[0] is 3. Without the from-zero picture, every worked example mis-reads.


Symbol 3 — parentheses () vs the comma ,

The parent page's biggest trap: what actually builds a tuple?

a = (3)     # NO comma → just the number 3, parens only "hug" it
b = (3,)    # comma → a tuple with one item
c = 3, 4    # comma, NO parens → still a tuple (3, 4)

Picture the comma as glue between boxes and the parentheses as a cardboard tray you may set the glued row into. Glue is what makes it "one thing"; the tray is optional packaging.

Why the topic needs this: the one-element tuple (42,) and the swap trick a, b = b, a are pure comma-magic. If you think parentheses do the work, both look like nonsense.


Symbol 4 — mutable vs immutable

Now the heart of it.

Figure — Tuples — immutability, use cases

Look at the figure — two objects side by side:

  • List (left, open box): you can slide items in and out. Mutable.
  • Tuple (right, sealed & taped): you may peek through the window (read), but the tape stops any edit. Immutable.

Symbol 5 — a hash

The parent page claims tuples can be dictionary keys "because they're hashable." Here is what that word means, from zero.

Picture handing a librarian a book; she instantly reads a shelf-number off its cover and files it. To find it later she reads the same cover, gets the same shelf-number, and walks straight to it.

This is exactly the parent's chain "immutable → hashable → valid dict key." You now hold every link. Details live in Hashing in Python, used by Dictionaries — keys and values and Sets — hashing & uniqueness.


Symbol 6 — packing and unpacking

Figure — Tuples — immutability, use cases

The figure shows it both ways: three loose values squeeze into one tuple (pack, arrows inward), then that tuple bursts back out onto three names (unpack, arrows outward). The count must match: 3 items → 3 names.

Why the topic needs it: return min(nums), max(nums) packs, and lo, hi = min_max(...) unpacks. The swap a, b = b, a packs the right side into a temporary tuple, then unpacks it — that's the whole trick. See Functions — return values.


How these feed the topic

value: one piece of data

name and equals: a tag pointing at a value

ordered and index: fixed positions from zero

comma vs parentheses: comma builds the tuple

immutable: value frozen after creation

hash: stable fingerprint of a frozen value

packing and unpacking

TUPLE topic

Read it top-down: values need names; names plus ordering give indexing; the comma plus freezing give immutability; immutability enables hashing; and packing/unpacking is how tuples flow in and out of functions. All arrows land on the topic.


Equipment checklist

Test yourself — cover the right side, answer, then reveal.

In Python, does = ask a question or give a command?
A command: "make the left name refer to the right value."
Is a name a container that holds a value, or a tag pointing at one?
A tag pointing at the value — the value lives elsewhere in memory.
What index does the FIRST item of a tuple have?
0 — positions count from zero.
For an -item tuple, what index is the LAST item?
.
What actually builds a tuple — the parentheses or the comma?
The comma; parentheses are optional grouping.
Write a one-element tuple containing 42.
(42,) — the trailing comma is mandatory.
Define "immutable" in one sentence.
Frozen after creation — its insides can never be edited; you must build a new object to "change" it.
Does t = t + (9,) modify the old tuple?
No — it builds a NEW tuple and re-aims the tag t. The old value is untouched.
What is a hash, in plain words?
A stable fingerprint number computed from a value; same value → same fingerprint every time.
Why must a dictionary key be immutable?
So its hash (fingerprint) never changes, or the dict would look on the wrong shelf and lose the entry.
What is "unpacking"?
Spreading a tuple's items out onto several names at once, e.g. x, y = t.

Connections

  • Lists — mutability, methods — the mutable box these foundations contrast against.
  • Mutable vs Immutable objects — Symbol 4 is the seed of this whole idea.
  • Hashing in Python — Symbol 5 in full detail.
  • Dictionaries — keys and values — where hashable tuples pay off.
  • Sets — hashing & uniqueness — same hashing requirement.
  • Functions — return values — packing/unpacking in action.
  • Yeh foundations Hinglish mein →