Print Number Pattern in Python

Print a basic right-angled triangle number pattern using nested loops.

BeginnerLoop ProgramsExample 16 of 25
print-number-pattern.py
1# Program to print a right-angled number triangle
2
3rows = int(input("Enter number of rows: "))
4
5for i in range(1, rows + 1):
6 for j in range(1, i + 1):
7 print(j, end=" ")
8 print()

Output

Enter number of rows: 4
1 
1 2 
1 2 3 
1 2 3 4 

What's going on

We use nested loops: the outer loop controls rows and the inner loop prints numbers from 1 up to the row index.

After this program

If the idea is still fuzzy, read Day 9: Loops and Iteration in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 loop programs, or see every topic on Python exercises.