Skip to main content

How to Create a Tuple in Python

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A tuple groups a fixed set of values, such as the x and y of a point, into one value. Unlike a list, it can't be changed after it's made, which makes it a safe way to pass such a group around. This guide creates a point, with real output from Python 3.15.2.

Syntax​

Syntax
<name> = (<value 1>, <value 2>, ...)
<name> = (<value>,) # a tuple with one item needs the comma
PartWhat it means
( ... )The parentheses group the values; the commas are what make the tuple
<value>, ...Any values, of any types, in a fixed order

The Python tutorial: "A tuple consists of a number of values separated by commas".

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

point.py
point = (3, 4)

print(point)
print(point[0])
print(len(point))

The point.py script in a macOS-style code window: point = (3, 4), then print(point), print(point[0]) and print(len(point)).

Step 2: Run It​

Terminal
uv run python point.py
Output
(3, 4)
3
2

The script's output in a terminal: (3, 4), 3 and 2.

The tuple prints in parentheses. Its items are read by position like a list's, and len counts them.

Common Mistakes​

MistakeWhat happensDo this instead
A one-item tuple without the comma(5) is just the number 5: type((5)) was <class 'int'>Add the comma: type((5,)) was <class 'tuple'>
Trying to change an itempoint[0] = 10 raised TypeError: 'tuple' object does not support item assignmentMake a new tuple, or use a list if the values must change

Troubleshooting​

SymptomLikely causeFix
TypeError: 'tuple' object does not support item assignmentTuples can't be changed after they're madeBuild a new tuple, such as (10, point[1])

FAQ​

When should I use a tuple instead of a list?​

When the values belong together and shouldn't change, such as a point, a date's parts or a pair of settings. Use a list for a collection that grows or changes.

Why does one item need a comma?​

The tutorial: "a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses)."

Conclusion​

point = (3, 4) created a tuple: it printed as (3, 4), point[0] read 3, and len(point) counted 2. Remember the comma for one item, and that a tuple can't be changed.

Additional Resources​