Python program to print numeric pattern

In this tutorial, you are going to learn that how you can print the different types of numeric pattern in Python. Numeric pattern are the series of digits(0-9) which forms any pattern or shape. These patterns are created in the programming language with the help of nested for loop. There are different types of numeric pattern that can be made through the for loop.

Different types of numeric pattern

Number pattern 1:

  1. During the first iteration of the outer loop the i becomes 1 and it goes into the inner loop.
  2. The inner loop will print j which has a value from 1 to 5 in a line then this loop will end.
  3. The pointer will go on the next line due to print().
  4. Now, the outer loop will iterate for the second time. Repeat all the above step again until the outer loop end.
  5. The outer loop will end when i becomes 5 and numeric pattern is created.
# Outer loop
for i in range (1,6):
    # Inner loop
    for j in range (1,6):
        print( j,end=" ")
    print()

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

Number pattern 2




  1. The first iteration of the outer loop will give i=1 and goes into the inner loop.
  2. The inner loop will work for 5 times for the range (1,6) and print i in a line that will print the value of i and then the loop ends.
  3. print() will get the pointer in the next line.
  4. Now, the outer loop will iterate for the second time. Repeat all the above steps again until the outer loop end.
  5. The outer loop will end when i becomes 5.
# Outer loop
for i in range (1,6):
    # Inner loop
    for j in range (1,6):
        print(i,end=" ")
    print()

Output:-

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

Number pattern 3

  1. The first iteration of the outer loop will give i=1 and goes into the inner loop.
  2. The inner loop will work for the range(1,i+1) i.e., one time for the first iteration of the outer loop.
  3. The inner loop will print j in the first line {j=1 for the first iteration of the outer loop}.
  4. print() will get the pointer in the next line.
  5. Now, the outer loop will iterate for the second time. Repeat all the above steps again until the outer loop end.
  6. The outer loop will end when i becomes 5.
# Outer loop
for i in range (1,6):
    # Inner loop
    for j in range (1,i+1):
        print(j,end="")
    print()
    

Output:-

1
12
123
1234
12345

Number pattern 4

  1. The first iteration of the outer loop will give i=1 and goes into the inner loop.
  2. The inner loop will work for the range(1,i+1) i.e., one time for the first iteration of the outer loop.
  3. The inner loop will print i in the first line {i=1 for the first iteration of the outer loop}.
  4. print() will get the pointer in the next line.
  5. Now, the outer loop will iterate for the second time. Repeat all the above steps again until the outer loop end.
  6. The outer loop will end when i becomes 5.
# Outer loop
for i in range (1,6):
    # Inner loop
    for j in range (1,i+1):
        print(i,end="")
    print()
    

Output:-

1
22
333
4444
55555

Number pattern 5

This pattern is called Floyd’s triangle.

  1. Take a constant k which is equal to 1.
  2. During the first iteration of the outer loop the i becomes 1 and goes into the inner loop.
  3. The inner loop will work for the range(1,i+1) i.e., this will increase the range by one at every line of the pattern.
  4. Print the value of k and increment it to print the pattern {k,k+1,k+2,k+3….}
  5. The first iteration will be complete with print() and the pointer goes into the next line.
  6. The outer loop will end when i becomes 5 until repeat all the above steps to print the pattern.
k=1
# Outer loop
for i in range (1,6):
     # Inner loop
    for j in range (1,i+1):
        print(k,end=" ")
        k=k+1
    print()
    

Output:-

The output of our Python program is given below:

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15

Number pattern 6

  1. The working of this pattern is similar to the above pattern.
  2. The only difference is that the inner loop will print instead of k.
# Outer loop
for i in range (1,6):
    # Inner loop
    for j in range (1,6):
        print(i,end=" ")
        i=i+1
    print()
    

Output:-

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

 

So, you can see that we are able to print different types of numeric pattern in Python.

Go and check other tutorials on python:

 


Leave a Reply

Your email address will not be published. Required fields are marked *