A program tracks a changing task queue and a fixed pair of geographic coordinates.
What You Need to Understand
A list is mutable: it supports insertions, deletions and reassignment. A tuple is immutable: its element references cannot be reassigned after creation. Choose based on whether the number and identity of values should change.
When to Use It
A program tracks a changing task queue and a fixed pair of geographic coordinates.
Step-by-Step Python Example
tasks = ["email", "meeting"]
tasks.append("review")
coordinates = (32.1, 34.8)
print(tasks)
print(coordinates[0])
How the Example Works
The task list gains a new entry. Coordinates remain a two-item tuple, useful when the pair is conceptually fixed.
Example Output
['email', 'meeting', 'review']
32.1
4 7
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
point = (4, 7)
print(point[0], point[1])
Important Behavior and Edge Cases
A tuple can contain a mutable list, so immutability of the tuple does not freeze everything nested inside it.
Try It Yourself
Model a fixed RGB color as a tuple and a growing collection of colors as a list.
Common Beginner Mistakes
- A tuple can contain a mutable list, so immutability of the tuple does not freeze everything nested inside it.
- Assuming every sequence operation modifies the original data or that a tuple makes nested mutable objects immutable.
Key Takeaways
- A list is mutable: it supports insertions, deletions and reassignment.
- The task list gains a new entry.
- A tuple can contain a mutable list, so immutability of the tuple does not freeze everything nested inside it.