Programs for printing pyramid patterns in Python - GeeksforGeeks

18 Dec, 2021
Patterns can be printed in python using simple for loops. First outer loop is used to handle number of rows and 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 patterndef 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 Coden = 5pypart(n)
* 
* * 
* * * 
* * * * 
* * * * * 
Another Approach: 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 patterndef pypart(n):    myList = []    for i in range(1,n+1):        myList.append("*"*i)    print("\n".join(myList)) # Driver Coden = 5pypart(n)
*
**
***
****
*****
After 180 degree rotation
Python3
# Python 3.x code to demonstrate star pattern # Function to demonstrate printing patterndef 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 Coden = 5pypart2(n)
        * 
      * * 
    * * * 
  * * * * 
* * * * * 
Printing Triangle
Python3
# Python 3.x code to demonstrate star pattern # Function to demonstrate printing pattern triangledef 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 Coden = 5triangle(n)
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
Number Pattern
Python3
# Python 3.x code to demonstrate star pattern # Function to demonstrate printing pattern of numbersdef 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 coden = 5numpat(n)
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 numbersdef 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 Functioncontnum(n)
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 alphabetsdef 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 Coden = 5alphapat(n)
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 alphabetsdef  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 coden = 5contalpha(n)
Output:
A 
B C 
D E F 
G H I J 
K L M N O
This article is contributed by Manjeet Singh(S.Nupur) . If you like GeeksforGeeks 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 GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
  
aneeshgarray2000
kskaman
simranarora5sos
pattern-printing
Python Pattern-printing
Python
School Programming
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java