Skip to content
ComputerCS
Start learning

Python Programming · Lesson 2.6.2

Tuples

Fixed collections that cannot change after creation — packing values together, unpacking them back, and when a tuple beats a list.

01

Learning objectives

  • State the difference between a tuple and a list in terms of mutability.
  • Pack several values into a tuple and unpack them into separate variables.
  • Use tuple assignment to swap two variables without a temporary variable.
  • Predict the TypeError raised when a tuple element is reassigned.
  • Choose sensibly between a tuple and a list for a given situation and justify the choice.
02

Lesson notes

A tuple is a sequence like a list, written with round brackets: point = (3, 4). It stores elements in order and supports indexing and slicing exactly like a list. The one difference is huge: tuples are immutable — once created, you can never change, add or remove an element. Try point[0] = 9 and Python raises TypeError: 'tuple' object does not support item assignment. Read that message carefully; it appears in MCQs almost word for word. Everything you already know about reading lists transfers to tuples unchanged.

Why does Python need an unchangeable sequence? Fixed records: a coordinate pair, an RGB colour, a roll-number–name–marks row. When a value must not drift — a date of birth, an answer key — a tuple guarantees it. Immutability also lets tuples be used as dictionary keys, which lists cannot. In team projects, handing a function a tuple is a promise: you may read this, but not edit it. That promise prevents a whole family of accidental bugs.

Packing writes several values into one tuple: position = 25, 40 packs even without brackets. Unpacking pulls them back out into separate variables: x, y = position — the right side must supply exactly as many values as there are names, or Python raises ValueError. The star of the show is the swap: a, b = b, a exchanges two variables in one line with no temporary variable, because the right side is fully evaluated before any assignment happens.

One-element tuples need a trailing comma: single = (5,) is a tuple of length 1, while (5) is just the number 5 in brackets. Print single and Python confirms it with (5,). Forgetting that comma is a classic quiz question. Also remember that len(), indexing and in all work on tuples, so most list-reading code runs on tuples without edits — only the changing operations are missing.

Choosing rule of thumb for the board exam: use a list for a collection that grows or gets edited (marks that update, a to-do queue); use a tuple for a fixed record whose parts belong together (coordinates, a date, two answers returned together). If a question asks which structure and why, state the mutability difference first, then give the reason — that two-step answer earns full marks.

03

Worked code examples

Packing and unpacking

The tuple packs two values; unpacking copies them into x and y. Indexing and len() work exactly as they do on lists.

point = (3, 4)
x, y = point
print(x + y)
print(len(point), point[0])
Output
7
2 3

The one-line swap

The right side (b, a) is built first and then assigned, so the swap needs no temporary variable. Unpacking must match the value count exactly.

a, b = 10, 20
a, b = b, a
print(a, b)

position = (25, 40, 55)
rolls, marks, rank = position
print(marks, rank)
Output
20 10
40 55

The one-element tuple

Without the comma the brackets are plain grouping — (5) is the int 5. The trailing comma is what makes it a tuple.

single = (5,)
print(single, len(single))
not_a_tuple = (5)
print(not_a_tuple)
Output
(5,) 1
5
04

Key terminology

tuple

An ordered, immutable sequence written with parentheses, like (3, 4).

immutable

Cannot be changed after creation; tuple elements can never be reassigned.

packing

Collecting several values into one tuple, e.g. position = 25, 40.

unpacking

Splitting a tuple into separate named variables, e.g. x, y = position.

swap

Exchanging two variables' values in one step with a, b = b, a.

05

Common misconceptions

Common mistake

Tuples are just lists written with ( ) instead of [ ].

Correct understanding

The brackets differ, but the real difference is immutability: a tuple can never be edited, appended to or shrunk after creation.

Common mistake

(5) creates a tuple containing 5.

Correct understanding

(5) is just the number 5 — brackets alone group. The tuple needs a trailing comma: (5,).

Common mistake

x, y = point works as long as point has at least two elements.

Correct understanding

The counts must match exactly — three values into two names raises ValueError: too many values to unpack.

06

Check your understanding

1. point = (3, 4). What happens when the program runs point[0] = 9?

2. After a, b = 1, 2 and then a, b = b, a + b, what are a and b?

3. Which data structure fits a fixed (latitude, longitude) coordinate best, and why?

NERDLYY · FEEDBACK

Help shape Nerdlyy

Your feedback goes directly to the team. We read every submission.

No reply needed — you can send feedback without sharing contact details.