About 2156 letters
About 11 minutes
A tuple is an ordered collection of immutable values.
The literal syntax for a tuple uses parentheses ()
to wrap a group of values. For example:
students: tuple[str, str, str] = ("Tom", "Jerry", "Spike")
The type annotation tuple[str, str, str] indicates a tuple with three elements, each of type str
.
Tuple elements can be of different types. For example:
student_info: tuple[str, int, str] = ("Yukari", 17, "female")
You can access elements in a tuple using square brackets []
, just like in most programming languages.
Python uses zero-based indexing:
students: tuple[str, str, str] = ("Tom", "Jerry", "Spike")
print(students)
print(students[0])
print(students[1])
print(students[2])
Once a tuple is created, it cannot be changed — you can't add, remove, or modify any element. However, you can reassign the variable. This doesn't change the tuple — it simply assigns a new value to the variable:
students: tuple[str, str, str] = ("Tom", "Jerry", "Spike")
students = 10
But as mentioned earlier, it's recommended not to change the variable type.
You can create an empty tuple like this:
empty: tuple[()] = ()
To create a tuple with a single element, you need to include a trailing comma ,
to avoid ambiguity, since parentheses ()
have multiple uses in Python:
student: tuple[str] = ("Tom",)
Created in 5/15/2025
Updated in 5/21/2025