Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    Python Program to Creating an Equilateral Triangle (Pyramid Pattern)

    Python Programming Examples


    A Python program to display the stars in an equilateral triangular form using a single for loop. It is also called the program of creating a Pyramid Pattern from Star shape. The primary purpose of creating this program is to explain the concept of the loop in the Python program.

    Program:

    # to display stars in equilateral triangular form 
    n=20
    for i in range(1, 11):
        print(' '*n, end='') # repet space for n times
        print('* '*(i)) # repeat stars for i times
        n-=1

    Program Output:

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

    In the above program the end='' represents that it should not throw the cursor to the next line after displaying each star.

    This program can also be written using the elegant style of Python as:

    Example:

    # to display stars in equilateral triangular form 
    n=20
    for i in range(1, 11):
        print(' '*(n-i) + '* '*(i))
        

    Python Program to Display Stars in the Right-angled Triangular Form

    In the above program, n-=1 is used to reduce the space by 1 in every row, so that the stars will form the equilateral triangle. Otherwise, it will display as right-angled triangular form.

    Example:

    # to display stars in right-angled triangular form 
    n=20
    for i in range(1, 11):
        print(' '*n, end='') # repet space for n times
        print('* '*(i)) # repeat stars for i times

    Program Output:

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


    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.