xSKILLby Infirals
Register

Python for Beginners

0 of 24 notes read 0%

Reading progress is saved to your INFIRALS account. Sign in to keep this note marked as read.

Tuples

Data structures4 min readLesson 1 of 3

A tuple is an ordered, immutable sequence. Once built, it cannot change.

python
point = (3, 5)
x, y = point          # unpacking
point[0]              # 3

Because it never changes, a tuple is hashable and can be a dictionary key or a set member. A list cannot.

python
seen = {(0, 0), (1, 2)}
locations = {"start": (0, 0), "end": (4, 7)}

Use a tuple for a fixed record (a point, an RGB colour, a row of coordinates) and a list for a collection that grows. Python also lets you return several values at once, which is just a tuple: