Challenge - 5 Problems
Single-element Tuple Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of single-element tuple creation
What is the output of this Python code?
Python
t = (5) print(type(t))
Attempts:
2 left
๐ก Hint
Remember how Python treats parentheses without a comma.
โ Incorrect
Parentheses alone do not create a tuple. Without a comma, (5) is just the number 5 in parentheses, so its type is int.
โ Predict Output
intermediate2:00remaining
Correct way to create a single-element tuple
What is the output of this code?
Python
t = (5,) print(type(t))
Attempts:
2 left
๐ก Hint
Look for the comma inside the parentheses.
โ Incorrect
A single-element tuple requires a trailing comma. (5,) is a tuple with one element.
โ Predict Output
advanced2:00remaining
Output of tuple unpacking with single-element tuple
What is the output of this code?
Python
t = (42,) x, = t print(x)
Attempts:
2 left
๐ก Hint
Unpacking a single-element tuple assigns the element to the variable.
โ Incorrect
The comma after x means unpacking expects one element. x gets 42, so print(x) outputs 42.
โ Predict Output
advanced2:00remaining
Difference between parentheses and tuple with one element
What is the output of this code?
Python
print(type((7))) print(type((7,)))
Attempts:
2 left
๐ก Hint
Check the comma in the second print statement.
โ Incorrect
Without a comma, (7) is just 7, an int. With a comma, (7,) is a tuple.
๐ง Conceptual
expert2:00remaining
Why is the comma required for single-element tuples?
Why does Python require a comma to create a single-element tuple like (5,)?
Attempts:
2 left
๐ก Hint
Think about how parentheses work in math expressions.
โ Incorrect
Parentheses group expressions, so (5) is just 5 grouped. The comma tells Python to make a tuple.