Introduction :
In this tutorial, we will learn how to print an inverted right-angled triangle with numbers or characters in Python
. A right-angled triangle has one 90 degrees angle or right angle. For an inverted right-angled triangle, this right angle will be at the top left corner. The other angles will be 45 degrees each.
The program will print the right-angled triangle using numbers or any other character. It will ask the user to enter the height and the character to print the triangle and print it with the user-given values.
Example 1: Python 3 program to print inverted right-angled triangle using number:
The following program takes the height of the triangle as input from the user and prints the triangle with numbers:
height = int(input("Enter the height of the triangle: "))
for i in range(1, height + 1):
for j in range(1, height - i + 2):
print(str(j) + " ", end="")
print()
Get it on GitHub
Output :
You will get output similar to the screenshot below:
How does it work?
In this example, we are printing an inverted right-angled triangle using only numbers. The numbers are printed serially in increasing order like 1,2,3,4,5…
.
If the height of the triangle is 5:
- For the first line, we are printing 5 numbers ‘1,2,3,4,5’
- For the second line, we are printing 4 numbers ‘1,2,3,4’
- For the third line, three numbers ‘1,2,3’ etc.
We need to use two loops, one outer loop, and one inner loop to print the pattern. The outer loop will point to each row of the triangle and the inner loop will print the numbers.
- First of all, the program is reading the
height
of the triangle as input from the user. It can print a triangle of any height. - It runs one
for
loop forheight
number of times. It runs fromi = 1
toi = height
. - For each iteration of the outer loop, it runs one more inner loop to print the numbers of the triangle.
- The inner loop runs from
j = 1
(inclusive) toj = height – i +2
(exclusive)- e.g. if the height is
5
, for the first time, it will run in the range ofj = 1
toj = 5 – 1 + 2 = 6
i.e. it will run for j = 1, 2, 3, 4, 5. - For the second time, it will run from
j = 1
toj = 4
or 4 times, etc.
- e.g. if the height is
- The inner loop prints out the numbers serially.
Example 2: Python 3 program to print an inverted right-angled triangle using any character :
We can also print the same inverted triangle using any character. The following program shows how to print the pattern with a user-given character:
height = int(input("Enter the height of the triangle: "))
c = str(input("Enter the character you want to print the triangle: "))
for i in range(0, height):
for j in range(0, height - i):
print(c + " ", end="")
print()
Get it on GitHub
Output :
You can use any character to print the pattern:
How does it work?
This is similar to the previous example.
- It uses two
for
loops to print the pattern. - The outer loop runs for
height
number of times. It will run fori = 0
toi = height - 1
. This step is different than the previous example. You can also start the loop fromi = 1
. - The inner loop runs from
j = 0
toj = height - i
. Inside this loop, it prints the character. - Unlike the previous example, we are printing a character to create the triangle. The character is also provided by the user. In the example, we are using
*
to print the triangle, but you can also use any character e.g. ‘$’,’&’,’#’ etc.
Example 3: How to use while
loops to print the inverted right-angled triangle:
You can use while
loops to print the same pattern:
height = int(input("Enter the height of the triangle: "))
c = str(input("Enter the character you want to print the triangle: "))
i = 0
while i < height:
j = 0
while j < height - i:
print(c + " ", end="")
j += 1
print()
i += 1
Get it on GitHub
This is similar to the previous example. The difference is that you need to initialize the variables i
and j
before the loop starts and the value of i
and j
should be incremented at the end of each iteration of the loops.
It will print similar results.
Enter the height of the triangle: 5
Enter the character you want to print the triangle: %
% % % % %
% % % %
% % %
% %
%
Example 4: How to print a triangle with incremental numbers:
The following example shows how to print an inverted right-angled triangle with incremented numbers, e.g.:
1 2 3 4 5
6 7 8 9
10 11 12
13 14
15
We can use the same approach with a small change to the print
statement:
height = int(input("Enter the height of the triangle: "))
count = 1
for i in range(1, height + 1):
for j in range(1, height - i + 2):
print("{:4s}".format(str(count)), end="")
count += 1
print()
Get it on GitHub
- It uses a new variable
count
to print the pattern. The value of this variable is incremented by 1 at the end of each iteration of the loop. - The
print
statement formats the value with 4 spaces and is left justified. Without this change, the numbers won’t be aligned vertically.
Sample output:
Enter the height of the triangle: 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27
28 29 30 31 32 33 34
35 36 37 38 39 40
41 42 43 44 45
46 47 48 49
50 51 52
53 54
55
Example 5: How to print the triangle with numbers in reverse order:
We need to run the inner for
loop in reverse to print the triangle with reverse ordered numbers. For example,
height = int(input("Enter the height of the triangle: "))
for i in range(0, height):
for j in range(height - i, 0, -1):
print(str(j) + " ", end="")
print()
Get it on GitHub
The range(height - i, 0, -1)
statement will run the loop from j = height - i
to j = 1
, and on each step it decrements the value of j
by 1.
If you run this program, it will print the output as below:
Enter the height of the triangle: 5
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
Conclusion:
In this tutorial, we have learned how to print one inverted right-angled triangle in Python using numbers or any other characters. This tutorial is a good example of using nested for loops in Python. You can modify the program to print one non-inverted right-angle triangle. You can also try to print any other shapes using the same approach.
If you want to contribute in any other way to this post, please raise a pull request on GitHub.
Similar tutorials:
- Python program to print a triangle using star
- Python program to print a right angled triangle
- Python program to find out the perimeter of a triangle
- Python program to find the area and perimeter of a triangle
- Python program to print a star hollow square pattern
- Python program to print list elements in different ways