Data Structures · Unit 3

Tuples

Ordered, unchangeable collections

Unit 3 · Data Structures

Choose the right container for your data

A data structure organizes values so a program can find and work with them. Explore four built-in Python structures and use each lab to see how its rules affect real data.

Lists

Ordered and changeable

Tuples

Ordered and fixed

Sets

Unique values

Dictionaries

Key → value lookup

Ordered · not changeable · duplicates allowed

Tuples: a fixed record of values

A tuple stores a sequence like a list, but its items cannot be reassigned, added, or removed after creation. Use parentheses for clarity; a one-item tuple needs a trailing comma, such as (5,).

Think of a printed result slip: its recorded marks stay fixed. You can read a position or copy its values into new names, but the original tuple remains unchanged.

Tuple index explorer

Select a position to retrieve its value. The tuple has four marks and cannot be edited in this lab.

index 091
index 182
index 276
index 388

marks[0] returns 91.

marks[0]Read by position.

marks[:2]Slice the first two values.

a, b = (1, 2)Unpack values into names.

Read and unpack a tuple
Starting up Python…
marks = [91,82,76,88]
print("Selected item:", marks[0])
print("First two:", marks[:2])
first, second, *remaining = marks
print("Unpacked:", first, second, remaining)
The editor is ready while Python loads in the background.

Output

Your program's output will appear here.
Common Python mistakes

NameError: name 'mark' is not defined

NameError: A name was used before Python could find a variable or function with that name.

IndexError: list index out of range

IndexError: The requested position is outside the items in this list or sequence.

KeyError: 'email'

KeyError: That key does not exist in this dictionary.

TypeError: can only concatenate str (not "int") to str

TypeError: This operation does not work with the kind of value it received.

ZeroDivisionError: division by zero

ZeroDivisionError: A number cannot be divided by zero.

SyntaxError: invalid syntax

SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.

IndentationError: expected an indented block

IndentationError: The spaces at the start of a line do not match Python's block structure.

Avoid common tuple mistakes

  • Parentheses alone do not make a one-item tuple: write ("OS",), including the comma.
  • Tuples are immutable: assigning marks[0] = 100 raises a TypeError. Create a new tuple if the values must change.
  • Unpacking needs matching values: each target name needs a corresponding value unless extended unpacking is used.
Tuples check

1. How do you write a tuple containing only the number 5?

2. Can you replace an item in an existing tuple?

Compare the four structures

Ask two questions: does order matter, and should the collection change after creation?

StructureOrdered?Changeable?Access values by
listYesYesPosition (index)
tupleYesNoPosition (index)
setNo guaranteed orderYesMembership, not index
dictInsertion order is preservedYesKey

Use a list for a sequence that changes, a tuple for a fixed record, a set for unique membership, and a dictionary for named lookups.