How do you print an equilateral triangle in python?

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:

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

How do you print an equilateral triangle in python?

We can print a plethora of patterns using Python. The basic and only prerequisite is a good understanding of how loops work in Python. In this shot, we will use simple for loops to generate an inverted equilateral triangle using stars.

Description

A triangle is said to be equilateral if it has the same length on all three sides. An inverted equilateral triangle will be the inverted form of the same, with its vertex lying on the bottom, pointing downwards.

To execute this using Python programming, we will use two for loops nested within an outer for loop:

  • The outer loop will handle the number of rows and columns.
  • One inner loop will handle the initial spaces and the other inner loop will handle spaces in between the characters as well as printing the characters.

Code

Let us look at the code snippet below to understand this better.

# User Input for number of rows and columns (Length of Triangle)
num = 3

# Loop over number of rows and columns
for i in range(num, 0, -1):
    
    # Spaces across rows
    for j in range(0, num-i):
        print(end=" ")
    
    # Print spaced stars
    for j in range(0, i):
        print("*", end=" ")
    
    # Go to next line
    print()

Explanation

  • In line 2, the user gives input to specify the number of rows and columns, i.e., the length of the triangle

  • In line 5, we create an outer for loop to loop over the rows and columns

  • In lines 8 and 9, we create an inner for loop to print the initial spaces across the rows.

  • In lines 12 and 13, we create another inner loop to print the characters and spaces in between them.

  • In line 16, we use print() outside the inner loops but inside the outer loop to move to the next line.

How do you show a triangle in Python?

Programs to print triangles using *, numbers and characters.
First, we get the height of the pyramid rows from the user..
In the first loop, we iterate from i = 0 to i = rows ..
The second loop runs from j = 0 to i + 1. ... .
Once the inner loop ends, we print new line and start printing * in a new line..

How do you print a full triangle in Python?

Pattern - 4: Printing Triangle Pyramid.
n = int(input("Enter the number of rows: ")).
m = (2 * n) - 2..
for i in range(0, n):.
for j in range(0, m):.
print(end=" ").
m = m - 1 # decrementing m after each loop..
for j in range(0, i + 1):.
# printing full Triangle pyramid using stars..

What is a formula of equilateral triangle?

Formulas and Calculations for an Equilateral Triangle: Area of Equilateral Triangle Formula: K = (1/4) * √3 * a2. The altitude of Equilateral Triangle Formula: h = (1/2) * √3 * a. Angles of Equilateral Triangle: A = B = C = 60 degrees. Sides of Equilateral Triangle: a equals b equals c.