Tuples
A tuple is written with parentheses. It keeps order like a list but cannot be changed after creation, which makes it good for fixed groups of values such as coordinates. A tuple with one item needs a trailing comma: (5,).
Example
point = (10, 20)
print(point[0], len(point))
x, y = point
print(x + y)
single = (5,)
print(type(single))
try:
point[0] = 99
except TypeError as error:
print("Error:", error)Result
10 2 30 <class 'tuple'> Error: 'tuple' object does not support item assignment
Exercise
Create a tuple with one item.
t = (5)
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.