Tuples: Keeping Related Values Together

A tuple is an ordered sequence of values whose element positions cannot be changed after creation. In Python, tuples help you keep a fixed group of related values together, such as a location's coordinates or a sensor's identifier and reading. You will use them when the meaning of each position is known and the group should stay structurally fixed.

Creating a tuple

Write a tuple by separating its elements with commas, usually inside parentheses. Elements can have different types: measurement = ("room A", 21.5) groups a room name with its temperature in degrees Celsius. Order matters because each position has a particular meaning.


The comma is especially important when you create a tuple containing one element. Parentheses alone can simply group an expression.


Python expression

Result

Number of tuple elements

()

Empty tuple

0

(8,)

Tuple containing one integer

1

(8, 12)

Tuple containing two integers

2

(8)

Integer, not a tuple

Not applicable



Tuples do not sort their elements. Python preserves the order you supply, even when the numbers are out of numerical order. You can store repeated values too: (5, 5) contains two separate positions.

Reading and unpacking values

An index selects one element. Python starts counting positions at zero, so readings[0] retrieves the first element and readings[1] retrieves the second.


For a tuple containing nn elements, a valid nonnegative index ii satisfies:


0≤i<n0 \le i < n


The function len(readings) returns the number of elements. Therefore, the final nonnegative index is len(readings) - 1. Python also supports negative indices: readings[-1] retrieves the last element, while readings[-2] retrieves the element immediately before it. An index outside the available positions raises an IndexError.


Three tuple elements appear in order beneath their Python indices.


Example: If readings = (14, 19, 23), then readings[1] is 19, readings[-1] is 23, and len(readings) is 3.


You can also use tuple unpacking to give each position a meaningful name:


Python
readings = (14, 19, 23)
morning, noon, evening = readings


Here, morning becomes 14, noon becomes 19, and evening becomes 23. With this simple unpacking form, the number of variable names must equal the number of elements.

Keeping tuple contents fixed

A tuple is immutable: after it is created, its elements cannot be replaced, added, or removed within that same tuple. For example, readings[0] = 16 raises a TypeError. You can still read an element and use it in a calculation.


Immutability describes the tuple itself. It does not prevent a variable from referring to a different tuple:


Python
point = (2, 7)
original = point
point = (4, 9)


After these assignments, point refers to (4, 9), while original still refers to (2, 7). The last line creates a new tuple and changes what point refers to; it does not edit the earlier tuple. Also, tuple immutability fixes its element references, rather than guaranteeing that every object stored inside it is itself immutable.



What to remember

  • Use commas to construct a tuple; a one-element tuple needs a trailing comma.
  • Positions begin at zero, and index -1 selects the last element.
  • Simple unpacking matches names to elements in order.
  • A tuple stays fixed, but a variable can refer to a new tuple.

Example: Checking a Room Temperature Record

A classroom logger stores morning, noon, and evening temperatures in degrees Celsius:


Python
temperatures = (18, 22, 20)


Use tuple unpacking, which assigns the elements to separate names in order, to work with this record. Assume each reading requires a calibration correction of +1 °C.


a) Unpack the readings and calculate how much warmer noon was than morning.


First, assign a name to each position:


Python
morning, noon, evening = temperatures


There are ________ elements and three names. For this simple form, the number of names must match the number of elements. The value assigned to morning is ________ °C, and the value assigned to noon is ________ °C.


Next, subtract the morning reading from the noon reading:


ΔT=22 ∘C−18 ∘C=4 ∘C\Delta T = 22\ ^\circ\text{C} - 18\ ^\circ\text{C} = 4\ ^\circ\text{C}


Noon was 4 °C warmer than morning.


b) Calculate the arithmetic mean of the three stored readings, to one decimal place.


First, add the readings. Their sum is ________ °C:


18 ∘C+22 ∘C+20 ∘C=60 ∘C18\ ^\circ\text{C} + 22\ ^\circ\text{C} + 20\ ^\circ\text{C} = 60\ ^\circ\text{C}


Next, divide by the number of readings:


T‾=60 ∘C3=20.0 ∘C\overline{T} = \frac{60\ ^\circ\text{C}}{3} = 20.0\ ^\circ\text{C}


The arithmetic mean is 20.0 °C. This is the mean of the three recorded observations; it does not assume they describe every temperature throughout the day.


c) Construct a tuple containing the corrected readings. Keep the original tuple available as temperatures.


First, calculate each corrected reading by adding 1 °C. The three calculations are 18+1=1918 + 1 = 19, 22+1=2322 + 1 = 23, and 20+1=2120 + 1 = 21, with all readings measured in degrees Celsius.


Next, construct a new tuple and assign it to a different variable:


Python
corrected = (morning + 1, noon + 1, evening + 1)


The corrected tuple is (19, 23, 21). The original temperatures tuple remains (18, 22, 20). Constructing a new tuple avoids attempting to replace elements inside the immutable original.

A greenhouse program must store a single temperature reading of 27 °C in a one-element Python tuple. Which assignment creates that tuple?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A school booking program stores a room name, the number of students, and whether the booking is confirmed in one Python tuple:


Python
booking = ("Lab B", 14, True)


Complete the description using the Python type names str, int, and bool. The room name has type , the student count has type , and the confirmation value has type .

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A temperature logger stores four readings, in degrees Celsius, using readings = (11, 17, 14, 20). Complete the table with exact integers. Reading rows require temperatures in degrees Celsius; the count row requires the number of elements.

Requested valueInteger answer
Reading at index 0
Reading at index 2
Reading at index -1
Number of elements

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice