How does python print a triangle?

To print a triangle, you can achieve this using a loop statement.

Here is an example of using nested loops to print an isosceles right triangle.

n = 5  # 三角形的行数

# 外层循环控制行数
for i in range(n):
    # 内层循环控制每行的列数
    for j in range(i + 1):
        print("*", end="")
    print()  # 换行

Executing the above code will produce the following output:

*
**
***
****
*****

You can modify the value of n as needed to control the size of the triangle.

If you need to print other types of triangles, you can modify the logic of the inner loop according to specific requirements.

bannerAds