350-401 · Question #942
Refer to the exhibit. What is output by this code?
The correct answer is C. 0 1 2 3 4. The Python range(5) function generates a sequence of numbers starting from 0 up to, but not including, the specified argument, which are then printed by the loop.
Question
Refer to the exhibit. What is output by this code?
Options
- A0 5
- B0 1 2 3 4 5
- C0 1 2 3 4
- D(0,5)
How the community answered
(21 responses)- A5% (1)
- B10% (2)
- C71% (15)
- D14% (3)
Why each option
The Python `range(5)` function generates a sequence of numbers starting from 0 up to, but not including, the specified argument, which are then printed by the loop.
This output is incorrect because `range(5)` generates multiple values, not just 0 and 5.
This output is incorrect because `range(5)` excludes the stop value (5), so 5 itself is not printed.
The `range(5)` function generates the sequence `0, 1, 2, 3, 4`. The `for` loop iterates through these values, and the `print(i)` statement displays each number on a new line.
This output is incorrect because `range(5)` generates individual numbers, and the print statement displays them one by one, not as a tuple.
Concept tested: Python `range()` function and `for` loop
Source: https://docs.python.org/3/library/stdtypes.html#range
Topics
Community Discussion
No community discussion yet for this question.