×

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 Breakpoint

Introduction In Python 3.7, a brand-new created function called breakpoint() was added. Due to the close relationship between both the executable and the code of a debugging component, debugging Python programming...

4 minutes read.

N2 in Python

N2 is known as Nearest Neighbor Algorithm, because it contains 2 N's (N-Nearest, N-Neighbor). This is a library in the python build using C++ and Python. Before N2 was made,...

3 minutes read.

Introduction to Scratch programming

Scratch programming is generally designed for children who may create digital stories, games, and animations using the computer language Scratch, which has the largest kid-focused community in the world. Scratch...

3 minutes read.

Captcha Code in Python with Example

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Compound Interest GUI Calculator using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

6 minutes read.

Difference between Sort and Sorted in Python

If you new to Python, it must be confusing the distinction between the Sort and Sorted functions. However, it is important to understand the differences in order to use them...

5 minutes read.

Reason for Python So Popular

One of the languages that is witnessing outstanding development and acceptance every year in Python. Python has emerged as the programming language with the greatest rate of growth, and Stack...

3 minutes read.

STL in Python

Python has many libraries that are very useful and simplify many complex codes. In the same way, STL is also one of the python libraries used for reading and writing...

3 minutes read.

Python Program to check whether a given number is Armstrong or not

Program to check whether a given number is Armstrong or not A number is said to be an Armstrong if the sum of each digit's cube of a given number equals...

1 minute read.

First Unique Character in a String Python

This article aims to introduce you to strings and how to create a string in Python, and then we will solve a simple yet exciting DSA (Data Structures and Algorithms)...

3 minutes read.

Python Lists vs Tuples

The difference between lists and tuples is one of the most frequently asked questions in an interview related to python language. Lists and Tuples are two of Python’s built-in data...

4 minutes read.

Python Constructor

Introduction A constructor is defined as the special kind of function or method that is used for instance variables initialization during the creation of an object of a class. The constructor's task...

5 minutes read.

Raise Exception in Python

Most of the programmers/developers create large programs to solve complex tasks in Python. The code can be of 1000 lines and even more based on the need of the problem....

5 minutes read.

Python | a += b is not always a = a + b

a += b in Python doesn't always behave the same way as a = a + b; the same operands can produce different outcomes depending on the circumstances. But we...

3 minutes read.

Python comment symbol

Python programmers frequently use the comment system because, without it, things may quickly become very perplexing. The developers' helpful information is provided in the comments, which helps the reader understand...

3 minutes read.

How to create a DataFrames in Python

How to create a DataFrames in Python Data Frame is a data structure in which data is stored in tabular form. They can also be referred as two-dimensional collection of data. Various...

5 minutes read.

Fsolve in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python List extend() method

Python List extend() method The list.extend() method extends the list by appending all the items from the iterable. Syntax list.extend(iterable) Parameter iterable: It is a required parameter which represents any iterable unlike list, set, tuple, etc. Example 1 # Python...

1 minute read.

Python Modules

Python Modules The Python module is a collection of definition and statements. It can contain functions, classes, and variables.  Combining the related code into a module makes the code easier to understand and use....

5 minutes read.

Working with files in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has an...

5 minutes read.