Lesson 18 of 30

Lesson 18 ? Tuples and coordinates

Title: Store positions that should stay together

Description: Use tuples for simple coordinates, colors, and pairs of values.

Why it matters for kids: Coordinates help place characters on a map or screen.


1. First tuple

pythonpython
position = (3, 5)

print(position)
print(position[0])
print(position[1])

A tuple is like a list, but it is usually used for values that belong together.


2. Player position

pythonpython
player_position = (2, 4)
x = player_position[0]
y = player_position[1]

print(f"Player is at x={x}, y={y}.")

3. Unpack a tuple

pythonpython
home = (10, 20)
x, y = home

print(x)
print(y)

4. Color tuple

pythonpython
red_color = (255, 0, 0)

print("Red amount:", red_color[0])
print("Green amount:", red_color[1])
print("Blue amount:", red_color[2])

5. Easy examples

pythonpython
start = (0, 0)
finish = (5, 5)

print(start)
print(finish)
pythonpython
student = ("Mia", 9)
name, age = student

print(f"{name} is {age} years old.")
pythonpython
size = (800, 600)
width, height = size

print("Width:", width)
print("Height:", height)

6. Mini challenge

Click each example to open it.

Example 1

Create position = (0, 0).

Example 2

Print the x value.

Example 3

Print the y value.

Example 4

Unpack x, y from a tuple.

Example 5

Create a home coordinate.

Example 6

Create a treasure coordinate.

Example 7

Create a color tuple.

Example 8

Create a size tuple.

Example 9

Create a student tuple.

Example 10

Print a robot position sentence.