If you have any query feel free to chat us!
Happy Coding! Happy Learning!
To print an inverted triangle pattern in Python, you can use nested loops to control the number of stars (or any other character) in each row, but in a descending order. Here's an example of how to create an inverted triangle pattern using nested loops:
pythonCopy code
def inverted_triangle(size): for i in range(size, 0, -1): print("* " * i) # Example usage: size_of_triangle = 5 inverted_triangle(size_of_triangle)
Output:
markdownCopy code
* * * * * * * * * * * * * * *
In this example, the
inverted_triangle()
function takessize
as input, which represents the number of rows in the inverted triangle. The function uses afor
loop that starts with the maximum number of stars (size) and iterates backward (decrementing by 1 in each iteration) until it reaches 1.During each iteration of the loop, a row of stars is printed using the
print("* " * i)
statement, wherei
represents the current row number. The*
character is repeatedi
times in each row, and theprint()
statement automatically moves to the next line after each row.When you call the
inverted_triangle()
function withsize_of_triangle = 5
, it will create an inverted triangle pattern with 5 rows, as shown in the output.You can modify the
size_of_triangle
variable to create inverted triangle patterns of different sizes or replace the "*" character with any other character or string to create patterns using different symbols.
Comments: 0