PCEP-30-02 · Question #268
What is the result of the following code? ``python x = (3, ) print(len(x)) ``
The correct answer is C. 1. x = (3,) creates a tuple with a single element - the integer 3. The trailing comma inside the parentheses is what makes it a tuple, so len(x) returns 1, the count of its elements. Why the distractors are wrong: A (2): There's no basis for 2; the tuple contains only one item. B…
Question
x = (3, )
print(len(x))
Options
- A2
- BThe program will cause an error.
- C1
- D3
How the community answered
(35 responses)- A3% (1)
- B9% (3)
- C83% (29)
- D6% (2)
Explanation
x = (3,) creates a tuple with a single element - the integer 3. The trailing comma inside the parentheses is what makes it a tuple, so len(x) returns 1, the count of its elements.
Why the distractors are wrong:
- A (2): There's no basis for 2; the tuple contains only one item.
- B (error): This is perfectly valid Python syntax - no error occurs.
- D (3): This confuses the value of the element (
3) with the count of elements (1).
Memory tip: Think of len() as counting slots, not reading values. The comma is the real tuple-maker - (3) is just a parenthesized integer, but (3,) is a one-slot tuple.
Community Discussion
No community discussion yet for this question.