10 Number Pattern In Python
In this tutorial, we are going to discuss different number pattern in python. You can use the programs below in your python programs as well.
Number Pattern
Number pattern is a pattern created by numbers of any geometrical shape using controlled loops like squares, triangles, rectangles, etc.
Let us see some examples of number patterns in python.
The above image shows some of the number patterns that you can create in python. Apart from these, there are many more number patterns that you can think of and create.
Print numbers from 0 to 9
Before we start creating number patterns, let us first see how to print numbers from 0 to 9.
To do this you can simply use for loop to loop through the numbers from 0 to 9 and print them.
# print 0 to 9
for i in range(10):
print(i, end=' ')
Output:
0 1 2 3 4 5 6 7 8 9
Let's now create different number patterns.
1. Square Patterns
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
The square pattern is very simple to create using python. You need to use 2 nested loops to create a square pattern.
The internal loop will print the number of times you want to print the number. The outer loop will execute the internal loop for the number of times you want.
# square pattern
for i in range(5):
for j in range(5):
print(j+1, end=' ')
print() # new line
Output:
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
2. Left Triangle Alphabet Pattern
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
The left triangle pattern is a pattern in the shape of a triangle created using numbers.
The program for this will be 2 nested loops where the inner loop will print the number of times the outer loop is executed and print the number in every iteration.
# Left triangle pattern
n = 5
for i in range(n+1):
for j in range(1, i+1):
print(j, end=' ')
print()
Output:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
You can create variations in this pattern by creating 1 pattern that changes every next number in a row or another pattern that changes every next number in a column.
# Left triangle pattern
n = 5
for i in range(n+1):
for j in range(1, i+1):
print(i, end=' ')
print()
Output:
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
3. Right triangle Pattern
1 12 123 1234 12345
You can see above how the right triangle number pattern looks like.
The pattern starts with a bunch of spaces and then the number increases in every iteration.
To create this pattern, you can use 2 internal loops where the first loop will print spaces and the second loop will print the number. The outer loop will execute the inner loop for the number of times you want.
# right triangle pattern
size = 5
for i in range(size):
for j in range(1, size - i):
print(" ", end="")
for k in range(1, i + 2):
print(k, end='')
print()
Output:
1 12 123 1234 12345
4. Hollow triangle alphabet Pattern
1 12 1 2 1 2 1 2 123456
You can see above how the hollow triangle pattern looks. It is a bit complex to create because of the spaces within the pattern.
To create this keep a few things in mind, print only numbers in the first and last row, and in other rows print numbers only at the first and last position of the row and rest print spaces.
# hollow triangle number pattern
n = 6
for i in range(1, n+1):
count = 1
for j in range(i):
# print numbers only at the start and end of the row
if j == 0 or j == i-1:
print(count, end='')
count += 1
# print only numbers if it's last row
else:
if i != n:
print(' ', end='')
else:
print(count, end='')
count += 1
print()
Output:
1 12 1 2 1 2 1 2 123456
5. Number Pyramid Pattern
1 123 12345 1234567 123456789
The pyramid pattern is a very famous pattern you can create it using numbers too.
Every line has an odd number of numbers, the first line has 1 number, the second line has 2 numbers, the third line has 3 numbers, and so on.
The program will have 2 internal loops where the first loop will print spaces and the second loop will print the 2n + 1 increasing numbers.
# pyramid number pattern
n = 5
for i in range(n):
for j in range(n - i - 1):
print(' ', end='')
for k in range(2 * i + 1):
print(k + 1, end='')
print()
Output:
1 123 12345 1234567 123456789
6. Hollow Number Pyramid Pattern
1 1 2 1 2 1 2 123456789
The hollow number pyramid pattern is a little bit tricky just as the hollow triangle pattern.
This is the same as the hollow triangle pattern except it also has spaces at starting of the row.
# hollow pyramid number pattern
n = 5
for i in range(n):
# printing spaces
for j in range(n - i - 1):
print(' ', end='')
# printing number
count = 1
for k in range(2 * i + 1):
# print number at start and end of the row
if k == 0 or k == 2 * i:
print(count, end='')
count += 1
else:
if i == n - 1:
print(count, end='')
count += 1
else:
print(' ', end='')
print()
Output:
1 1 2 1 2 1 2 123456789
7. Reverse Number Pyramid Pattern
123456789 1234567 12345 123 1
You can see above reverse number pyramid pattern is equivalent to a number pyramid pattern but upside down.
This is may look not easy but it is very simple to create. Here is the program for this pattern.
# reverse pyramid pattern
n = 5
for i in range(n):
# printing spaces
for j in range(i):
print(' ', end='')
# printing alphabet
for j in range(2*(n-i)-1):
print(j+1, end='')
print()
Output:
123456789 1234567 12345 123 1
8. Number Diamond Pattern
1 123 12345 1234567 123456789 1234567 12345 123 1
This is a number diamond pattern. Try observing it closely you will find 2 figures in the pattern, one is the number pyramid and the other is the reverse number pyramid.
So you have to create a program that prints a number pyramid and reverses the number pyramid pattern back to back.
Let's see the complete program for this pattern.
# diamond number pattern
n = 5
# upward pyramid
for i in range(n):
for j in range(n - i - 1):
print(' ', end='')
for j in range(2 * i + 1):
print(j+1, end='')
print()
# downward pyramid
for i in range(n - 1):
for j in range(i + 1):
print(' ', end='')
for j in range(2*(n - i - 1) - 1):
print(j+1, end='')
print()
Output:
1 123 12345 1234567 123456789 1234567 12345 123 1
9. Number Hourglass pattern
123456789 1234567 12345 123 1 123 12345 1234567 123456789
The number hourglass pattern is again a famous pattern you can create it using numbers. It is the same as a diamond pattern but you should have a vision for this as how it is that.
When observed you will find 2 figures in the pattern, one is the number pyramid and the other is the reverse number pyramid.
So using the concepts of the above program here is a complete program for this pattern.
# hourglass number pattern
n = 5
# downward pyramid
for i in range(n-1):
for j in range(i):
print(' ', end='')
for k in range(2*(n-i)-1):
print(k+1, end='')
print()
# uppward pyramid
for i in range(n):
for j in range(n-i-1):
print(' ', end='')
for k in range(2*i+1):
print(k+1, end='')
print()
Output:
123456789 1234567 12345 123 1 123 12345 1234567 123456789
10. Right pascal triangle pattern
1 12 123 1234 12345 1234 123 12 1
The right pascal triangle pattern is shown above. Again it contains 2 figures within it, one is a left triangle and the other is a reverse left triangle.
You have seen above how to create both of them. Let's see the complete code for this pattern.
# right pascal
n = 5
# upper triangle
for i in range(n):
for j in range(i + 1):
print(j+1, end="")
print()
# lower triangle
for i in range(n):
for j in range(n - i - 1):
print(j+1, end="")
print()
Output:
1 12 123 1234 12345 1234 123 12 1
11. Heart pattern in python
12 12 12345678 123456 1234 12
The number heart pattern can be created using numbers and spaces. It is really pattern complex to create.
You can see the complete code of heart pattern below.
Note - Do not increase the size of the heart by more than 4 because numbers become 2 digits after 9 which will distort the shape of the heart.
# heart pattern
n = 4
# upper part of the heart
for i in range(n//2, n, 2):
# print first spaces
for j in range(1, n-i, 2):
print(" ", end="")
# print first number
for j in range(i):
print(j+1, end="")
# print second spaces
for j in range(1, n-i+1, 1):
print(" ", end="")
# print second number
for j in range(i):
print(j+1, end="")
print()
# lower part
for i in range(n, 0, -1):
for j in range(i, n):
print(" ", end="")
for j in range(i*2):
print(j+1, end="")
print()
Output:
12 12 12345678 123456 1234 12
Conclusion
You have learned to create many different types of number pattern in python in this section. Now you have enough experience to create many other patterns.
For further exploration, you can see pattern programs in python.