Thursday, December 26, 2024
Google search engine
HomeLanguagesPrograms for printing pyramid patterns in Python

Programs for printing pyramid patterns in Python

 

Patterns can be printed in python using simple for loops. First outer loop is used to handle the number of rows and the Inner nested loop is used to handle the number of columns. Manipulating the print statements, different number patterns, alphabet patterns, or star patterns can be printed. 

Some of the Patterns are shown in this article. 

  • Simple pyramid pattern

Python3




# Python 3.x code to demonstrate star pattern
 
# Function to demonstrate printing pattern
def pypart(n):
     
    # outer loop to handle number of rows
    # n in this case
    for i in range(0, n):
     
        # inner loop to handle number of columns
        # values changing acc. to outer loop
        for j in range(0, i+1):
         
            # printing stars
            print("* ",end="")
      
        # ending line after each row
        print("\r")
 
# Driver Code
n = 5
pypart(n)


Output

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

Time Complexity: O(n2),This algorithm has a time complexity of O(n2). This is because the outer loop runs n times and the inner loop runs n times for each iteration of the outer loop, resulting in a total of n2 iterations.

Space Complexity: O(1),This algorithm has a space complexity of O(1). This is because there are no auxiliary data structures (such as arrays or lists) used, only primitives and constants.

Approach 2: Using List in Python 3, this could be done in a simpler way

Python




# Python 3.x code to demonstrate star pattern
 
# Function to demonstrate printing pattern
def pypart(n):
    myList = []
    for i in range(1,n+1):
        myList.append("*"*i)
    print("\n".join(myList))
 
# Driver Code
n = 5
pypart(n)


Output

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

Time Complexity: O(n) ,The time complexity of this code is O(n), as it iterates through a loop of size n, where n is the number of stars to be printed.

Space Complexity: O(n) ,The space complexity of this code is O(n), as the myList array is used to store the string of stars, which is of size n.

Approach 3: Using recursion

Python3




#python3 code to print pyramid pattern using recursion
def pypart(n):
    if n==0:
        return
    else:
        pypart(n-1)
        print("* "*n)
  
# Driver Code
n = 5
pypart(n)
#this code is contributed by Shivesh Kumar Dwivedi


Output

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

Approach 4: Using while loop 

Python3




# python3 code to print pyramid pattern using while loop
 
# input
n=5
 
i=1;j=0
# while loop check the condition until the
# condition become false. if it is true then
# enter in to loop and print the pattern
while(i<=n):
    while(j<=i-1):
        print("* ",end="")
        j+=1
     # printing next line for each row
    print("\r")
    j=0;i+=1
 
     
     
 # this code is contributed by gangarajula laxmi


Output

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

The time complexity of the given code is O(n^2), where n is the input size.

 Space complexity is O(1).

Approach 5: Using for loops

Python3




n = 5
for i in range(0, n):
    for j in range(0, i+1):
        print("*", end=" ")
    print()


Output

* 
* * 
* * * 
* * * * 
* * * * * 
  • After 180 degrees rotation

Python3




# Python 3.x code to demonstrate star pattern
 
# Function to demonstrate printing pattern
def pypart2(n):
     
    # number of spaces
    k = 2*n - 2
 
    # outer loop to handle number of rows
    for i in range(0, n):
     
        # inner loop to handle number spaces
        # values changing acc. to requirement
        for j in range(0, k):
            print(end=" ")
     
        # decrementing k after each loop
        k = k - 2
     
        # inner loop to handle number of columns
        # values changing acc. to outer loop
        for j in range(0, i+1):
         
            # printing stars
            print("* ", end="")
     
        # ending line after each row
        print("\r")
 
# Driver Code
n = 5
pypart2(n)


Output

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

Optimized Solution:

Here, we have to print a space (height – row) times and then print “*” row times.

For example: let height of the pyramid is 5

then , on the row number 1 we print blank space 4 times(that is 5-1 or height -row)

and then we print star 1 time(that is row times) and then a new line

then , on the row number 2 we print blank space 3 times(that is 5-2 or height -row)

and then we print star 2 times (that is row times) and then a new line

and so on….

Method: Using while loop

Python3




# python3 code to print pyramid pattern using while loop
n=5;i=0
while(i<=n):
  print(" " * (n - i) +"*" * i)
  i+=1


Output

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

Method: Using for loop

Python3




#python3 code to implement above approach
height = 5
for row in range(1, height+ 1):
    print(" " * (height - row) +"*" * row)
#this code is contributed by Shivesh kumar dwivedi


Output

    *
   **
  ***
 ****
*****
  • Printing Triangle

Python3




# Python 3.x code to demonstrate star pattern
 
# Function to demonstrate printing pattern triangle
def triangle(n):
     
    # number of spaces
    k = n - 1
 
    # outer loop to handle number of rows
    for i in range(0, n):
     
        # inner loop to handle number spaces
        # values changing acc. to requirement
        for j in range(0, k):
            print(end=" ")
     
        # decrementing k after each loop
        k = k - 1
     
        # inner loop to handle number of columns
        # values changing acc. to outer loop
        for j in range(0, i+1):
         
            # printing stars
            print("* ", end="")
     
        # ending line after each row
        print("\r")
 
# Driver Code
n = 5
triangle(n)


Output

    * 
   * * 
  * * * 
 * * * * 
* * * * * 
  • Number Pattern

Python3




# Python 3.x code to demonstrate star pattern
 
# Function to demonstrate printing pattern of numbers
def numpat(n):
     
    # initialising starting number
    num = 1
 
    # outer loop to handle number of rows
    for i in range(0, n):
     
        # re assigning num
        num = 1
     
        # inner loop to handle number of columns
            # values changing acc. to outer loop
        for j in range(0, i+1):
         
                # printing number
            print(num, end=" ")
         
            # incrementing number at each column
            num = num + 1
     
        # ending line after each row
        print("\r")
 
# Driver code
n = 5
numpat(n)


Output

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
  • Numbers without reassigning

Python3




# Python 3.x code to demonstrate star pattern
 
# Function to demonstrate printing pattern of numbers
def contnum(n):
     
    # initializing starting number
    num = 1
 
    # outer loop to handle number of rows
    for i in range(0, n):
     
        # not re assigning num
        # num = 1
     
        # inner loop to handle number of columns
        # values changing acc. to outer loop
        for j in range(0, i+1):
         
            # printing number
            print(num, end=" ")
         
            # incrementing number at each column
            num = num + 1
     
        # ending line after each row
        print("\r")
 
n = 5
 
# sending 5 as argument
# calling Function
contnum(n)


Output

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
  • Character Pattern

Python3




# Python 3.x code to demonstrate star pattern
 
# Function to demonstrate printing pattern of alphabets
def alphapat(n):
     
    # initializing value corresponding to 'A'
    # ASCII value
    num = 65
 
    # outer loop to handle number of rows
    # 5 in this case
    for i in range(0, n):
     
        # inner loop to handle number of columns
        # values changing acc. to outer loop
        for j in range(0, i+1):
         
            # explicitly converting to char
            ch = chr(num)
         
            # printing char value
            print(ch, end=" ")
     
        # incrementing number
        num = num + 1
     
        # ending line after each row
        print("\r")
 
# Driver Code
n = 5
alphapat(n)


Output

A 
B B 
C C C 
D D D D 
E E E E E 
  • Continuous Character pattern

Python3




# Python code 3.x to demonstrate star pattern
 
# Function to demonstrate printing pattern of alphabets
 
 
def contalpha(n):
 
    # initializing value corresponding to 'A'
    # ASCII value
    num = 65
 
 
    # outer loop to handle number of rows
- for i in range(0, n):
 
    # inner loop to handle number of columns
    # values changing acc. to outer loop
    for j in range(0, i+1):
 
        # explicitly converting to char
        ch = chr(num)
 
        # printing char value
        print(ch, end=" ")
 
        # incrementing at each column
        num = num + 1
 
    # ending line after each row
    print("\r")
 
# Driver code
n = 5
contalpha(n)


Output:

A 
B C 
D E F 
G H I J 
K L M N O
  • Character Triangle

Python3




n = 5
alph = 65
for i in range(0, n):
    print(" " * (n-i), end=" ")
    for j in range(0, i+1):
        print(chr(alph), end=" ")
        alph += 1
    alph = 65
    print()


Output

      A 
     A B 
    A B C 
   A B C D 
  A B C D E 

This article is contributed by Manjeet Singh(S.Nupur). If you like Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.

Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments