Related Tags

communitycreator
python

# Generate a hollow right angled triangle using numbers in Python

Vinisha Maheshwari

In this shot, we will discuss how to generate a hollow right-angled triangle using numbers in Python.

We can print a plethora of patterns using Python. The only prerequisite is a good understanding of how loops work in Python. Here, we will use simple for loops to generate a hollow right-angled triangle using numbers.

### Description

A triangle is said to be right-angled if it has an angle equal to 90 degrees on its left side.

To create a hollow right-angled triangle using Python programming, we will use two for loops:

• An outer loop: To handle the number of rows.
• An inner loop: To handle the number of columns.

### Code

Let’s see the code snippet below.

# Number of rows
n = 5

# Loop through rows
for i in range(1,n+1):

# Loop through columns
for j in range(1, n+1):

# Printing Pattern
if (i==j) or (j==1) or (i==n):
print(i, end=" ")
else:
print(" ", end=" ")
print()



### Explanation

• In line 2, we take the input for the number of rows (i.e., length of the triangle).

• In line 5, we create a for loop to iterate through the number of rows.

• In line 8, we create an inner nested for loop to iterate through the number of columns.

• From lines 11 to 14, we create the pattern using conditional statements. The end statement helps to stay on the same line.

• In line 15, the print() statement is used to move to the next line.

RELATED TAGS

communitycreator
python

CONTRIBUTOR

Vinisha Maheshwari
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time