Why does `(5)` not make a one-item tuple in Python?
Quick answer
In Python, parentheses group an expression; they do not by themselves create a tuple. The comma is what makes a tuple, even when there is only one item: (5) is an integer, while (5,) is a one-item tuple. Check the difference with type((5)) is int and type((5,)) is tuple. The empty tuple is written ().[S1]
The question, the details, the next step.
A practical checklist
If the value should be a one-item tuple, write
(value,)with a trailing comma.[S1]Do not rely on parentheses alone to create a tuple;
(value)groups the expression.[S1]Use
type(value) is tuplein a small check when you need to verify the resulting type.Write the empty tuple as
().[S1]Check the receiving function's own requirements; tuple syntax alone does not show what that function accepts.
A few more useful answers
Is `(5,)` different from `(5)` only because of its type?
Yes: (5) evaluates to the integer 5, while (5,) evaluates to a one-item tuple containing 5.[S1] They are different kinds of values, not two spellings of the same tuple.
Can I write a one-item tuple without parentheses?
Yes. The comma creates the tuple, so 5, is a one-item tuple too.[S1] Parentheses can make the intended grouping clearer; (5,) is an explicit form.
How do I write a tuple with no items?
Use (). The empty tuple is a special case; a one-item tuple needs a trailing comma, as in (5,).[S1]
Sources
References used for this guide.
- [S1] Python Language Reference: Parenthesized forms and tuple displays docs.python.org · Checked
