Tuples
A tuple is like a list — it stores multiple items in one place, in order. The big difference: you can’t change it after you create it. No adding, removing, or swapping items.
That makes tuples great for data that should stay fixed, like a coordinate on a screen or an RGB color value.
Creating a tuple
Put your items inside parentheses (), separated by commas:
point = (100, 200)
color = (255, 128, 0)
days = ("Monday", "Tuesday", "Wednesday")If your tuple has only one item, add a comma after it — otherwise Python just treats the parentheses as grouping:
single = (42,) # this is a tuple
not_a_tuple = (42) # this is just the number 42Tuples vs. lists
| List | Tuple | |
|---|---|---|
| Syntax | [1, 2, 3] | (1, 2, 3) |
| Can change items? | Yes | No |
| Good for | Collections that grow or change | Fixed groups of related values |
Use a tuple when the values belong together and shouldn’t change — like the (x, y) position of something on screen, or the (red, green, blue) values of a color.
Try it out
main.py
Output
Last updated on