What is the output of the following code? for i in range(3): print(i) else: print("Loop finished")
Question
What is the output of the following code?
for i in range(3):
print(i)
else:
print("Loop finished")
Solution
The output of the code will be:
0 1 2 Loop finished
Explanation:
The range(3)
function generates a sequence of numbers from 0 up to but not including 3. The for
loop then iterates over this sequence, and for each iteration, it prints the current number (i
).
After the for
loop has finished iterating over all items in the sequence (i.e., when i
has taken on all values in the range from 0 to 2), the code in the else
block is executed. This block simply prints the string "Loop finished".
Similar Questions
What will be the output of the following code?i = 3for i in range (4) print (i)
What will be printed by the following code?for i in range(5): for j in range(5, 0, -1): if i == j: print(i, end=" ")
What will be the output for the following code?i=0while(i<3): j=0 while(j<3): print(i,j,end=" ") j+=1 print()
What do these lines print?>>> for i in range(0, 3):>>> print(i, end=" ")1 2 30 1 2 30 1 2
What will be the output of the following code snippet?for index in range(20, 10, -3) :print(index, end = ' ')
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.