Python Tuple Packing and Singleton Tuples
Core idea
A tuple is an ordered sequence whose item references cannot be replaced. Pack several values into one tuple with commas. Parentheses make the grouping clear; the comma is what distinguishes a singleton tuple from an ordinary grouped expression. Use () for an empty tuple.
The two numbers form one tuple; indexing selects its first member.
One item and zero items
The trailing comma preserves the one-item tuple; parentheses alone do not.
Convert a list
tuple() consumes the list items in order.
Common mistake
Writing (value) only groups an expression. Writing (value,) creates a tuple, even if the value is itself a list.
Practice
Pack a room number and floor in a tuple; then create a singleton tuple containing only the room number.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Tuple packing
Check your understanding of tuple packing.
What is printed?
print((5,))What is printed?
print((8))What is printed?
print(tuple([3, 1, 3]))Which expression creates an empty tuple?