In this shot, we will discuss how to generate a hollow equilateral triangle using asterisks in Python.
We can print a plethora of patterns using Python. A prerequisite for doing so is to have a good understanding of how loops work in Python.
Here we will be using simple
for loops to generate a hollow equilateral triangle.
A triangle is said to be equilateral if all three of its sides are of the same length.
To display the triangle in Python, we will be using two
for loops nested within an outer
Let’s look at the code snippet below.
def hollow_triangle(n): # Outer loop for rows for i in range(1,n+1): # Inner loop for columns for j in range(1,2*n): # Conditions for creating the pattern if i==n or i+j==n+1 or j-i==n-1: print("*", end="") else: print(end=" ") print() hollow_triangle(8)
forloop to handle the number of rows.
forloop to handle the number of columns.
print()to move to the next line. The entire code has been enclosed inside the function block
hollow_triangle, which takes the row size as an argument.
View all Courses