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
9. What will be the output of the following Python code?x = (i for i in range(3))for i in x: print(i)for i in x: print(i)
What will be the output of the following code?for i in range(1,5): if i == 3: continue else: print(i,end = " ")Options: Pick one correct answer from below1 2 3 41 2 41 2 3 4 51 2
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 of the following code snippet?for index in range(20, 10, -3) :print(index, 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()0 00 10 20 0 0 0 1 2Infinite loopError
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.