Learning LibraryCore Coding LibraryTeens

Tuples in Python: fixed, unchangeable sequences

A tuple is like a list that can't change. That sounds like a limitation, but it's exactly what you want for things that come as a fixed set — an (x, y) point, an (r, g, b) color.

The big idea

A tuple is an ordered, immutable sequence written with parentheses: once created, its items can't be changed.

See it in code

1The basics

A tuple is a fixed sequence written with parentheses. A position is naturally a pair, so we store it as (120, 250) and index it like a list:

python
position = (120, 250)
print("x is", position[0])
print("y is", position[1])
Run it — the pair, reached by index:
x is 120
y is 250

position[0] and position[1] pull out each half — the same square brackets a list uses.

2A step further

Better than indexing: unpack the tuple into named variables in one line. x, y = position splits the pair straight into x and y:

python
position = (120, 250)
x, y = position
print("x =", x, "y =", y)
Run it — one line, both values named:
x = 120 y = 250

One clean line instead of position[0] and position[1]. Now let's unpack a position and use it to draw.

3In our world

Here the tuple (250, 250) is a star's position, and the colour is a tuple too. We unpack the position into x and y, then draw:

python
from art import Canvas, star

screen = Canvas.create()
Canvas.fill(screen)

position = (250, 250)
x, y = position
gold = (255, 205, 86)

star(screen, x, y, size=120, color=gold)
print("Unpacked:", x, y)
print("position is a", type(position).__name__)
Run it — the tuple unpacks straight into x and y:
Unpacked: 250 250
position is a tuple
A single large gold star centered on a dark navy canvas.

x, y = position is the same unpacking from the warm-up, now feeding star(). And because the tuple is immutable, position[0] = 300 would raise an error: a fixed pair stays fixed.

The same idea, everywhere

Tuples shine wherever a value is a fixed bundle: coordinates, colors, a function returning several results (return x, y), a date as (year, month, day), or a dictionary key (lists can't be keys, tuples can). Their immutability is a feature — it signals 'this shouldn't change' and makes the data safe to share.

Try it yourself

Return two values from a function — return x, y — and unpack them at the call site. Then try to reassign position[0] and read the TypeError that proves a tuple is immutable.

The common mistake

Trying to modify a tuple, or forgetting the comma for a single-item tuple. (5) is just the number 5 in parentheses; a one-element tuple needs the trailing comma: (5,). And to 'change' a tuple, you build a new one — you can't edit it in place.

What it unlocks

Tuples complement lists, package coordinates and RGB colors, and unpack cleanly in loops and returns.