Part of the series

Several example codes

~2 min read • Updated Oct 1, 2025

Program Overview

This Python program uses nested loops to generate an 8×8 number table.
In each row, the first few values increase from 2 up to the row index + 1.
The remaining cells are filled with the number 1.


Python Code:


# Number of rows and columns
n = 8

# Generate table using nested loops
for i in range(1, n + 1):
    for j in range(1, n + 1):
        if j <= i:
            print(j + 1, end=" ")
        else:
            print(1, end=" ")
    print()

Sample Output:


2 1 1 1 1 1 1 1  
2 2 1 1 1 1 1 1  
2 2 3 1 1 1 1 1  
2 2 3 4 1 1 1 1  
2 2 3 4 5 1 1 1  
2 2 3 4 5 6 1 1  
2 2 3 4 5 6 7 1  
2 2 3 4 5 6 7 8

Step-by-Step Explanation:

- The outer loop controls the rows (1 to 8)
- The inner loop controls the columns (1 to 8)
- If the column index is less than or equal to the row index, print j + 1
- Otherwise, print 1
- end=" " keeps the output on the same line
- print()


Written & researched by Dr. Shahin Siami