Program to Create a Pyramid Pattern in python

In this tutorial, you will learn how to create pyramid pattern in Python programming language. You will learn to create 13 different patterns using a nested for loop that displays the desired output.

The pyramid patterns that you are going to program are as follows.

  1. Right-aligned pyramid
  2. Left-aligned pyramid
  3. Full pyramid
  4. Inverted pyramid
  5. Half pyramid using numbers
  6. Half pyramid using alphabets
  7. Pascal’s triangle
  8. Diamond pattern
  9. Hourglass pattern
  10. Zigzag pattern
  11. Hollow inverted pyramid

Right-aligned pyramid

n = int(input("Enter the number of rows: "))
for i in range(1, n+1):
    print(" "*(n-i) + "*"*i)
  • The right-aligned pyramid program prints a right-aligned pyramid pattern with asterisks as the number of rows of a pyramid is input by the user.
  • The program uses a for loop to iterate through the range from 1 to row + 1.
  • For each iteration of the loop, the program prints a string consisting of two parts:
  • The first part of the string is a number of spaces equal to the difference between the total number of rows n and the current row number i.
  • The second part of the string is a number of asterisks equal to the current row number i.
  • The print statement within the loop prints each of these strings on a separate line.
  • Once the loop is finished, the program ends.
  • For example, if the user enters 5 for the number of rows, the output would be

Output

C:\Users\user\tutcoach.com>python program.py
Enter the number of rows: 5    
    *
   **
  ***
 ****
*****

Left aligned pyramid

The left-aligned pyramid is just the mirror of the right-aligned pyramid, the source code for the left-aligned pyramid in Python is given below for the user input row value of 5.

n = int(input("Enter the number of rows: "))
for i in range(1, n+1):
    print("*"*i)

Output

C:\Users\user\tutcoach.com>python program.py
Enter the number of rows: 5
*
**
***
****
*****

Full pyramid

In this example you will learn how to print a full pyramid pattern in Python, to print full pyramid we perform the following steps.

  1. First, we ask the user to enter the number of rows they want in the pyramid and store the input as an integer in the variable n.
  2. Then we use a for loop to iterate through the range from 1 to n+1 ie up to row+1.
  3. For each iteration of the loop, we print a string consisting of two parts:
    • The first part of the string " "*(n-i) is a number of spaces equal to the difference between the total number of rows n and the current row number i.
    • The second part of the string "* "*i is a number of asterisks equal to twice the current row number i minus 1, separated by a single space.
  4. The print statement within the loop prints each of these strings on a separate line.
  5. Once the loop is finished, the program ends.
n = int(input("Enter the number of rows: "))
for i in range(1, n+1):
    print(" "*(n-i) + "* "*i)
C:\Users\user\tutcoach.com>python program.py
Enter the number of rows: 10
         *
        * *
       * * *
      * * * *
     * * * * *
    * * * * * *
   * * * * * * *
  * * * * * * * *
 * * * * * * * * *
* * * * * * * * * *

Inverted full pyramid pattern.

In this program you will learn how to print an inverted full pyramid pattern in python, to print an inverted full pyramid pattern we will use a loop by counting in reverse.

For each iteration of the loop here consists of two parts:

  • The first part of the string is a number of spaces equal to the difference between the total number of rows n and the current row number i.
  • The second part of the string is a number of asterisks equal to twice the current row number i minus 1, separated by a single space

The source code for the full pyramid pattern is given below.

Source code

n = int(input("Enter the number of rows: "))
for i in range(n, 0, -1):
    print(" "*(n-i) + "* "*i)

Output

C:\Users\user\tutcoach.com>python program.py
Enter the number of rows: 10
* * * * * * * * * *
 * * * * * * * * *
  * * * * * * * *
   * * * * * * *
    * * * * * *
     * * * * *
      * * * *
       * * *
        * *
         *

Half pyramid in Python using numbers

In this program you will learn how to print a half pyramid in python using numbers. First, we ask a user to input a row of pyramids. After that, we use an outer loop to iterate through the range from 1 to row + 1. Again we use an inner loop to print the value of a counter variable j with a space character, and end is used to create a space between the numbers. the last print function is called to create a new line for every row.

Source code

n = int(input("Enter the number of rows: "))
for i in range(1, n+1):
    for j in range(1, i+1):
        print(j, end=" ")
    print()

Output

C:\Users\user\tutcoach.com>python program.py
Enter the number of rows: 10
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

Half pyramid in Python using alphabets

Here you will learn to write a program to print half pyramid using alphabets, to print half pyramid we will use nested for loops as shown below.

Source code

n = int(input("Enter the number of rows: "))
for i in range(65, 65+n):
    for j in range(65, i+1):
        print(chr(j), end=" ")
    print()

Output

C:\Users\user\tutcoach.com>python program.py
Enter the number of rows: 10
A
A B
A B C
A B C D
A B C D E
A B C D E F
A B C D E F G
A B C D E F G H
A B C D E F G H I
A B C D E F G H I J

Pascal’s triangle

  • In this program you will learn to create pascal’s triangle in python.Pascal’s triangle is a mathematical concept discovered by French mathematician Blaise Pascal.
  • Pascal’s triangle is a triangular array of numbers in which each number is the sum of the two numbers directly above it.
  • The first and last numbers in each row of the triangle are always 1, and the remaining numbers in the row are determined by adding the two numbers above them.
  • Pascal’s triangle usages in the real world include the areas of mathematics, including combinatorics and probability theory, and it has applications in fields such as computer science, physics, and engineering.

Properties of pascal’s triangle are

  • Pascal’s triangle is a symmetric arrangement of numbers
  • Each row of the triangle is determined by adding the two numbers in the row above it.
  • Pascal’s triangle contains the coefficients of the binomial expansion, so pascal’s triangle is used to calculate of powers of binomials.
  • Pascal’s triangle is symmetry across the center and diagonal lines.
  • Pascal’s triangle is also used in combinatorics, probability theory, and fractal geometry, among other applications.

The above Properties of pascal’s triangle are very important that are used to solve the problems.

The formula for Pascal’s triangle is given by:

C(n, k) = C(n-1, k-1) + C(n-1, k) where C(n, k) represents the value at row n and column k in Pascal’s triangle.

To find the value at row 3, column 2 in Pascal’s triangle.

Using the formula C(n, k) = C(n-1, k-1) + C(n-1, k), we can calculate the value as follows:

C(3, 2) = C(2, 1) + C(2, 2)

C(3, 2) = (C(1, 0) + C(1, 1)) + (C(1, 1) + C(1, 2))

C(3, 2) = (1 + 1) + (1 + 0)

C(3, 2) = 3

computation of pascal’s triangle value using pascal’s formula.

Therefore, the value at row 3, column 2 in Pascal’s triangle is 3.

Source code

Here we have defined a Python function def pascals_triangle(rows): to generate Pascal’s triangle using nested for loops. The function takes input from the user for the number of rows to be printed in Pascal’s triangle.

Here we have used a nested loop to generate a Pascal’s triangle. The outside loop is iterated through each row of the triangle, while the inner loop is used to print the values of each row.

The num_rows variable is used to store the user input for the number of rows to be printed in the triangle.

def pascals_triangle(rows):
    coef = 1

    for i in range(1, rows+1):
        for space in range(1, rows-i+1):
            print(" ",end="")
        for j in range(0, i):
            if j==0 or i==0:
                coef = 1
            else:
                coef = coef * (i - j)//j
            print(coef, end = " ")
        print()

# Prompt user for input
num_rows = int(input("Enter the number of rows for Pascal's triangle: "))

# Call the function with user input as argument
pascals_triangle(num_rows)
C:\Users\user\tutcoach.com>python program.py
Enter the number of rows for Pascal's triangle: 10
         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

Diamond pattern in python

In this program you will learn how to create a diamond pattern in python, here we have utilized the two python for loop print the upper and lower part of a diamond

Source code

n = int(input("Enter the number of rows: "))
for i in range(1, n+1):
    print(" "*(n-i) + "* "*i)
for i in range(n-1, 0, -1):
    print(" "*(n-i) + "* "*i)

Output

C:\Users\user\tutcoach.com>python program.py
Enter the number of rows: 4
   *
  * *
 * * *
* * * *
 * * *
  * *
   *

Hourglass pattern in Python

To print the hourglass pattern in python, you can use two for loop as shown below in the source code.

n = int(input("Enter the number of rows: "))
for i in range(n, 0, -1):
    print(" "*(n-i) + "* "*(i))
for i in range(1, n+1):
    print(" "*(n-i) + "* "*(i))

Output


C:\Users\user\tutcoach.com>python program.py
Enter the number of rows: 10
* * * * * * * * * *
 * * * * * * * * *
  * * * * * * * *
   * * * * * * *
    * * * * * *
     * * * * *
      * * * *
       * * *
        * *
         *
         *
        * *
       * * *
      * * * *
     * * * * *
    * * * * * *
   * * * * * * *
  * * * * * * * *
 * * * * * * * * *
* * * * * * * * * *

Zigzag pattern

To print a zigzag pattern in python we use nested for loop and conditional if else statements to print the pattern. The source code is given below.

Source code

n = int(input("Enter the number of rows: "))
for i in range(1, n+1):
    for j in range(1, n+1):
        if i == j or j == n-i+1:
            print("*", end="")
        else:
            print(" ", end="")
    print()

Output

Enter the number of rows: 5
*   *
 * *
  *
 * *
*   *

Hollow inverted pyramid pattern

In this example you will see how to print hollow inverted pyramid star pattern in python, to print the hollow inverted pyramid pattern we have used for loop and conditional if-else statements.

Source Code

n = int(input("Enter the number of rows: "))
for i in range(n, 0, -1):
    if i == n or i == 1:
        print("*"*i)
    else:
        print("*" + " "*(i-2) + "*")

Output

C:\Users\user\tutcoach.com>python program.py
Enter the number of rows: 5
*****
*  *
* *
**
*

Floyd’s Triangle

In this program you will see how to print Floyd’s triangle using python. to program a Floyd’s triangle program we have used nested for loop inside a function.

Source code

def print_floyds_triangle(rows):
    number = 1
    for i in range(1, rows+1):
        for j in range(1, i+1):
            print(number, end=" ")
            number += 1
        print("")

rows = int(input("Enter the number of rows for Floyd's Triangle: "))
print_floyds_triangle(rows)

The output of Floyd’s Triangle program

C:\Users\user\tutcoach.com>python program.py
Enter number of rows: 10
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55

In programming, printing patterns is a common exercise to improve logical thinking and problem-solving skills:

  • Right Triangle Pattern
  • Inverted Right Triangle Pattern
  • Pyramid Pattern
  • Inverted Pyramid Pattern
  • Half Diamond Pattern
  • Diamond Pattern
  • Hollow Diamond Pattern
  • Floyd’s Triangle Pattern
  • Pascal’s Triangle Pattern

Each pattern has its unique way of printing and requires different types of loops and conditions. By practicing these patterns, one can improve their programming skills and get a better understanding of nested loops and control statements.

Overall, printing patterns is a great way to practice problem-solving and logical thinking in programming, and mastering them can lead to better programming skills and the ability to tackle more complex problems.

Leave a Comment