×

Number Pattern Programs in Python

Learning Python is very easy, and it understands in better way compared to other programming languages. One of the many reasons why it has emerged as the most widely used programming language in this decade is the ease with which numerous libraries may be implemented. Although learning is simple, most of interviewers frequently ask us how we built the logic for pattern programmes which is the main key to programming skills.

Pattern programmes in Python are followed as:

  • Square pattern
  • Left triangle alphabet pattern
  • Right triangle pattern
  • Pascals triangle pattern
  • Hollow triangle alphabet pattern
  • Number pyramid pattern
  • Diamond pattern
  • Hourglass pattern

Before learning patterns lets discuss the code which is used to print the numbers from 0-9 and it is followed by for loop.

Code:

for i in range(10):
 print(i, end=’  ’)

Output:

0 1 2 3 4 5 6 7 8 9

Different Patterns are followed as:

Square Pattern:

Generating square pattern is very easy using Python. In order to make a square pattern, we need to utilise two stacked loops. We can set the internal loop to print the number a certain number of times, and it will print that many times. The internal loop will run based on the declaration done in outer loop.

Code:

for m in range(5):
for n in range(5):
print(n+1, 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

Left Triangle Alphabet Pattern:

A triangle-shaped pattern made out of numbers is called the left triangle pattern. The programme for this will consist of two nested loops, with the inner loop printing the number of times the outer loop has been executed and doing so for each iteration.

Code:

a = 5
for m in range(a+1):
    for n in range(1, m+1):
      print(n, end=' ')
    print()

Output:

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

And we can also edit and manipulate the code that tends a pattern that modifies each succeeding digit in a row, or another pattern that modifies each succeeding digit in a column.

Code:

a = 5
for m in range(a+1):
    for n in range(1, m+1):
      print(m, end=' ')
    print()

Output:

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

Right Angle Pattern:

Every time the pattern is repeated, the number of spaces rises at first.

Two loops are needed and the need is described as the first loop is initiated to

print the spaces and the other loop is declared to print the numbers in the

specified pattern.

Code:

m = 5
for a in range(m):
    for j in range(1, m - a):
        print(" ", end="")
    for h in range(1, a + 2):
        print(h, end='')
    print()

Output:

    1
   12
  123
 1234
12345

Right Pascal Triangle:

Above is a diagram of the right pascal triangle pattern. Once more, it has two figures within it: a left triangle and a reverse left triangle. How to create both of them has been demonstrated above. Let's examine this pattern's whole source code.

Code:

n = 5
# upper triangle formation
for m in range(n):
    for k in range(m + 1):
        print(k+1, end="")
    print()
# lower triangle formation
for m in range(n):
    for k in range(n - m - 1):
        print(k+1, end="")
    print()

Output:

1
12
123
1234
12345
1234
123
12
1

Hollow Triangle Alphabet Pattern:

The voids throughout the pattern make it a little difficult to make. Keep in mind a few things when creating this: only print numbers in the first and last rows, and only print numbers in the first and last positions of other rows, leaving the remainder blank.

Code:

n = 6
for a in range(1, n+1):
    count = 1
    for k in range(a):
        if k == 0 or k == a-1:
            print(count, end='')
            count += 1
        else:
            if a != n:
                print(' ', end='')
            else:
                print(count, end='')
                count += 1
    print()

Output:

1
12
1 2
1  2
1   2
123456

Number Pyramid Pattern:

A well-known pattern that may be made with numbers is the pyramid pattern.

Each line contains an odd number of integers; the first line contains one, the second contains two, the third contains three, and so on. The programme has two internal loops, the first of which prints white space and the second of which prints the growing integers 2n +1

Code:

a = 5
for m in range(a):
    for l in range(a - m - 1):
        print(' ', end='')
    for h in range(2 * m + 1):
        print(h + 1, end='')
    print()

Output:

   1
   123
  12345
 1234567
123456789

Number Pattern:

This diamond pattern has numbers in it. If we look attentively, we will notice two figures in the pattern: a number pyramid and a reverse number pyramid. Therefore, we must write a programme that prints a number pyramid and then reverses the pattern back to front.

Code:

a = 5
#top pyramid
for m in range(a):
    for k in range(a - m - 1):
        print(' ', end='')
    for k in range(2 * m + 1):
        print(k+1, end='')
    print()
#bottom pyramid
for m in range(a - 1):
    for k in range(m + 1):
        print(' ', end='')
    for k in range(2*(a - m - 1) - 1):
        print(k+1, end='')
    print()

Output:

     1
    123
   12345
  1234567
 123456789
  1234567
   12345
    123
     1

Hourglass Pattern:

Another well-known pattern that may be made with numbers is the hourglass pattern. Although it is the same as a diamond pattern, we should have a mental image of how it looks. Two figures can be seen in the pattern when it is observed: one is the number pyramid and the other is the reverse number pyramid. So here is a complete programme for this pattern utilising the ideas from the programme above.

Code:

a = 5   # bottom pyramid
for m in range(a-1):
    for s in range(i):
        print(' ', end='')
    for h in range(2*(a-m)-1):
        print(h+1, end='')
    print()
# top pyramid
for m in range(a):
    for s in range(a-m-1):
        print(' ', end='')
    for h in range(2*m+1):
        print(h+1, end='')
    print()

Output:

123456789
 1234567
  12345
   123
    1
   123
  12345
 1234567
123456789

Related Topics

Python pynmea2

Define Pynmea2 Python Programming Language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in...

4 minutes read.

Python ltrim() function

Python ltrim() function The ltrim() function in PHP removes whitespace or other predefined characters from the beginning or left side of a string. The related functions are as follows: rtrim() – This function is used to...

1 minute read.

Method Overloading in Python

Overloading is a capability of a method and operator to behave differently according to the nature of parameters. Overloading is popular because it provides a good number of advantages: Reusability of code.Reduces...

3 minutes read.

How to find square root in python

How to find Square Root of a number In Python Python makes a lot of tasks easier by using different functions, modules, and libraires. There is an inbuilt function in Python...

6 minutes read.

Number Pattern Programs in Python

Learning Python is very easy, and it understands in better way compared to other programming languages. One of the many reasons why it has emerged as the most widely used...

4 minutes read.

Python Program to Generate a Random String

The term "random" refers to a group of information or data that can be accessed in any chronological order. To create random strings, a Python program called random is utilized....

6 minutes read.

Amazon rekognition using python

Amazon recognition service is an AI service which is an image labelling API (Application programming interface). In this article, we shall learn to use the AWS python boto3 module to...

3 minutes read.

Python Variables

Variables are one of the most important terms we should be familiar with if we want to be good programmers. In simpler words, we use variables to store a value....

4 minutes read.

Yield Statement In Python

The generators are defined by using the yield statement in Python. Generally, it converts a normal Python function into a generator.  The yield statement hauls the function and returns back the...

2 minutes read.

Iterate a Dictionary in Python

In this tutorial, we will learn how to Iterate a Dictionary in Python. Dictionary: In Python, a dictionary is an unordered collection of data values that is used to store data values,...

3 minutes read.

Sentence to python vector

Conversion of a Sentence to Vector in Python Before starting the tutorial, let’s just recap about the vector and the respective package that has to be imported in Python. Python Vector: Putting simply,...

3 minutes read.

Python Switch Case

What is a Switch Case Statement? In the programming languages, a switch statement is a logical loop or syntax that tests the variable's value and compares it with multiple cases. Once...

3 minutes read.

Slicing of a String in Python

In this tutorial, we will learn about slicing of a string in Python. First of all, let us know what String and Slicing is. String String is a group of characters that...

6 minutes read.

Assignment Operators in Python

The prime usage of Assignment Operators is to assign values to variables. These are taken into account to do operations on values and variables. There are some special symbols in python...

4 minutes read.

Python program to find whether a given number is even or odd

Python program to find whether a given number is even or odd A number is said to be even if any number is completely divisible by 2, which means there is...

1 minute read.

Python delattr() function

Python delattr() function The delattr() function in Python is used to delete the named attribute from the object, with the prior permission of the object. Syntax delattr(object, name) Parameter object : This parameter represents the object from which...

1 minute read.

Import Function in Python

In Python programming language, the import keyword plays an important role in the whole code because it makes one module make available in another module. Import is used to structure...

3 minutes read.

Python sort() function

Python provides many built-in functions for solving many problems that arise in different situations in programs. One of such methods is the sort () method. In this article, the syntax...

4 minutes read.

How to Declare a Variable in Python?

The concept of constants and variables is something that we are studying right from our primary classes. We know that constants are the fixed values whereas variables are those whose...

4 minutes read.

List Comprehension in Python

Sometimes we need new lists that are completely based on previously existing lists, so to perform this operation successfully, some of the programming languages come with a syntactic construct known...

5 minutes read.