Number Pattern Programs in Python – Very Imp e.g.

Let it be an interview, college/university exams or Hackathon. Loop is very very important. Even in professional programming loop is very very important. And the best method to understand the loop is to practice Number Pattern Programs in Python.

So in this practice blog , we will see how to write 10 different number pattern programs using for loop in python.

To understand this example you should know the very basics of python. I mean core python. If you want to learn python easily you can consider this course of Core Python.

For all the practice Videos and Explanation on Python please click over here. Python Practice Series.

Video Explanation to Print Number Pattern Program in Python

The below videos explains a simple Pattern Program in Python. The video focuses more on the approach. If you understand one pattern program you can understand all the remaining program very easily. Before you move to the next pattern don’t forget to watch the Video

Different Types of Number Pattern in Python and Their Coding

I hope you understood the Video. Now we will see the different patterns and their code. The first code will explain the same program in the video. It will be easier to copy and paste if you want to use it anywhere.

1. Pattern: Simplest Number Pattern

1
2 2
3 3 3
4 4 4 4

This pattern prints the number in 4 different lines. With each new line, a number is incremented. We can implement this pattern in two different ways one using a single loop and the other using two loops.

Let’s see the code for both methods.

Method 1: Simplest Number Pattern – Single Loop

Method one is a bit tricky and uses the string multiplication feature of python. In python, multiplying string repeats the strings. So if you write “A” * 5, A will be printed 5 times.

#python code to print the simplest number pattern in Python
for r in range (1,5):
    print(str(r) * r)

Method 2: Simplest Number Pattern – Two Loops

Code to print the Number pattern in Python using two loops. This is the most traditional method and it is a must to understand this method, so that you can write other number pattern programs in python

Here we will write one loop to travel along row and column. In below the print statement is very important.

First “print” with end character as space ensures that the number is printed in the same line when the inner loop in python is running. The second print statement gives the next line after all the numbers in a single line are printed.

#python code to print the simplest number pattern in Python Method 2 using two loops

for r in range(1,5):
    for c in range(1,r+1):
        print(r, end=' ')
    print()

2. Pattern: Inverted Pyramid of Same Numbers in Python

This example is the same above to some extent but it’s reversed. Here the first line will print all the maximum time while the last line will have only 1 digit. It will look like this

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

We can print the above pattern using a single loop or two-loop. We will see and understand both the methods to print number pattern program in Python.

Method 1: Inverted Pyramid of Same Numbers in Python – 1 Loop

Since we need to print 5 numbers in one line the loop will start from 5 and will go till 0. After that, we will use the string multiplication feature of python.

In the range function, the first parameter is the initial value of the variable “r”, 0 is the last value and -1 is the step i.e. decrement.

#python code to print Inverted Pyramid of Same Numbers in Python - 1 Loop
for r in range (5,0,-1):
    print(str(r) * r)

Method 2: Inverted Pyramid of Same Numbers in Python – 2 Loop

In this method, we will use two loops. The first loop will travel from 5 to 1. Since the loop is starting from 5, the second loop will print numbers 5 times. But it will print the same number i.e. variable r.


#python code to print Inverted Pyramid of Same Numbers in Python - 2 Loop
for r in range (5,0,-1):
    for c in range(1,r+1):
        print(r, end=' ')
    print()

3. Pattern: Pyramid of Incremental Numbers in Each Line – Python

In the last two examples, we printed number patterns, with the same number in each line. But in this example, we will print the pyramid but the number will keep on increasing in the same line. It will start again with 1 in the next line.

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

Code to Print Number Pattern Program – Pyramid of Incremental Numbers

#python code to print Pyramid of Incremental Numbers in Each Line 
for r in range (6):
    for c in range(1,r+1):
        print(c, end=' ')
    print()

4. Pattern: Inverted Pyramid of Incremental Numbers in Each Line – Python

Well, this is the same as example number 3, but in the reverse order. So our first loop will start from 6 and will go till 0 with the step in the loop as -1. The inner loop will print incremental numbers till the value of “r” which is the counter of the first loop. But always remember to add the print statement after the inner loop since it ensures the next line after the inner loop.

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

Code to print above number pattern programs in python is this.


#python code to print Inverted Pyramid of Incremental Numbers in Each Line 
for r in range (5,0,-1):
    for c in range(1,r+1):
        print(c, end=' ')
    print()

5. Pattern: Pyramid of Decrementing Numbers in Each Line – Python

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

Code to print Pyramid of Decrementing Numbers in Each Line – Python

#python code to print Pyramid of Decrementing Numbers in Each Line - Python 
for r in range (6):
    for c in range(r,0,-1):
        print(c, end=' ')
    print()

7. Pattern: Inverted Pyramid of Decrementing Numbers in Each Line – Python

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

Code to print Inverted Pyramid of Decrementing Numbers in Each Line – Python

#python code to print Inverted Pyramid of Decrementing Numbers in Each Line - Python
for r in range (5,0,-1):
    for c in range(r,0,-1):
        print(c, end=' ')
    print()

8. Pattern: Pyramid of Incremental Numbers till the End up to 10 in Python

Till now we written number pattern programs in the python which reinitialize the sequence after every new line. But now we will write a program which will print a pyramid with number counter increasing. Even if the line changes. Exactly like the below pattern.

Here the trick to declare a third variable called as “number”. This will increase by 1 every time a loop iteration is executed. Instead of counter of loop we will print this third variable.

1
2 3
4 5 6
7 8 9 10

Code to print Pyramid of Incremental Numbers till the End in Python

#python code to print Pyramid of Incremental Numbers till the End in Python
number = 1
for r in range (5):
    for c in range(1,r + 1):
        print(number, end=' ')
        number = number + 1
    print()

9. Pattern: Pyramid of Decrementing Numbers till the from 10 in Python

10
9 8 
7 6 5
4 3 2 1
#python code to print Pyramid of Decrementing Numbers till the End in Python
number = 10
for r in range (5):
    for c in range(1,r + 1):
        print(number, end=' ')
        number = number - 1
    print()

10. Patterns: Pyramid of Even Numbers

Well you can convert any pattern into odd and even. You can write any program with odd and even numbers just by changing the initialization, increment or the printing factor that step in Python. Let’s try the simplest pattern program to print even numbers in Python. In this example we are not changing any part of Initialization or increment but just the printed factor. We just have multiplied the printing variable by 2.

2
4 4
6 6 6
8 8 8 8

Code to print Pyramid of Even Numbers


#python code to print Pyramid of Even Numbers

for r in range (5):
    for c in range(1,r + 1):
        print(r * 2, end=' ')
    print()

10. Patterns: Pyramid of Odd Numbers(Logic is totally Different)

The logic for Even number was very easy. But this is not the case with Odd Numbers. We just can’t multiply to get the odd numbers like we did in the patters for Even Number. logic a bit tricky over here.

So in the number pattern program of Python we will first write the main loop to with initialize value as 1 and step of 2. So the counter “r” will have values 1,3,5,7 and so on. These are nothing but our odd numbers.

In the inner loop the max range parameter is very important. The formula will be int(r/2))+2 , where r is the row that is counter of the first loop. And the “int” function is to convert the any decimal value into integer. Since division is involve we need to take care any possible decimal value.

int(r/2))+2 ,So if the value of r is one , this function will return 2 and hence the inner loop will run 1 time

If the value of r is 3, this formula will give the value 3 and hence the inner loop will run for 2 times. This process will go on until the main loop breaks.

This is how the pattern will look like.

1
3 3
5 5 5
7 7 7 7
9 9 9 9 9

Code to print Pyramid of Odd Numbers

#python code to print Pyramid of Odd  Numbers

for r in range (1,11,2):
    for c in range(1,int(r/2) + 2):
        print(r, end=' ')
    print()

General Logic – How to Print Number Pattern Programs in Python

While writing any program to print number patterns , the logic and the approach is very very important. Let’s assume a simple example and see how to approach the problem so that you can write any number pattern program in Python.

For Explanation we will assume this simple example of Number Pattern.

1 
1 2
1 2 3 
1 2 3 4 5

Time needed: 15 minutes.

How to Print Number Pattern Programs in Python

  1. Writing the Inner Loop

    Never approach the problem from the top but from the base. If you look at the above pattern we need to print 1 in first line , two numbers 1 and 2 in second line . 3 Numbers 1,2,3 in third line and so on. So Write a loop that prints 1 to n in a line.

    for j in range(1,6):
    print(j , end = ‘ ‘)

    We have added end character as space so that it will print the number in the same line

  2. Write the Main Loop

    If you look at the pattern you will that we need to print these numbers in five different lines. So we will include this loop from step 1, inside the main loop. And also remembers after the inner loop has done its work for the line we need the next link. For this, we will add a blank print statement link below code.

    for i in range (1,6):
    for j in range(1,5):
    print(j , end = ' ')
    print()

    If you run this code you will get this output.
    1 2 3 4 5
    1 2 3 4 5
    1 2 3 4 5 .
    1 2 3 4 5

    Well this not right, to correct it implement the third step.

  3. Make inner loop depending on the Main Loop

    In the above step, the range of the inner loop is 5 hence it’s printing 5 numbers in each line. We need to change the range to i + 1 i.e. the counter of the main loop. When the value of i is will the inner loop will run 1 time and when the value of i is 2, the inner loop will run two times and so on.

    Here is the complete code to print number pattern
    for i in range (1,6):
    for j in range(1,i+1):
    print(j , end = ‘ ‘)
    print()

Facebook Comments
(Visited 5 times, 1 visits today)

Leave A Comment

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