Python Print an Inverted Triangle Star Pattern

Document 对象参考手册Python3 Example

We will use Python to write a simple program to print an inverted triangle star pattern. This pattern will consist of multiple rows of asterisks, with the number of asterisks decreasing row by row.

Example

def print_inverted_triangle(n):
    for i in range(n, 0, -1):
        print('*' * i)

# Call the function to print a 5-row inverted triangle
print_inverted_triangle(5)

Code analysis:

  1. def print_inverted_triangle(n):Defined a function namedprint_inverted_trianglefunction, which accepts a parametern, representing the number of rows of the inverted triangle.
  2. for i in range(n, 0, -1):Useforloop fromndescending to 1, each iterationithe value will decrease by 1.
  3. print('*' * i)In each iteration, printiasterisks.'*' * iindicates repeating the asteriskitimes.
  4. print_inverted_triangle(5)Call the function to print a 5-row inverted triangle.

Output:

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

Document 对象参考手册Python3 Example

Other Extensions