pattern program in python

Pattern Programs are widely asked by interviewers whenever you go for a python developer job or a software developer job. Understanding the importance of this, we have come up with this article on “Pattern Program in Python”. 

Basic tutorial for Python to get started

  1. Number Pattern Problem
  2. Incrementing Number Pattern Problem
  3. Pyramid Pattern Problem with Stars
  4. Inverted Semi-Pyramid Pattern Problem with Numbers
  5. Inverted Semi-Pyramid Pattern Problem with Descending order of Numbers
  6. Semi-Pyramid Pattern Problem with Numbers descending in each row

Number Pattern Problem

The pattern we want to form should look like as following

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

Let’s go ahead and see how can we print this pattern program in python:

depth = 6
for number in range(depth):
    for i in range(number):
        print(number, end=" ")
    print(" ")

Code Explanation:

We start off by initializing a variable called “depth” and give it a value of 6 with the help of this command:

 depth = 6

Going ahead, we have a nested for loop. We have the outer for loop to set the depth or number of rows in this pattern. When we use the command:

for number in range(depth):

This helps us to get a list of numbers 0-5. Because the depth value is 6, this means that 6 is exclusive and that is why the range goes from 0-5.

Then, we set the inner for loop using this command: 

 for i in range(number).

In the inner for loop, initially, the value of the number is 0, that is why we skip the first row. Then we go back to the outer for loop and the number value becomes 1. Once the number value is 1, we head inside the inner loop and print 1.

After that number value is incremented again and it becomes 2, then we head to the inner loop and print 2 2.

Similarly, we go back to the outer loop and this time number value becomes 3. After this inside the for loop, we print 3 3 3.

Further which, again we go back to the outer loop, then the value of “number“ becomes 4 and we print 4 4 4 4 with the inner loop.

Finally, we will print 5 5 5 5 5.


Incrementing Number Pattern Problem

The pattern we want to form should look like as following

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

Now, let’s go ahead and see how can we have a sequence of numbers in each row with python:

depth = 6
for number in range(1, depth):
    for i in range(1, number + 1):
        print(i, end=' ')
    print("")

Code Explanation:

We start off by initializing the value of depth to be 6. Then, we set the outer for loop with this command:

for number in range(1, depth)

With the help of this command, we create a range of numbers which go on from 1 to 5. Then, we go ahead and set the inner for loop with this command:

 for i in range(1, number + 1). 

Inside the for loop, we print the digits using this command: 

print(i, end=' ').

Initially, the value of the number is 1 in outer for loop, so we enter the inner for loop and go ahead and print 1.

Then in the outer for loop, the value of number becomes 2, so we enter the inner for loop and we print 1 2.

Similarly, we go back to the outer loop and here the number value becomes 3, so this time we enter the inner loop and print 1 2 3.

Going ahead, the value of “number” in the outer loop becomes 4, so with the inner loop we print 

1 2 3 4

And in the final iteration, we print out: 1 2 3 4 5


Pyramid Pattern Problem with Stars

The pattern we want to form should look like as following

*
* *
* * *
* * * *
* * * * *

Let’s see the code for this pattern program in python:

def diamond(n):
    for m in range(0, n):
        for i in range(0, m+1):
            print("* ",end="")
print("\r")
n = 5
diamond(n)

Code Explanation:

We start off by defining a method called “diamond” with this command: def diamond(n).

We set the outer for loop with this command:

for m in range(0, n)

Then, we go ahead and set the inner for loop using this command:      

for i in range(0, m+1)

Initially, the value of’ is 0 in the outer for loop, so we go inside the for loop and print *. Then, the value of’ in the outer for loop becomes 1, this time we go inside the inner loop and print * *. Similarly, the value of’ in the outer loop becomes 2, so, we go inside the inner loop and print * * *.

Going ahead, the value of’ is incremented in the outer loop and it becomes 3, so, with the inner loop, we print * * * *.In the final iteration, we print out * * * * *.


Inverted Semi-Pyramid Pattern Problem with Numbers:

The pattern we want to form should look like as following

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

Let’s write the code for this:

row = 5
a = 0
for i in range(row, 0, -1):
    a += 1
    for j in range(1, i + 1):
        print(a, end=' ')
    print('\r')

Code Explanation:

We start off by initializing two variables. We set the value of row to be equal to 5 and the value of a to be equal to 0. After that we set the outer for loop with this command: 

for i in range(row, 0, -1):

This outer for loop gives us numbers in descending order starting with 5 going on till 1. In the outer for loop, we increment the value of a with 1. After that, we set the inner for loop using this command:

for j in range(1, i + 1).

In the first iteration, the value of ‘i’ in outer for loop is 5, so with the inner for loop we print: 

1 1 1 1 1

In the second iteration, the value of ‘i’ in outer for loop is 4, so in the inner for loop we print:

2 2 2 2

In the third iteration, the value of ‘i’ in the outer loop is decremented to 3, then, with the inner loop we print:

3 3 3

Going ahead in the fourth iteration, the value of ‘i’ in the outer loop is decremented to 2, so this time with the inner loop, we print:

2 2 

And finally, in the last iteration, we print out 1.


Basic tutorial for Python to get started

Inverted Semi-Pyramid Pattern Problem with Descending order of Numbers:

The pattern we want to form should look like as following

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

Let’s look at the code to implement this pattern program in python:

depth = 5
for i in range(depth, 0, -1):
    n = i
    for j in range(0, i):
        print(n, end=' ')
    print("\r")

Code Explanation:

We start off by initializing the value of depth to be equal to 5. Then using the outer for loop, we produce a list of numbers in descending order from 5 to 1. Inside the outer for loop, we set the value of n to be equal to i. After that, we start off the inner for loop using this command:     

for j in range(0, i):

In the first iteration the value of ‘i’ is 5 in the outer loop, so with the inner loop we print: 5 5 5 5 5

In the second iteration, the value of ‘i’ in the outer loop is decremented to 4, so we enter the inner loop and print 4 4 4 4

Then in the third iteration, the value of ‘i’ becomes 3, so with the inner loop, we print: 3 3 3

Going ahead in the fourth iteration, the value of ‘i’ becomes 2, this time we print out 2 2 with the inner loop.

Finally, in the last iteration, the value of ‘i’ becomes 1 and we print out 1.


Semi-Pyramid Pattern Problem with Numbers descending in each row:

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

Let’s look at the code for this pattern program in python:

size = 6
for row in range(1, size):
    for column in range(row, 0, -1):
        print(column, end=' ')
    print("")

Code Explanation:

We start off by setting the value of variable size to be equal to 6. Then, we set the outer for loop with this command: for row in range(1, size). With this outer for loop, we produce a list of numbers starting from 1 going on till 5. Then, we set the inner loop with this command:     

for column in range(row, 0, -1)

In the first iteration, the value of row is 1 in the outer loop, so we enter the inner for loop and print 1.

Then in the second iteration, the value of row is 2 in the outer loop, so this time we enter the inner loop and print: 2 1

After that, in the third iteration, the value of row becomes 3 in the outer loop, so in the inner loop we go ahead and print: 3 2 1

Going ahead, in the fourth iteration, the value of row becomes 4 in the outer loop, so this time, in the inner loop we print: 4 3 2 1

Finally, in the last iteration, we go ahead and print: 5 4 3 2 1

This brings us to the end of this article where we learned some basic programs and pattern program in python. In order to get a free course on Python, click on the banner below.

pattern program in python
0

LEAVE A REPLY

Please enter your comment!
Please enter your name here

two × one =