Related Tags

communitycreator
python
triangle
equilateral

# How to generate a hollow equilateral triangle in Python

Vinisha Maheshwari

In this shot, we will discuss how to generate a hollow equilateral triangle in Python.

We can print a plethora of patterns using Python. The only prerequisite to do this is a good understanding of how loops work in Python. Here, we will be using simple for loops to generate a hollow equilateral triangle using numbers.

### Description

A triangle is said to be equilateral if it has all three sides of the same length. To create a hollow equilateral triangle with numbers using Python programming, we will be using two for loops:

• An outer loop to handle the number of rows
• An inner loop to handle the number of columns

### Code

Let’s look at the below code snippet.

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(i, end=" ")
else:
print(end="  ")
print()

hollow_triangle(5)



### Explanation

• In line 4, we create a for loop to handle the number of rows.
• In line 7, we create a nested for loop to handle the number of columns.
• From lines 10 to 13, we define the conditions for creating the required shape using numbers.
• In line 14, we use 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.

RELATED TAGS

communitycreator
python
triangle
equilateral

CONTRIBUTOR

Vinisha Maheshwari
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time