Python Programs to Print Patterns - Pyramid Triangle Star

Python Programs to Print Patterns – Pyramid, Triangle using Star

This tutorial will guide on how to write python programs to print patterns like pyramid or triangle using start symbol. Let’s see how to print half pyramids, inverted half pyramid, full pyramid or triangle and inverted full pyramid patterns using Python programming language.

Python Programs to Print Patterns

Patterns can be printed using simple for loops in python programming. We need to use two for loops for printing all the patterns. The first outer for loop is used to handle number of rows and the inner nested for loop is used to handle number of columns.

You need to manipulate the print statements to print the pattern in required format. Here we have tried to print start patterns. Therefore, we will be manipulating print statement using star “*” in the statements.

Python Programs to Print Patterns - Pyramid Triangle Star

 

Print simple half pyramid pattern

The following program will print Half Pyramid Pattern.

>>> def pyhalfpyramidpattern(n):
...     for i in range(0,n):
...             for j in range(0, i+1):
...                     print("* ",end="")
...             print("\r")
...

Output

>>> n = 6

>>> pyhalfpyramidpattern(n)

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

Print inverted half pyramid pattern

Now, let’s see how to print the above half pyramid pattern in the inverted fashion.

Here is the program to print Inverted Half Pyramid Pattern 

>>> def pyinvertedhalfpyramidpattern(n):
...    k=2*n-2
...    for i in range(n, -1, -1):
...        for j in range(k, 0, -1):
...            print(end="")
...        k=k+1
...        for j in range(0, i+1):
...            print("* ",end="")
...        print(" ")

Output

>>> n=6

>>> pyinvertedhalfpyramidpattern(n)

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

Half Pyramid Pattern – 180 rotation

Next, let’s try to rotate the half pyramid pattern by 180 degree and print.

Here is the program to print Half Pyramid Pattern – 180 degree rotation.

>>> def pyhalfpyramidpattern2(n):
...     k = 2*n-2
...     for i in range(0,n):
...             for j in range(0, k):
...                     print(end=" ")
...             k = k-2
...             for j in range(0, i+1):
...                     print("* ", end="")
...             print("\r")
...

Output

>>> n = 6

>>> pyhalfpyramidpattern2(n)
          *
        * *
      * * *
    * * * *
  * * * * *
* * * * * *

Similarly, in the next section let’s learn how to print full pyramid or triangle pattern and inverted full pyramid pattern.

Full Pyramid/ Triangle pattern

Here is the program to print Full Pyramid Pattern or Triangle Pattern.

>>> def pyfullpyramidpattern(n):
...     k = n-1
...     for i in range(0,n):
...             for j in range(0,k):
...                     print(end=" ")
...             k = k-1
...             for j in range(0,i+1):
...                     print("* ", end="")
...             print("\r")
...

Output

>>> n = 6

>>> pyfullpyramidpattern(n)

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

Inverted Pyramid or Triangle

Next, lets try to print Inverted Full Pyramid or Triangle Pattern.

>>> def pyinvertedtriangle(n):
...    k = n -2
...    for i in range(n, -1, -1):
...        for j in range(k, 0, -1):
...            print(end=" ")
...        k = k + 1
...        for j in range(0, i+1):
...            print("* ", end="")
...        print()

Output

>>> n = 6

>>> pyinvertedtriangle(n)

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

That’s it. You had learnt how to print half pyramids, inverted half pyramid, full pyramid or triangle and inverted full pyramid patterns using Python programming language.

Hope it helped 🙂

You’ll also like:

References

 

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments