Collections

Tuples

Tuples group a fixed number of positional values.

Use a tuple for a fixed-size record where each position has a known meaning. Unpacking turns those positions into names at the point of use.

Source

point = (3, 4)
x, y = point
print(x + y)

Output

7
FROZEN SEQUENCE(3, 1, 4, 1).append
Tuples are ordered, immutable sequences; positions matter, contents do not change once constructed.

Tuples are sequences, so indexing and len() work. They are different from lists because their length and item references are fixed after creation.

Source

red = (255, 0, 0)
print(red[0])
print(len(red))

Output

255
3

Tuples pair naturally with multiple return values and unpacking. If the fields need names everywhere, graduate to a dataclass or named tuple.

Source

record = ("Ada", 10)
name, score = record
print(f"{name}: {score}")

Output

Ada: 10

Lists and tuples carry different intent. A list holds a variable number of similar items and grows with append; a tuple has a fixed shape where each position has its own meaning, and unpacking gives those positions names.

Source

scores = [10, 9, 8]
scores.append(7)
print(scores)

student = ("Ada", 2024, "math")
name, year, subject = student
print(name, year, subject)

Output

[10, 9, 8, 7]
Ada 2024 math

Notes

See also

Run the complete example

Example code

Expected output

7
255
3
Ada: 10
[10, 9, 8, 7]
Ada 2024 math

Execution time appears here after you run the example.