Select the correct output for the following Python code. for i in range(6, 1,…
2023
Select the correct output for the following Python code.
for i in range(6, 1, -2):
print(end='*$$')Answer: C. *$$*$$*$$ — ConceptIn Python, range(start, stop, step) includes start, excludes stop, and repeatedly adds step while values remain within the stopping condition. A print…
- A.
*$$*$$ - B.
*$$ *$$ *$$ - C.
*$$*$$*$$ - D.
*$$ *$$
Attempted by 2176 students.
Show answer & explanation
Correct answer: C
Concept
In Python, range(start, stop, step) includes start, excludes stop, and repeatedly adds step while values remain within the stopping condition.
A print call with no objects emits only its end string; supplying end also replaces the default newline.
Application
For
range(6, 1, -2), begin at 6 and subtract 2 each time. The generated values are6, 4, 2; the next value 0 is below the exclusive stop boundary, so there are three iterations.On each iteration,
print(end='*$$')has no object argument, so it emits exactly*$$through theendparameter.Because the
endstring contains neither a space nor a newline, the three emitted segments are adjacent:*$$*$$*$$.
Cross-check
A trace gives one emitted segment for each loop value:
Loop value | Emitted text |
|---|---|
|
|
|
|
|
|
Therefore, the output is *$$*$$*$$.