Print right angle triangle in Python
In this tutorial, we are going to learn how to print the right-angled triangle in Python.
Using for loop we can print the right-angled triangle. To understand this you should know the basics of for loop.
To print the right-angled triangle in Python, we can take the input from the user for the length of the triangle.
x=int(input("Enter row number=\n")) for i in range(x): for j in range(i+1): print("#",end='') print("")
As you can see, Input is taken from the user as (x). As we know that a for loop is used for iterating over a sequence. Then using nested for loop, you can print the right-angled triangle.
First of all, a for loop is used for row and inside that, another for loop is used for the column. The range (i+1) indicates that as the number of rows increases, the number of columns will also increase. You can print the right-angled triangle by any of the symbols.
Enter row number=4
As I have given the input as 4. So it will print the right-angled triangle printing the symbol (#) in 4 rows and 4 columns. Often it is done by using the “*”.
Now if we run our program, it will give the output that you can see below:
# ## ### ####
So, we did it successfully. We able to create a right angle triangle formed with “#” symbol in Python.