KnowledgeBoat Logo

Computer Science

Write the output of the following:

for x in range(1, 6): 
    for y in range(1, x+1):
        print (x, ' ', y)

Python

Python Control Flow

5 Likes

Answer

1   1
2   1
2   2
3   1
3   2
3   3
4   1
4   2
4   3
4   4
5   1
5   2
5   3
5   4
5   5

Working

The code features two nested for loops. The outer loop iterates over values from 1 to 5 (inclusive) and assigns each to x. The inner loop iterates from 1 to the x + 1 value and assigns each to y. The print(x, ' ', y) statement outputs x and y separated by a space during each iteration. This arrangement generates combinations of (x, y) pairs.

Answered By

1 Like


Related Questions