Example HTML page

A Python program to print the pattern of a Right angled triangle

We already know  the shape of a right angled triangle. So to print the pattern of that triangle with a specific character here we go.

Pattern program of Right angled triangle

num=int(input("Enter the number of rows to print: "))
for x in range(1,num+1):
    for y in range(1,x+1):
        print("*", end= " ")
    print( )

Output

Enter the number of rows to print: 5
* 
* * 
* * * 
* * * * 
* * * * *

Tracing:

Let’s trace the above program step by step.

1. for x=1 in range(1,5+1) ⇒range(1,6) ⇒True then execution moves to next for loop; for y=1 in range(1,1+1) ⇒ range(1,2) ⇒True. Then it moves to immediate statement(i.e print(“*”) statement) and prints “*”. Here it prints one “*” because of range (1,2) is 1.

*

2. Similarly, for x=2 in range(1,5+1) ⇒range(1,6) ⇒True then, for y=2 in range(1,2+1) ⇒ range(1,3) ⇒True.Range(1,3) is 2 and it prints “**”.

* *

3. for x=3 in range(1,5+1) ⇒range(1,6) ⇒True then, for y=3 in range(1,3+1) ⇒ range(1,4) ⇒True.Range(1,4) is 3and it prints “***”.

* * *

4.for x=4 in range(1,5+1) ⇒range(1,6) ⇒True then, for y=4 in range(1,4+1) ⇒ range(1,5) ⇒True.Range(1,5) is 4 and it prints “****”.

* * * *

5.for x=5 in range(1,5+1) ⇒range(1,6) ⇒True then, for y=5 in range(1,5+1) ⇒ range(1,6) ⇒True.Range(1,6) is 5 and it prints “*****”.

* * * * *

6.for x=6 in range(1,6+1) ⇒range(1,7) ⇒6(False) because the input range is given as ‘5’.Hence it exits from the inner for loop and executes the outer for loop print statement which has nothing to print.

 

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest