PCEP-30-02 · Question #55
How many elements does the my_list list contain? my_list = [0 for i in range(1, 3)]
The correct answer is C. two. Option C is correct because range(1, 3) generates the sequence [1, 2] - two values - and the list comprehension produces one 0 for each, resulting in [0, 0]. Option A (one) is wrong because there are two iterations, not one - the range starts at 1 and includes both 1 and 2…
Question
Options
- Aone
- Bthree
- Ctwo
How the community answered
(53 responses)- A13% (7)
- B6% (3)
- C81% (43)
Explanation
Option C is correct because range(1, 3) generates the sequence [1, 2] - two values - and the list comprehension produces one 0 for each, resulting in [0, 0].
Option A (one) is wrong because there are two iterations, not one - the range starts at 1 and includes both 1 and 2.
Option B (three) is the classic trap: range(1, 3) does not include the stop value 3, so it yields only 1 and 2, not 1, 2, and 3.
Memory tip: Think of range(start, stop) as a half-open interval [start, stop) - the stop value is always excluded. Counting elements is simply stop - start, so 3 - 1 = 2.
Community Discussion
No community discussion yet for this question.