Printing Different Pyramid Patterns in Python

Printing Different Pyramid Patterns in Python

Printing Different Pyramid Patterns in Python

pyramid pattern, pattern program in java, pyramid program in java, number pattern ,program in java, different star pattern, program in java



Its become easier to print the pattern through For loop. The outer most loop will decide or handle the number of rows and the inner loop will decide the number of columns. By using different printing styles, we can print patterns of Alphabets, Numbers, Stars can be possible.

We will show some of the star pyramid patterns in this article 

Simple Pyramid 

def pyra(n): 

     # outer loop for rows 

    # n in this case 

    for i in range(0, n): 

      

        # inner loop for columns 

         for j in range(0, i+1): 

           # print stars 

             print("* ",end="") 

         print("\r") 

 n = 5

pyra(n)


Output: 

* * 

* * * 

* * * * 

* * * * * 


There is Another Approach:

Here we can print the pyramid pattern with the help of list

def pyra(n): 

    myList = [] 

    for i in range(1,n+1): 

        myList.append("*"*i) 

    print("\n".join(myList)) 

  n = 5

pyra(n) 


Output:

*

**

***

****

*****

Now lets see  the same pattern After 180 degree rotation

def pyra2(n): 

      #  spaces 

    k = 2*n - 2

    # outer loop to handle number of rows 

    for i in range(0, n): 

        # in ner loop for space

        for j in range(0, k): 

            print(end=" ") 

        k = k - 2

        # inner loop for columns 

        for j in range(0, i+1): 

            # print stars 

            print("* ", end="") 

        print("\r") 

n = 5

pyra2(n)


 Output:

        * 

      * * 

    * * * 

  * * * * 

* * * * * 


Printing an Triangle:

def triangle(n): 

    k = 2*n - 2

    # outer loop for rows 

    for i in range(0, n): 

        # inner loop for spaces 

        for i in range(0, k): 

            print(end=" ")  

        k = k - 1

        # inner loop for  columns 

        for j in range(0, i+1): 

            # print stars 

            print("* ", end="") 

        print("\r") 

n = 5

triangle(n) 


Output:

        * 

       * * 

      * * * 

     * * * * 

    * * * * * 


Number pattern :

def numpat(n): 

    # initialising starting number  

    num = 1

    #outer loop for rows 

    for i in range(0, n): 

        # re assigning num 

        num = 1

        # inner loop for columns 

        for j in range(0, i+1): 

            print(num, end=" ") 

            num = num + 1

        print("\r") 

n = 5

numpat(n) 


Output:

1 2 

1 2 3 

1 2 3 4 

1 2 3 4 5 


Number without re-assingning:

def contnum(n): 

    # initializing starting number  

    num = 1

    # outer loop for rows 

    for i in range(0, n): 

        # not re assigning num 

        # num = 1 

        # inner loop for columns 

        for j in range(0, i+1): 

          #print number

            print(num, end=" ") 

            num = num + 1

        print("\r") 

n = 5

contnum(n) 


Output:

2 3 

4 5 6 

7 8 9 10 

11 12 13 14 15 


Character Pattern:

def alphpat(n):   

    # initializing value corresponding to 'A'  

    # ASCII value 

    num = 65

    # outer loop for rows 

    # 5 in this case 

    for i in range(0, n): 

        # inner loop for columns 

         for j in range(0, i+1): 

            # explicitely converting to char 

            ch = chr(num) 

            # printing char value  

            print(ch, end=" ") 

        num = num + 1

        print("\r")

n = 5

alphpat(n) 


Output:

B B 

C C C 

D D D D 

E E E E E


Continuous Character pattern:

def  contalpha(n): 

    # initializing value corresponding to 'A'  

    # ASCII value 

    num = 65 

    # outer loop for rows 

    for i in range(0, n): 

        # inner loop for column

        for j in range(0, i+1): 

            # explicitely converting to char 

            ch = chr(num) 

            # print value  

            print(ch, end=" ") 

            num = num +1

        print("\r")  

n = 5

contalpha(n) 


Output:

B C 

D E F 

G H I J 

K L M N O 


 also see:





Post a Comment

0 Comments