Tuples
Literal syntax, and how it differs from a list
A tuple looks almost like a list, but uses parentheses instead of brackets — and, crucially, it’s immutable:
This is the whole point of a tuple: once created, it can’t be changed. If you’re coming from Java, there’s no exact built-in equivalent pre-records (Java 16+); it’s closer to a fixed, unmodifiable array. C#’s tuple type is a much closer match syntactically.
Why use a tuple instead of a list
Immutability is a signal, not just a restriction — a tuple says “this is a fixed, small collection of related values that won’t change,” while a list says “this is a growable collection.” A coordinate pair, an RGB color, a (name, age) record — these are naturally tuples, because there’s no reason to append a fourth value to a coordinate.
rgb = (255, 0, 0) # a color has exactly 3 parts, always
tools = ["calculator", "search"] # a list of tools can growUnpacking
You can assign a tuple’s values directly to multiple variables in one line:
This also explains something you may not have thought twice about: when a
Python function “returns multiple values” with return a, b, c, it’s
actually returning a single tuple — the comma-separated values on the right
of return get packed into one automatically, and a, b, c = some_function()
unpacks them back out on the receiving end.
What happens when you try to run point[0] = 10 on a tuple?
For each point, unpack it into x, y, and calculate its distance from target_x as abs(x - target_x). Keep track of the point with the smallest distance seen so far as you loop through the list, and return it at the end. (Composes tuple unpacking with a running comparison, no sorting needed.)