×

Programs for Printing Pyramid Patterns in Python

<!-- wp:paragraph -->
<p>Python supports printing patterns using basic for loops. The number of rows is handled by the first outer loop, while the number of columns is handled by the inner nested loop. Different number patterns, letter patterns, or star patterns can be printed by manipulating the print statements.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let’s discuss some types of patterns with the code in this tutorial.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>1.Basic simple Pyramid:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>Code:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>def pypart(n):
    for a in range(0, m):
        # declaring inner loop to handle number of columns
        # values changing w.r.t to outer loop
        for k in range(0, a+1):
            # stars to be printed
         #   ‘ * ’ is used for printing
            print("* ",end="")
        # ending line of the rows
        print("\r")
# Driver Code
m = 5
pypart(m)
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p><strong>Output:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>* 
* * 
* * * 
* * * * 
* * * * *</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":3} -->
<h3>2.Using List:</h3>
<!-- /wp:heading -->

<!-- wp:code -->
<pre class="wp-block-code"><code>def pypart(m):
    List = &#91;]
    for i in range(1,m+1):
        List.append("*"*i)
    print("\n".join(List))
# Driver Code
m = 5
pypart(m)
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p><strong>Output:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>* 
** 
*** 
**** 
***** 
</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":3} -->
<h3>3.Using Approach of Recursion:</h3>
<!-- /wp:heading -->

<!-- wp:code -->
<pre class="wp-block-code"><code>def pypart(m):
    if m==0:
        return
    else:
        pypart(m-1)
        print("* "*m)
# Driver Code
m = 5
pypart(m)</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p><strong>Output:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>* 
* * 
* * * 
* * * * 
* * * * * </code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":3} -->
<h3>4.Using While Loop Approach:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>Code:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>m=5
a=1; j=0
# As long as the condition is true, the while loop checks it. Upon entering the loop # publishing the pattern if it is true.
while(a&lt;=m):
while(j&lt;=a-1):
print("* ",end="")
j+=1
# printing of next line for each row
print("\r")
j=0;a+=1</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p><strong>Output:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>* 
* * 
* * * 
* * * * 
* * * * * 
</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":3} -->
<h3>5. Printing of Patterns for the Left Side:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>Code:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>While loop:
m=5;i=0
while(i&lt;=m):
  print(" " * (m - i) +"*" * i)
  i+=1</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p><strong>Output:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>
    *
   **
  ***
 ****
*****
</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":3} -->
<h3>6. For loop:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>Code:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>h = 5
for row in range(1, h+ 1):
    print(" " * (h - row) +"*" * row)
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p><strong>Output:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>    *
   **
  ***
 ****
*****
</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":3} -->
<h3>7. Printing of Shape of Triangle:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>Code:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>def triangle(n):  
    # in between number of spaces
    k = m – 1 
    
    for i in range(0, m):
        # values changing w.r.t to need of values
        for j in range(0, k):
            print(end=" ")
     
        # Decreasing of k after every loop
        k = k - 1
        # handling of inner loops by columns
        # values changing w.r.t. to outer loop
        for j in range(0, i+1):
            # printing stars
            print("* ", end="")
        print("\r")
# Driver Code
m = 5
triangle(m)</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p><strong>Output:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>    * 
   * * 
  * * * 
 * * * * 
* * * * * 
</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":3} -->
<h3>8. Number Pattern Approach:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>Code:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>def numbpat(m):
    # initialising starting number
    num = 1
     # handling of outer loops by rows
    for i in range(0, m):     
        # re assigning of num
        num = 1
        # handling of inner loops by columns
            # values changing w.r.t to outer loop
        for j in range(0, i+1):
                # printing number
            print(num, end=" ")
            # increasing number at every column wise
            num = num + 1
             # ending line after every row
        print("\r")
 # Driver code
m = 5
numbpat(m)
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p><strong>Output:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":3} -->
<h3>9. Character Pattern of Pyramid:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>Code:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>def pat(m):
    # initializing value associated to A
    # ASCII value demonstration
    num = 65 
    # outer loop to be handled in number of rows
    # 5 in this case as it will be declared
    for i in range(0, m): 
    
        # inner loop to be handled in number of columns
        # values changing w.r.t to outer loop
        for j in range(0, i+1):   
      
            # conversion to char
            ch = chr(num)   
     
            # printing of char value
            print(ch, end=" ")

        # increasing number by 1
        num = num + 1
        # ending of line after every row
        print("\r")

# Driver Code
m = 5
pat(m)
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p><strong>Output:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>A 
B B 
C C C 
D D D D 
E  E  E  E   E 
</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":3} -->
<h3>10. Flow of Alphabet Pattern:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>Code:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>def conpha(m):

    # initializing value associated to A
    # ASCII value demonstration
    num = 65
    #     # outer loop to be handled in number of rows
    # 5 in this case as it will be declared

- for i in range(0, m):
        # inner loop to be handled in number of columns
        # values changing w.r.t to outer loop
    for j in range(0, i+1): 

        #conversion to char
        ch = chr(num) 
        # printing of char value
        print(ch, end=" ") 

        # increasing at each column
        num = num + 1 
    # ending of line after each row
    print("\r") 
# Driver code
n = 5
conpha(m)
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p><strong>Output:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>A 
B C 
D E F 
G H I J 
K L M N O
</code></pre>
<!-- /wp:code -->

Related Topics

Method Overloading in Python

Overloading is a capability of a method and operator to behave differently according to the nature of parameters. Overloading is popular because it provides a good number of advantages: Reusability of code.Reduces...

3 minutes read.

Python Multiprocessing Processor

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Python Skyline

Python Programming Language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Python Logging Maxbytes

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

6 minutes read.

Insertion Sort using Python

Insertion sort is a type of sorting technique that is used for sorting an array with random elements. Using sorting methods, any unsorted array can be sorted into ascending or...

3 minutes read.

Python Combination

Python Combination Permutations and combinations are the important part of our mathematical tools. We use them often in our Python programs. In this tutorial, we will learn about combinations in Python...

6 minutes read.

Difference between Package and Module in Python

What are Python modules? A file with the “.py” suffix that includes Python or C executable code is known as a module. Multiple Python commands and expressions make compose a module....

3 minutes read.

GUI to Shut down, Restart and Logout from the PC using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

4 minutes read.

Python Subprocess Call Example

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

4 minutes read.

Classes & Objects in Python

Classes and Objects are used in most modern programming languages, mainly in Object-oriented Programming languages. What is meant by Object Oriented Programming language? Usage of objects and their components in order to...

7 minutes read.

Python Encapsulation

What is meant by Encapsulation? The process of restricting access to the methods and variables so that accidental modification of data can be prevented is known as Encapsulation. The motive of Encapsulation:...

3 minutes read.

How to Install Pandas in Python?

Pandas is a library created in Python for handling and analyzing data. Pandas provides a variety of procedures and data structures for manipulating time series and numerical data.  Pandas is...

3 minutes read.

Confusion Matrix Visualization Python

The confusion matrix is a two-dimensional array that compares the anticipated and actual category labels. These are the True Positive, True Negative, False Positive, and False Negative classification categories for...

4 minutes read.

StandardScaler in Sklearn

When and How Should You Use StandardScaler? StandardScaler comes into play whenever the properties of the provided dataset change significantly within their ranges or are recorded in different measurement units. Since using...

4 minutes read.

Handling missing keys in Python dictionaries

In this lesson, you will discover how to create a Python application that will manage missing keys in a dictionary. Python dictionaries store data as key-value pairs, where each value...

3 minutes read.

Salary of Python Developers in India

In this tutorial, we will understand who python developers are and what is their salary when they work in India. Python Developers Python Developers are the people who are involved in designing...

4 minutes read.

Python String expandtabs() method

Python String expandtabs() method The string.expandtabs() method in Python returns a copy of the string where all tab characters are expanded using spaces. Syntax String.expandtabs([tabsize]) Parameter tabsize: This parameter specifies the number of characters to...

1 minute read.

GUI to extract lyrics from a song Using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

4 minutes read.

Python String lower() method

Python String lower() method The string.lower() method in Python returns a string where all characters are lower case. Syntax string.lower() Parameter NA Return This method returns a string where all characters are lower case. Example 1 # Python...

1 minute read.

Python Program to check whether a given number is Armstrong or not

Program to check whether a given number is Armstrong or not A number is said to be an Armstrong if the sum of each digit's cube of a given number equals...

1 minute read.