KnowledgeBoat Logo
|

Robotics & Artificial Intelligence

Predict the output of the following snippet:

for x in range(1, 4):
    for y in range(1, 3):
        print(x*y, end=' ')
    print()

Python Control Flow

1 Like

Answer

Output
1 2 
2 4 
3 6 
Explanation

The outer loop runs with values 1, 2, and 3. For each value of x, the inner loop runs with values 1 and 2. During each iteration, the product of x*y is printed. Thus, when x = 1, the outputs are 1 and 2; when x = 2, the outputs are 2 and 4; and when x = 3, the outputs are 3 and 6. The print() statement moves the cursor to the next line after each row is completed.

Answered By

1 Like


Related Questions