Skip to main content

Lesson 2 · Tuples

Module 4 · Manhattan Navigation

Lesson 2 · Tuples

50 minPython dataStoring a position
👩‍🏫

Teacher mode is on. Toggle it off (bottom-right) to preview the student view.

You can name any grid spot with (row, column). Now you'll store one in Python using a tuple — a single value that neatly packages two numbers. This is where Module 4 turns into code.

Learning Objectives

By the end of this lesson you will be able to:

  • Create a tuple to hold a (row, column) coordinate
  • Read its parts with position[0] (row) and position[1] (column)
  • Explain immutability and why it suits coordinates
  • Compare positions with == and compute a Manhattan distance in code

One value, two numbers

You could store a position in two variables — row = 2, col = 3 — but that gets messy fast (ten positions would need twenty variables). A tuple packs both into one value, written with parentheses:

position = (2, 3)
start = (0, 0)
destination = (3, 2)

Pull the pieces out by index — and since Python counts from zero (just like our grid), index 0 is the row and index 1 is the column:

position = (2, 3)
print(position[0]) # 2 → the row
print(position[1]) # 3 → the column
Knowledge Check

Given position = (3, 5), what does position[0] return?

Tuples don't change (and that's good)

Try to modify a tuple and Python stops you:

position = (2, 3)
position[0] = 5 # TypeError: 'tuple' object does not support item assignment

Tuples are immutable — once made, they can't be altered. That's exactly right for a coordinate: (2, 3) should always mean row 2, column 3. To represent a new spot, you make a new tuple:

old_position = (2, 3)
new_position = (2, 4) # one column right — a brand-new tuple

Think of a street address: it doesn't change. Move, and you get a new address — you don't edit the old one.

Knowledge Check

What happens when you run position[0] = 5 on a tuple?

Comparing positions

== checks whether two tuples are the same position — both parts must match, in order:

current = (1, 2)
target = (1, 2)
print(current == target) # True

other = (2, 1)
print(current == other) # False — order matters!

This matters later: the algorithm asks "have we reached the destination yet?" by comparing tuples.

Knowledge Check

Does (1, 4) == (4, 1) evaluate to True?

Activity · Manhattan distance in code

The paper counting from Lesson 1 becomes simple arithmetic on tuple parts:

start = (0, 0)
destination = (2, 3)

row_distance = destination[0] - start[0] # 2
col_distance = destination[1] - start[1] # 3
total_steps = row_distance + col_distance # 5
print("Total steps:", total_steps)
Knowledge Check

Using this method, what's the Manhattan distance from (1, 1) to (4, 3)?

Try it: store five of your grid positions as tuples and print each one's row and column; then compute a couple of distances.

Real-world connections

Bundling related values into one unit is everywhere in code:

Graphics

Points & colors

Screen points (x, y) and colors (r, g, b) are stored exactly like coordinate tuples.

Data

Records

A row of a dataset — (name, age, score) — is a fixed bundle, just like a coordinate.

Maps

Lat/long pairs

Every mapped location is a two-value pair passed around as one unit.

Wrap-up

  • How do you get the row and column out of a tuple? ([0] and [1].)
  • What does immutable mean, and why is it good for coordinates? (Can't change; a position should stay fixed.)
  • Why is (2, 3) == (3, 2) False? (Elements must match in order.)

Resources