Pattern Program in Python

In this tutorial, I will demonstrate how to print patterns in Python. The for loop, while loop, and range() functions are used in the following Python scripts to display various patterns.
This tutorial will show you how to print the patterns shown below in Python.

By printing different patterns, you can build a solid understanding of loops in Python. After reading this article you can create various types of patterns.

Steps to Print Pattern in Python

Use the below steps to print pattern in Python

  1. Decide the number of rows and columns
    Any design may be printed using a standard format, which includes the number of rows and columns. To print any pattern, we must use two loops, or nested loops.The inner loop informs us of the column required to print the pattern, while the outer loop provides the number of rows. Use the input() function to get the user’s input on how many rows to accept before determining the size of a pattern.

  2. Iterate rows
    Next, create an outer loop with a for loop and the range() method to iterate the number of rows.

  3. Iterate columns
    The inner loop or nested loop should then be written to manage the number of columns. The results of the outer loop’s iteration affect the inner loop’s iteration.

  4. Print star or number
    Use the print() function in each iteration of the nested for loop to display the symbol or number of a pattern (like a star (asterisk *) or number).

  5. Add a new line after each iteration of the outer loop
    For the pattern to show properly, add a new line using the print() method after each iteration of the outer loop.

Algorithm and Explanation of Pattern Program in Python

Pattern program in python
Pattern 1

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

Python Program for Pattern

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

Output

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

Explanation:
In this number pattern, the first row had a single digit, the second row contained the next two digits, the third row contained the following three numbers, and so on until the desired number of rows was reached.

Pattern 2

1 1 1 1 1 
2 2 2 2 
3 3 3 
4 4 
5
rows = 5
b = 0
for i in range(rows, 0, -1):
    b += 1
    for j in range(1, i + 1):
        print(b, end=' ')
    print('\r')

Output

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

Pattern 3(Pascal triangle pattern)

1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
1 6 15 20 15 6 1
def print_pascal_triangle(size):
    for i in range(0, size):
        for j in range(0, i + 1):
            print(decide_number(i, j), end=" ")
        print()


def decide_number(n, k):
    num = 1
    if k > n - k:
        k = n - k
    for i in range(0, k):
        num = num * (n - i)
        num = num // (i + 1)
    return num

rows = 7
print_pascal_triangle(rows)

Output

1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
1 6 15 20 15 6 1

Pattern 4(Multiplication table pattern)

1  
2  4  
3  6  9  
4  8  12  16  
5  10  15  20  25  
6  12  18  24  30  36  
7  14  21  28  35  42  49  
8  16  24  32  40  48  56  64
rows = 8
for i in range(1, rows + 1):
    for j in range(1, i + 1):
        square = i * j
        print(i * j, end='  ')
    print()

Output

1  
2  4  
3  6  9  
4  8  12  16  
5  10  15  20  25  
6  12  18  24  30  36  
7  14  21  28  35  42  49  
8  16  24  32  40  48  56  64 

Pattern 5(Downward star pyramid pattern)

        * * * * * * 
         * * * * * 
          * * * * 
           * * * 
            * * 
             *
rows = 5
k = 2 * rows - 2
for i in range(rows, -1, -1):
    for j in range(k, 0, -1):
        print(end=" ")
    k = k + 1
    for j in range(0, i + 1):
        print("*", end=" ")
    print("")

Output

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

Conclusion:
Understanding loops and putting programming theory into practice are both necessary for pattern printing in Python.
One may need to refer to additional in-depth concepts and resources to gain a deeper understanding of Python and its use in fields like data science. PrepBytes is one such tool that will assist you in learning Python from A to Z as well as its uses and implementation in data science.

Other Python Programs
Python program to reverse a number
Python program for heap sort
Python program to check armstrong number
Python program to check leap year
Python program to convert celsius to fahrenheit
Python program to find factorial of a number
Python program to reverse a linked list
Python Program to find the middle of a linked list using only one traversal
Python Program to Add Two Numbers
Python Program to Check Palindrome Number
Python Program to Print the Fibonacci Series
Python Loop Program
Anagram Program in Python
Fizzbuzz Program in Python
String Programs in Python
List Program in Python
Prime Number Program in Python

Leave a Reply

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