0
0
Pythonprogramming~5 mins

Single-element tuple in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a single-element tuple in Python?
A single-element tuple is a tuple that contains exactly one item. It must have a comma after the item to be recognized as a tuple.
Click to reveal answer
beginner
Why do we need a comma after the single element in a tuple?
The comma tells Python that this is a tuple. Without the comma, Python treats the parentheses as just grouping, not a tuple.
Click to reveal answer
beginner
Which of these is a single-element tuple?
(5) or (5,)?
(5,) is a single-element tuple. (5) is just the number 5 inside parentheses.
Click to reveal answer
beginner
How do you create a single-element tuple with the string 'hello'?
You write ('hello',) with a comma after the string inside parentheses.
Click to reveal answer
beginner
What happens if you forget the comma in a single-element tuple?
Python will not create a tuple. Instead, it will treat the value as the type of the single element itself.
Click to reveal answer
Which of the following is a valid single-element tuple?
A[42]
B(42,)
C(42)
D{42}
What does Python interpret (7) as?
AA tuple with one element 7
BAn error
CThe number 7
DA list with one element 7
How do you create a single-element tuple with the boolean value True?
A(True)
B{True}
C[True]
D(True,)
What is the type of ("apple",)?
Atuple
Bstring
Clist
Ddict
Why is the comma important in single-element tuples?
AIt tells Python to treat the value as a tuple
BIt separates elements
CIt is optional
DIt makes the tuple immutable
Explain how to create a single-element tuple and why the comma is necessary.
Think about how Python reads parentheses and commas.
You got /3 concepts.
    What happens if you write (5) instead of (5,)? How does Python interpret each?
    Consider the difference between grouping and tuple creation.
    You got /3 concepts.