×

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

How to Declare a Variable in Python?

The concept of constants and variables is something that we are studying right from our primary classes. We know that constants are the fixed values whereas variables are those whose...

4 minutes read.

Python NewLine

In python, a new line character denoted by "\n" is known as an escape character or escape sequence, and it will force the cursor to change its position to the next...

6 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.

How to Install Python

Python Installation Guide Step by Step Installing Python is quite simple and easy. In this tutorial, we will show stepwise procedure to install Python and set up the Python environment in different...

2 minutes read.

Python Virtual Environment

In this tutorial, we will understand Virtual Environment in Python. We will understand the need for a virtual environment and also see how to make use of it in python....

3 minutes read.

How to Program in Python on Raspberry pi?

Introduction to Python A popular programming tool with simple, complete novice syntax is Python structure of paragraphs, phrases, and words. Due to its widespread use, this has a large community that...

4 minutes read.

Performing Transaction Using Python MySQL

Transaction A Transaction contains a set of SQL commands used to change the data in the dataset. If we say transaction, it means that the data in the database has changed....

3 minutes read.

How to run Python program in CMD

How to run python program in cmd Command Prompt provides us all together with a different approach for dealing with our programs. The programs can be executed by accessing the directories. In...

3 minutes read.

Math Module in Python

In this article, you are going to learn everything in detail about the “ math “ module in Python. We can normally work with general operations using python without importing or...

16 minutes read.

Python List copy() method

Python List copy () method The list.copy () method returns a shallow copy of the list. Syntax list.copy() Parameter NA Return This function returns the copy for the given list. Example 1 # Python program explaining # the list.copy() method # passing the...

1 minute read.

Covariance in Python

Covariance is defined as the estimate of the difference of change between two variables or more variables. It defines the changes of two variables together. In Python, The covariance can...

2 minutes read.

SKLearn Linear Module

The SK learn linear module is one such module that helps to study the relationship between the independent and dependent variables.The linear module can be implemented by using the best...

3 minutes read.

How to Concatenate Two Strings in Python

How to Concatenate Two Strings in Python Like the other data types, operations on strings are quite useful when we deal with some real-life applications. Here we will talk about a simple...

4 minutes read.

Python CGI Programming

The Concept of CGI CGI is an abbreviation for Common Gateway Interface. It is not a type of language but a set of rules (specification) that establishes a dynamic interaction between...

9 minutes read.

SKLearn Clustering

These are ml methods thatare responsible for detecting patterns and the similarities within the data.The clustering methods are unsupervised.Here the data is clustered to form groups with the help of...

3 minutes read.

Python Call Function

In this article, you will learn about calling a function in Python. But before learning this, we should know about functions in Python.So, let’s get some overview of functions in...

6 minutes read.

Reverse a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Python Tuple index() Method

Python Tuple index() Method The tuple.index() method of Python finds the first occurrence of the specified value and returns its index if the value is found else raises an exception if...

1 minute read.

Python Tuple Methods

Python Tuple Methods Python has two built-in methods that are used for tuples. The following are the two methods: Method Description count() The tuple.count() method in Python returns the number of times a...

1 minute read.

Python Filter List

To filter a list, we use filter () method function. The filter () will test every element true or not in the sequence. Syntax: Filter(function, sequence) Parameters: Function: Function that check and verifies if...

2 minutes read.