Python Programs to Print Pattern – Print Number, Pyramid, Star, Triangle, Diamond, and Alphabets Patterns

Posted in

Python Programs to Print Pattern – Print Number, Pyramid, Star, Triangle, Diamond, and Alphabets Patterns
vinaykhatri

Vinay Khatri
Last updated on December 18, 2024

    Printing patterns using Programming languages is one of the most common interview problems. In this tutorial, we will see how we can use the Python programming language to print different patterns. This tutorial includes the following Patterns in Python

    1. Number patterns
    2. Triangle patterns
    3. Start * patterns
    4. Pyramid patterns
    5. Inverted Pyramid patterns
    6. Half Pyramid patterns
    7. Diamond patterns
    8. Alphabets patterns
    9. Square and rectangle patterns

    How to print patterns in Python?

    Mostly all the patterns include nested loop, where the outer loop defines the number of rows, and the inner loop prints the data for each row. While printing any pattern, we should take care of the following steps.

    Step 1: Classify the role of rows and columns.

    Most of the patterns we want to print require a nested loop (a loop inside a loop). For that, we first need to decide the outer and inner loops range. The range for both the loops can either be Hardcoded, or we can ask the user to enter the height, rows, or columns for the pattern.

    Step 2: Define the outer loop (i).

    The outer loop defines the number of rows, and it generally represents the height of the pattern.

    Step 3: Define the inner loop (j).

    The inner loop is responsible for printing the pattern data, and its range can be dynamic with each iteration of the outer loop.

    Step 4: Print pattern characters

    We print the pattern characters inside the inner loop (j).  As the Python print statement prints every new statement in the new line, we need to set the end argument value to an empty string '' or blank space ' ' . So that the inner loop can print all the characters in a single line for each row iteration of the outer loop.

    Step 5: Print a new line

    The inner loop prints the pattern in a single line, and to print the next row in the new line we need to write the print() statement at the end of the outer loop. Note: By practicing printing the patterns in Python, we can hone our Python skills and upgrade our understanding of logic building and nested loops. So let's get started with Print patterns in Python.

    1. Python program to Print Number patterns

    1.1 Print Pyramid pattern of numbers in Python

    Pattern

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

    Program

    rows = int(input("Enter the number of rows: "))
    
    for i in range(1,rows+1):
        for j in range(1,i+1):
            #print the number from 1 to i+1
            print(j, end=" ")
    
        #print the next row in new line
        print()

    loop structure

    outer loop i = 1 to rows+1
    inner loop j = 1 to i+1
    print = j
    Note: In the above program the nested loop j range depends on i value. For the first row i=0 , the inner loop j goes from 1 to 1 , and for the last row i=5 , it goes from 1 to 6 .

    1.2 Print Inverted Pyramid pattern of numbers in Python

    Pattern

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

    Program

    rows = int(input("Enter the number of rows: "))
    
    for i in range(rows,0,-1):
        
        for j in range(1,i+1):
            #print the number from 1 to i+1
            print(j, end=" ")
    
        #print the next row in new line
        print()

    loop structure

    outer loop i = rows to 0
    inner loop j = 1 to i+1
    print = j

    1.3 Print Inverted Pyramid pattern of the same numbers in Python

    Pattern

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

    Program

    rows = int(input("Enter the number of rows: "))
    
    for i in range(1, rows+1):
        
        for j in range((rows+1)-i):
            #print the number
            print(i, end=" ")
    
        #print the next row in new line
        print()

    loop structure

    outer loop i = 1 to rows+1
    inner loop j = 0 to (rows+1)-i
    print = i

    1.4 Print Inverted Pyramid pattern of row number in Python

    Pattern

    6 6 6 6 6 6 
    6 6 6 6 6 
    6 6 6 6 
    6 6 6 
    6 6 
    6

    Program

    rows = int(input("Enter the number of rows: "))
    
    for i in range(1, rows+1):
        
        for j in range((rows+1)-i):
            #print the number
            print(rows, end=" ")
    
        #print the next row in new line
        print()

    Loop structure

    outer loop i = 1 to rows+1
    inner loop j = 0 to (rows+1)-i
    print = rows

    1.5 Print Inverted Pyramid pattern of numbers in descending order in Python

    Now let's write a script that prints the inverted pyramid of reverse numbers.

    Pattern

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

    Program

    rows = int(input("Enter the number of rows: "))
    
    for i in range(1, rows+1):
        
        for j in range((rows+1)-i):
            #print the number
            print((rows+1)-i, end=" ")
    
        #print the next row in new line
        print()

    Loop Structure

    outer loop i = 1 to rows+1
    inner loop j = 0 to (rows+1)-i
    print = (rows+1)-i

    1.6 Print Pyramid pattern of reverse numbers in Python

    Pattern

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

    Program

    rows = int(input("Enter the number of rows: "))
    
    for i in range(1, rows+1):
        
        for j in range(i, 0, -1):
            #print the number
            print(j, end=" ")
    
        #print the next row in new line
        print()

    Loop structure

    outer loop i = 1 to rows+1
    inner loop j = i to 0
    print = j

    1.7 Print Inverted Pyramid pattern of reverse numbers in Python

    Pattern

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

    Program

    rows = int(input("Enter the number of rows: "))
    
    for i in range(1, rows+1):
        
        for j in range((rows+1)-i, 0, -1):
            #print the number
            print(j, end=" ")
    
        #print the next row in new line
        print()

    Loop Structure

    outer loop i = 1 to rows+1
    inner loop j = (rows+1)-i to 0
    print = j

    1.8 Print a triangle pattern of numbers in Python

    Pattern

             1
            1 2
           1 2 3
          1 2 3 4
         1 2 3 4 5
        1 2 3 4 5 6
       1 2 3 4 5 6 7
      1 2 3 4 5 6 7 8
     1 2 3 4 5 6 7 8 9
    1 2 3 4 5 6 7 8 9 10

    Program

    height = int(input("Enter Height of the triangle: "))
    
    for i in range(1, height+1):
    
        #inner loop to print spaces before number
        for k in range(height-i):
            print(" ", end="")
            
        #inner loop to print number
        for j in range(1, i+1):
            #print the number
            print(j, end=" ")
    
        #print the next row in new line
        print()

    Loop Structure

    outer loop i = 1 to height+1
    inner loop k (space) = 0 to height-i
    print =  " "
    inner loop j ( for numbers )= 1 to i+1
    print = j

    1.9 Print Pascal's triangle pattern of numbers in Python

    In a pascal's triangle , the top starts with 1, and each next row is formed by the addition of its upper two adjacent numbers.

    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 
       1 7 21 35 35 21 7 1 
      1 8 28 56 70 56 28 8 1 
     1 9 36 84 126 126 84 36 9 1

    Program

    height = int(input("Enter Height of the triangle: "))
    
    #function to get the number for pascal's triange
    def pascal_number(a,b):
        num=1
        if b>a-b:
            b= a-b
        for p in range(0, b):
            num = num*(a-p)
            num = num //(p+1)
        return num
    
    #outer loop    
    for i in range(height):
    
        #inner loop to print spaces before number
        for k in range(height-i):
            print(" ", end="")
            
        #inner loop to print number
        for j in range(0, i+1):
            #print the number
            print(pascal_number(i, j), end=" ")
    
        #print the next row in new line
        print()

    Loop Structure

    outer loop i = 0 to height
    inner loop k (space) = 0 to height-i
    print =  " "
    inner loop j ( for numbers )= 0 to i+1
    print = pascal_number(i, j)

    1.10 Print square pattern of numbers in Python

    Pattern

    0 1 2 3 4 
    0 1 2 3 4 
    0 1 2 3 4 
    0 1 2 3 4 
    0 1 2 3 4

    Program

    length = int(input("Enter length of the square: "))
    
    #outer loop
    for i in range(length):
        #inner loop
        for j in range(length):
            #print the number
            print(j, end=" ")
    
        #print the new line
        print()

    Loop Structure

    outer loop i = 0 to length
    inner loop j ( for numbers )= 0 to length
    print = j

    1.11 Print multiplication Pyramid pattern of numbers in Python

    Pattern

    1 
    2 4 
    3 6 9 
    4 8 12 16 
    5 10 15 20 25

    Program

    rows = int(input("Enter number of rows: "))
    
    #outer loop
    for i in range(1, rows+1):
        #inner loop
        for j in range(1, i+1):
            #print the number
            print(i*j, end=" ")
    
        #print the new line
        print()
    

    Loop Structure

    outer loop i = 1 to rows+1
    inner loop j ( for numbers )= 1 to i+1
    print = i*j

    2. Python program to Print star/asterisk (*) patterns

    In the above section, we see how can we print the number pattern in Python. Now let's write the different Python code snippet to print the star's pyramid, triangle, and rectangle patterns. The code logic for Pyramid, triangle, and rectangle will remain the same as we have written in the above section, the only thing that will change is the print statement value.

    2.1 Print the half Pyramid pattern of stars in Python

    Pattern

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

    Program

    rows = int(input("Enter number of rows: "))
    
    #outer loop
    for i in range(rows):
        #inner loop
        for j in range(i+1):
            #print star
            print("*", end=" ")
    
        #print the new line
        print()

    Loop Structure

    outer loop i = 0 to rows
    inner loop j ( for stars )= 0 to i+1
    print = "*"

    2.2 Print the Equilateral triangle Pyramid pattern of stars in Python

    Pattern

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

    Program

    height = int(input("Enter Height of the triangle: "))
    
    for i in range(1, height+1):
    
        #inner loop to print spaces before stars
        for k in range(height-i):
            print(" ", end="")
            
        #inner loop to print stars
        for j in range(1, i+1):
            #print the star
            print("*", end=" ")
    
        #print the next row in new line
        print()

    Loop Structure

    outer loop i = 1 to height+1
    inner loop k (space) = 0 to height-i
    print =  " "
    inner loop j ( for stars )= 1 to i+1
    print = "*"

    2.3 Print the right angle triangle Pyramid pattern of stars in Python

    Pattern

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

    Program

    height = int(input("Enter Height of the triangle: "))
    
    for i in range(1, height+1):
    
        #inner loop to print spaces before stars
        for k in range(height-i,0, -1):
            print(" ", end="")
            
        #inner loop to print star
        for j in range(1, i+1):
            #print the star
            print("*", end="")
    
        #print the next row in new line
        print()

    Loop Structure

    outer loop i = 1 to height+1
    inner loop k (space) = height-i to 0
    print =  " "
    inner loop j ( for stars )= 1 to i+1
    print = "*"

    2.4 Print the inverted right angle triangle Pyramid pattern of stars in Python

    Pattern

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

    Program

    height = int(input("Enter Height of the triangle: "))
    
    for i in range(height):
            
        #inner loop to print stars
        for j in range(height-i, 0, -1):
            #print the star
            print("*", end="")
    
        #print the next row in new line
        print()

    Loop Structure

    outer loop i = 0 to height
    inner loop j ( for stars )= height-i to 0
    print = "*"

    2.5 Print the Inverted Pyramid/Equilateral triangle pattern of stars in Python

    Pattern

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

    Program

    height = int(input("Enter Height of the triangle: "))
    
    for i in range(height):
    
        #inner loop to print spaces before stars
        for k in range(i):
            print(" ", end="")
            
        #inner loop to print stars
        for j in range(height-i):
            #print the star
            print("*", end=" ")
    
        #print the next row in new line
        print()

    Loop Structure

    outer loop i =0 to height
    inner loop k (space) = 0 to i
    print =  " "
    inner loop j ( for stars )=0 to height-i
    print = "* "

    2.6 Print the Inverted mirror right angle triangle pattern of stars in Python

    Pattern

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

    Program

    height = int(input("Enter Height of the triangle: "))
    
    for i in range(height):
    
        #inner loop to print spaces before stars
        for k in range(i):
            print(" ", end="")
            
        #inner loop to print stars
        for j in range(height-i):
            #print the star
            print("*", end="")
    
        #print the next row in new line
        print()

    Loop Structure

    outer loop i =0 to height
    inner loop k (space) = 0 to i
    print =  " "
    inner loop j ( for stars )=0 to height-i
    print = "*"

    2.7 Print the two pyramid patterns of stars in Python

    Pattern

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

    Program

    height = int(input("Enter Height of the one Pyramid: "))
    
    #first pyramid
    for i in range(1, height+1):
        #inner loop to print stars
        for j in range(i):
            #print the star
            print("*", end="")
        #print the next row in new line
        print()
    
    #print the new line
    print()
    #mirror of pyramid
    for i in range(height):
        #inner loop to print stars
        for j in range(height-i):
            #print the star
            print("*", end="")
        #print the next row in new line
        print()

    Loop Structure

    First Pyramid outer loop i =1 to height+1
    inner loop j ( for stars )=0 to i
    print = "*"
    Mirror Pyramid outer loop i =0 to height
    inner loop j ( for stars )=0 to height-i
    print = "*"

    2.8 Print the Right start patterns of stars in Python

    Pattern

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

    Program

    height = int(input("Enter Height of the one Arrow: "))
    
    #upper part of the arrow
    for i in range(1, height+1):
        #inner loop to print stars
        for j in range(i):
            #print the star
            print("*", end="  ")
        #print the next row in new line
        print()
    
    #lower part of the arrow
    for i in range(1,height):
        #inner loop to print stars
        for j in range(height-i):
            #print the star
            print("*", end="  ")
        #print the next row in new line
        print()

    Loop Structure

    Upper Part outer loop i =1 to height+1
    inner loop j ( for stars )=0 to i
    print = "*  "
    Lower Part outer loop i =1 to height
    inner loop j ( for stars )=0 to height-i
    print = "*  "

    2.9 Print the Left start patterns of stars in Python

    Pattern

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

    Program

    height = int(input("Enter Height of the one Arrow: "))
    
    #upper part of the arrow
    for i in range(1, height+1):
    
        #space before the start patterns
        for k in range(height-i):
            print(" ", end ="")
        #inner loop to print stars
        for j in range(i):
            #print the star
            print("*", end="")
        #print the next row in new line
        print()
    
    #lower part of the arrow
    for i in range(1,height):
    
        #space before the start patterns
        for k in range(i):
            print(" ", end ="")
    
        
        #inner loop to print stars
        for j in range(height-i):
            #print the star
            print("*", end="")
        #print the next row in new line
        print()

    Loop Structure

    Upper Part outer loop i =1 to height+1
    inner loop k(for space) = 0 to height-i
    print = " ", end = ""
    inner loop j ( for stars )=0 to i
    print = "*" end = ""
    Lower Part outer loop i =1 to height
    inner loop k(for space) = 0 to i
    print = " ", end = ""
    inner loop j ( for stars )=0 to height-i
    print = "*" end=""

    2.10 Print the Sandglass patterns of stars in Python

    The sandglass pattern of stars can be generated with the help of Inverted Equilateral triangle and its mirror.

    Pattern

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

    Program

    height = int(input("Enter Height of the one sandglass: "))//2
    
    
    #upper part of the sandglass
    for i in range(height):
    
        #inner loop to print spaces before stars
        for k in range(i):
            print(" ", end="")
            
        #inner loop to print stars
        for j in range(height-i):
            #print the star
            print("*", end=" ")
    
        #print the next row in new line
        print() 
    
    #lower part of the sandglass
    for i in range(1, height+1):
    
        #inner loop to print spaces before stars
        for k in range(height-i):
            print(" ", end="")
            
        #inner loop to print stars
        for j in range(1, i+1):
            #print the star
            print("*", end=" ")
    
        #print the next row in new line
        print()

    Loop Structure

    The upper part of the sandglass pattern outer loop i =0 to height
    inner loop k (space) = 0 to i
    print =  " " , end =""
    inner loop j ( for stars )=0 to height-i
    print = "*", end =" "
    The Lower part of the sandglass pattern outer loop i = 1 to height+1
    inner loop k (space) = 0 to height-i
    print =  " ", end = ""
    inner loop j ( for stars )= 1 to i+1
    print = "*" end =" "

    2.11 Print the Diamond-shaped patterns of stars in Python

    To print the diamond-shaped pattern, we can build the upper part with the equilateral triangle and the lower part with its mirror.

    Pattern

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

    Program

    height = int(input("Enter Height of the Diamond: "))//2
    
    #upper part of the Diamond
    for i in range(1, height+1):
    
        #inner loop to print spaces before stars
        for k in range(height-i):
            print(" ", end="")
            
        #inner loop to print stars
        for j in range(1, i+1):
            #print the star
            print("*", end=" ")
    
        #print the next row in new line
        print()
    
    #lower part of the Diamond
    for i in range(height):
    
        #inner loop to print spaces before stars
        for k in range(i):
            print(" ", end="")
            
        #inner loop to print stars
        for j in range(height-i):
            #print the star
            print("*", end=" ")
    
        #print the next row in new line
        print()

    Loop Structure

    The upper part of the sandglass pattern outer loop i = 1 to height+1
    inner loop k (space) = 0 to height-i
    print =  " ", end = ""
    inner loop j ( for stars )= 1 to i+1
    print = "*" end =" "
    The Lower part of the sandglass pattern outer loop i =0 to height
    inner loop k (space) = 0 to i
    print =  " " , end =""
    inner loop j ( for stars )=0 to height-i
    print = "*", end =" "

    2.12 Print the Hollow Diamond-shaped patterns of stars in Python

    Pattern

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

    Program

    height = int(input("Enter Height of the Diamond: "))
    
    #upper part of the Hollow Diamond
    for i in range(1, height+1):
    
        #inner loop to print spaces before stars
        for k in range(height-i):
            print(" ", end="")
            
        #inner loop to print stars
        for j in range(1, i+1):
            #print the star for first and last value
            if j==1 or j==i: 
                print("*", end=" ")
            else:
                print(" ", end=" ")
    
        #print the next row in new line
        print()
    
    #lower part of the Hollow Diamond
    for i in range(height):
    
        #inner loop to print spaces before stars
        for k in range(i):
            print(" ", end="")
            
        #inner loop to print stars
        for j in range(height-i):
            #print the star for first and last value
            if j==0 or j==(height-i-1): 
                #print the star
                print("*", end=" ")
            else:
                print(" ", end=" ")
    
        #print the next row in new line
        print()

    Loop Structure

    The upper part of the Hollow Diamond outer loop j: 1 to height+1
    the inner loop k (space) : 0 to height-i
    print = " ", end =""
    inner loop j (stars): 1 to i+1
    print: if j==1 or j==i print star "*" end=" ", else print space " ", end =" "
    The lower part of the Hollow Diamond outer loop j: 0 to height
    the inner loop k (space) : 0 toi
    print = " ", end =""
    inner loop j (stars): 0to height-i-1
    print: if j==0 or j==height-i-1 print star "*" end=" ", else print space " ", end =" "

    3. Python program to Print Alphabets and letters pattern

    When we want to print a pattern of alphabets in Python with the help of a nested loop we always consider the ASCII code. The ASCII of UpperCase Aphabets starts at 65 (A) and ends at 90 (Z). And the lower case from 97(a) to 122 (z). To convert an ASCII code number to an alphabet character we can use the Python chr() function.

    3.1 Print the Pyramid patterns of Upper Case Alphabets in Python

    Pattern

    A 
    B C 
    D E F 
    G H I J 
    K L M N O 
    P Q R S T U 
    V W X Y Z A B

    Program

    #ascii value of A
    ascii_value = 65
    
    for i in range(7):
        for j in range(i+1):
            #convert the ascii into character 
            char = chr(ascii_value)
            
            #print the character
            print(char, end= " ")
    
            #increase the value of ascii for next chracter
            ascii_value +=1
    
            #if ascii value is greater than Z
            #set back ascii value to A 65
            if ascii_value > 90:
                ascii_value=65
            
        #print the new line
        print() 

    Loop Structure

    Outer Loop i: 0 to 7
    Inner Loop j : 0 to i+1
    Print: chr(ascii_value)

    3.2 Print all the subwords of a word in Python

    Pattern

    T
    Te
    Tec
    Tech
    TechG
    TechGe
    TechGee
    TechGeek
    TechGeekB
    TechGeekBu
    TechGeekBuz
    TechGeekBuzz

    Program

    word = "TechGeekBuzz"
    sub = " "
    for i in word:
        sub += i
        print(sub)

    Loop Structure

    Iterate over the word Add every character of the word to sub and print it.

    3.3 Print a Equilateral triangle pattern of characters/alphabets in Python

    Pattern

          A 
         B C 
        D E F 
       G H I J 
      K L M N O 
     P Q R S T U 
    V W X Y Z A B

    Program

    height =7
    
    #ascii value of A
    ascii_value =65
    
    for i in range(1, height+1):
    
        #inner loop to print spaces before alphabets
        for k in range(height-i):
            print(" ", end="")
            
        #inner loop to print number
        for j in range(1, i+1):
            #print the alphabet
            print(chr(ascii_value), end=" ")
    
            #increment the ascii_value for next alphabet
            ascii_value+=1
    
            if ascii_value>90:
                ascii_value=65
    
        #print the next row in new line
        print()

    Loop Structure

    outer loop i = 1 to height+1
    inner loop k (space) = 0 to height-i
    print =  " ", end= " "
    inner loop j ( for numbers )= 1 to i+1
    print = chr(ascii_vallue), end =" "

    4. Bonus Patterns (Some Miscellaneous Patterns in Python)

    Let's also see some examples of Python patterns that contain more logic inside the nested loop.

    4.1 Print a Pyramid pattern of horizontal number tables in Python

    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 
    9 18 27 36 45 54 63 72 81 
    10 20 30 40 50 60 70 80 90 100

    Program

    height = int(input("Enter Height of the Pyramid: "))
    
    #outer loop
    for i in range(1, height+1):
        #inner loop
        for j in range(1,i+1):
            print(i*j, end=" ")
        #print a new line
        print()

    Loop Structure

    Outer Loop i: 1 to height+1
    Inner Loop j : 1 to i+1
    print: i*j   end =" "

    4.2 Print a Pant pattern of numbers in Python

    Pattern

    8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 
    
    8 7 6 5 4 3 2     2 3 4 5 6 7 8 
    
    8 7 6 5 4 3         3 4 5 6 7 8 
    
    8 7 6 5 4             4 5 6 7 8 
    
    8 7 6 5                 5 6 7 8 
    
    8 7 6                     6 7 8 
    
    8 7                         7 8 
    
    8                             8

    Program

    height = int(input("Enter Height of the Pyramid: "))
    
    #outer loop 
    for i in range(0, height):
        #inner loop to print the numbers before space
        for j in range(height - 1, i, -1):
            print(j,  end=" ")
            
        #inner loop to print the spaces
        for k in range(i):
            print("    ", end="")
    
        #inner loop to numbers after space
        for l in range(i + 1, height):
            print(l,  end=" ")
        print("\n")

    Loop Structure

    Outer Loop i: 0 to height
    Inner loop j (left side numbers): height-1 to i
    Print: j end = " "
    Inner Loop k (for space): 0 to i
    Print: "    ", end =""
    Inner Loop l (for right-side numbers): i+1 to height
    Print: l , end =" "

    Conclusion

    In this Python Pattern tutorial, we discuss the most important and widely asked Python pattern interview problems. Printing a pattern is very simple, we only need to care about the two most important things. The first is how many rows or what the pattern's height would be, and the second is the loop logic for the inner loop that is printing the data for the pattern.

    After you practice all the patterns we have covered in this article you will build a solid understanding of printing patterns using Python nested loop If you have a new pattern in your mind that we have not mentioned in the above article, you can share that in the comment section.

    People are also reading:

    Leave a Comment on this Post

    0 Comments