A function expects a sequence even when only one measurement is available.
What You Need to Understand
A single-element tuple needs a trailing comma, such as (7,). Parentheses alone simply group an expression; (7) is an integer.
When to Use It
A function expects a sequence even when only one measurement is available.
Step-by-Step Python Example
single_score = (88,)
not_a_tuple = (88)
print(type(single_score).__name__)
print(type(not_a_tuple).__name__)
print(len(single_score))
How the Example Works
The comma makes the first expression a tuple containing one integer; the second remains an integer.
Example Output
tuple
int
1
red
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
colors = ("red",)
print(colors[0])
Important Behavior and Edge Cases
Forgetting the comma is a frequent source of bugs in APIs that expect tuples. An empty tuple uses () with no comma.
Try It Yourself
Make a one-item tuple containing a product name and confirm its length.
Common Beginner Mistakes
- Forgetting the comma is a frequent source of bugs in APIs that expect tuples. An empty tuple uses
()with no comma. - Assuming every sequence operation modifies the original data or that a tuple makes nested mutable objects immutable.
Key Takeaways
- A single-element tuple needs a trailing comma, such as
(7,). - The comma makes the first expression a tuple containing one integer; the second remains an integer.
- Forgetting the comma is a frequent source of bugs in APIs that expect tuples.