Python Program to Print a Number Pattern
Beginner · Patterns · CBSE class 11 practical list
This program prints a right-angled triangle of numbers in which each row counts from 1 up to the row number. Number patterns use the same nested loops as star patterns, but print the loop counter instead of a star.
Program
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
How it works
- The outer loop picks the row, with
igoing from 1 torows. - The inner loop prints the numbers 1 to
ion that row, separated by spaces withend=" ". - The empty
print()ends the row so the next one starts on a new line. - Printing
j, the inner counter, gives 1 2 3 on row 3, while printingiwould repeat the row number, like 3 3 3.
Other ways to write it
Inverted number triangle
1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
The outer loop counts down, so the first row is the longest: 1 2 3 4 5, then 1 2 3 4, and so on down to 1.
Floyd's triangle
1 2 3 4 5 6 7 8 9 10
A separate counter num keeps growing across the rows, so the numbers run 1, then 2 3, then 4 5 6, and so on.
Try it yourself
Change the inner print(j, end=" ") to print(i, end=" ") and see how the pattern changes.
Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.
Related programs
All Python programs · CBSE class 12 practical file · Python exercises