Robotics & Artificial Intelligence
Predict the output of the following snippet:
for i in range(4):
for j in range(i, -1, -1):
print(j, end=' ')
print()
Python Control Flow
1 Like
Answer
Output
0
1 0
2 1 0
3 2 1 0
Explanation
The outer loop runs from 0 to 3. For each value of i, the inner loop starts from i and decreases up to 0 using the step value -1. Therefore, when i = 0, the output is 0; when i = 1, the output is 1 0; when i = 2, the output is 2 1 0; and when i = 3, the output is 3 2 1 0. The print() statement moves the output to the next line after each iteration of the outer loop.
Answered By
1 Like
Related Questions
Predict the output of the following snippet:
p = 100 for i in range(1, 5): p = p - 1 print(p, end=' ')Predict the output of the following snippet:
p = -99 while(p < 0): p = p + 9 print(p, end=' ')Predict the output of the following snippet:
for x in range(1, 4): for y in range(1, 3): print(x*y, end=' ') print()Assertion (A): Loop is a repetitive structure in which a statement or a set of statements are executed until the desired number of iterations is over.
Reason (R): The for loop is used to repeat the execution of a block of statements for a fixed number of times.
Based on the above assertion and reasoning, pick an appropriate statement from the options given below:
- Both A and R are true and R is the correct explanation of A.
- Both A and R are true and R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.
- Both A and R are false.