nerdexam
Python_Institute

PCEP-30-02 · Question #17

How many elements does the L list contain? ``python 1 L = [i for i in range(-1, -2)] ``

The correct answer is D. zero. range(-1, -2) produces an empty sequence because the default step is +1, meaning it would try to count upward from -1 toward -2 - but -1 is already greater than -2, so there are no valid values to yield. The list comprehension iterates over nothing, leaving L = [] with zero…

Question

How many elements does the L list contain?
1 L = [i for i in range(-1, -2)]

Options

  • Aone
  • Btwo
  • Cthree
  • Dzero

How the community answered

(45 responses)
  • A
    2% (1)
  • B
    4% (2)
  • C
    11% (5)
  • D
    82% (37)

Explanation

range(-1, -2) produces an empty sequence because the default step is +1, meaning it would try to count upward from -1 toward -2 - but -1 is already greater than -2, so there are no valid values to yield. The list comprehension iterates over nothing, leaving L = [] with zero elements.

Why the distractors are wrong:

  • A (one): A common trap - you might assume -1 is included since it's the start, but range() only yields start when the sequence can actually progress toward stop.
  • B (two): There's no scenario where both -1 and -2 are included; range() never includes the stop value.
  • C (three): Completely unfounded - no values in [-1, -2) with step +1 exist.

Memory tip: Think of range(start, stop) as "count up from start as long as the value is still less than stop." If start >= stop with the default step, you get zero elements - the range is already "past the finish line" before it begins.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice