A drawing program stores mutable drawing steps but a fixed canvas dimension.
What You Need to Understand
Use lists when elements must be inserted, removed or replaced; use tuples for fixed structures or return values that are conceptually grouped. Both retain order and permit duplicates.
When to Use It
A drawing program stores mutable drawing steps but a fixed canvas dimension.
Step-by-Step Python Example
canvas_size = (800, 600)
drawing_steps = ["outline", "fill"]
drawing_steps.append("shadow")
print(canvas_size)
print(drawing_steps)
How the Example Works
Only the list is modified. The tuple documents that width and height belong together and should not be reassigned by position.
Example Output
(800, 600)
['outline', 'fill', 'shadow']
1280
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
sizes = [(640, 480), (1280, 720)]
print(sizes[1][0])
Important Behavior and Edge Cases
Tuples are not automatically hashable if they contain lists or other unhashable objects. Neither type automatically validates its contents.
Try It Yourself
Decide which type should represent a roster of players and which represents a two-dimensional point.
Common Beginner Mistakes
- Tuples are not automatically hashable if they contain lists or other unhashable objects. Neither type automatically validates its contents.
- Assuming every sequence operation modifies the original data or that a tuple makes nested mutable objects immutable.
Key Takeaways
- Use lists when elements must be inserted, removed or replaced; use tuples for fixed structures or return values that are conceptually grouped.
- Only the list is modified.
- Tuples are not automatically hashable if they contain lists or other unhashable objects.