We’ve talked about lists, and how they’re a great way to store a bunch of things in order. Now, let’s meet the list’s more stubborn sibling: the tuple.
A tuple looks a lot like a list, and acts a lot like a list… except for one very important thing: once you create a tuple, you cannot change it. No adding, no removing, no swapping out an item. It’s locked, permanently, the moment you create it. Programmers call this immutable, which is just a fancy word for "cannot be changed once made."
Why would you ever want that? Well, imagine you’ve got some data that should never accidentally get messed with later on in your program - like a person’s date of birth, or the (x, y) coordinates of a spot on a map. A tuple is Python’s way of saying "this data is locked in, don’t touch it."
You create a tuple with parentheses instead of the square brackets you’d use for a list.
coordinates = (10, 20)
katniss_info = ("Katniss", "Everdeen", 12)Just like lists, tuples can hold any mix of data types - strings, numbers, booleans, even other tuples or lists.
|
Note
|
If you want a tuple with just one item in it, you need a trailing comma, like (100,). Without the comma, Python thinks you just wrote the number 100 inside some plain parentheses, not a tuple at all!
|
not_a_tuple = (100) # this is just the number 100
single_tuple = (100,) # this IS a tuple with one itemReading from a tuple works exactly like reading from a list. Tuples are indexed starting at zero, just like everything else we’ve indexed so far.
katniss_info = ("Katniss", "Everdeen", 12)
print(katniss_info[0]) # "Katniss"
print(katniss_info[2]) # 12You can slice a tuple too, the same way you’d slice a list or a string.
weekday_scores = (88, 92, 79, 95, 100)
print(weekday_scores[1:3]) # (92, 79)
print(weekday_scores[-1]) # 100Let’s prove it. If you try to change an item in a tuple, Python will stop you cold with an error.
katniss_info = ("Katniss", "Everdeen", 12)
katniss_info[2] = 13
# TypeError: 'tuple' object does not support item assignmentCompare that to a list, where this same kind of line would work just fine. This is the whole reason tuples exist: sometimes you want that safety net, so nothing later in your program can accidentally mangle your data.
One of the nicest tricks with tuples is unpacking - pulling the items out into their own separate variables, all in one line.
katniss_info = ("Katniss", "Everdeen", 12)
first_name, last_name, home_district = katniss_info
print(first_name) # "Katniss"
print(last_name) # "Everdeen"
print(home_district) # 12This works because Python matches up each variable on the left with the item in the same position on the right. It’s a very clean way to hand out a tuple’s contents without writing katniss_info[0], katniss_info[1], and so on.
You’ll also see this used a lot with coordinates:
location = (35, 80)
x, y = location
print(x) # 35
print(y) # 80So when do you reach for a tuple instead of a list? Here’s a simple way to think about it:
-
Use a list when you have a collection of things that might change - growing, shrinking, or getting swapped around.
-
Use a tuple when you have a fixed little bundle of data that should stay exactly as it is - like a coordinate pair, a date, or a record you’re just reading from, never editing.
player_health_scores = [100, 100, 100, 100, 100] # this will change during the game
spawn_point = (0, 0) # this should never changeTuples aren’t as flashy as lists, but they earn their keep. Whenever you find yourself thinking "this data should never change," reach for a tuple, and let Python enforce that promise for you.
Keep tuples in mind as you move on - they’re about to make a surprise appearance in the next chapter, tucked inside one of Python’s more flexible function tricks.