Python Tuples
A tuple is a fixed group. seat = ("lida", "tea") holds the name lida, then tea. The parentheses and the comma belong to the writing. Square brackets would make a list. This one is not a list.
You read a place the way you read one in a list. seat[0] is lida. seat[1] is tea. Counting still starts at 0.
seat = ("lida", "tea")
print(seat[0])
print(seat[1])
The screen shows lida, then tea. The group was not reordered. lida is first because that is how you wrote it.
len(seat) is 2. The last place is 1, one less than the length. print(seat) writes both values and the parentheses around them. Place 1 is tea, and this second program shows that by writing the whole group.
seat = ("lida", "tea")
print(len(seat))
print(seat)
The screen shows 2, then lida and tea inside parentheses. tea has not moved. It is still in place 1.
seat[0] = "guest" stops the program. A place in this group does not take a new value. There is no append that adds at the end, and there is no sort that reorders the group in place. Those lines are not in the programs above. seat[2] also stops, because place 2 is not there.
("tea",) is a group of one value. The comma before the closing parenthesis is what makes the group. ("tea") without that comma is only the text tea. The one-value group is not in the programs above.
📊 Fixed, or a list
| Writing | What you get |
|---|---|
| ("lida", "tea") | a fixed group, lida then tea |
| ["lida", "tea"] | a list, and a place in it can be replaced |
| seat[0] = "guest" | the program stops |
The next page is a group that does not keep a repeated value. This page keeps the order you wrote. The same value could sit twice, and nothing here drops the second copy.
What the pair shows
- What does the first program write?
- What does the second program write first, and what sits in place 1?
- What happens if you write seat[0] = "guest"?
- What is ("tea") when the comma is missing?
lida stays first
The first screen is lida, then tea. The second screen starts with 2, and place 1 is tea. seat[0] = "guest" stops. ("tea") without a comma is the text tea, not a group. The next page drops a repeated value. This page keeps the order you wrote.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top