Inverted Triangle In Python

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 47:- Inverted Triangle In Python

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 takes size as input, which represents the number of rows in the inverted triangle. The function uses a for 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, where i represents the current row number. The * character is repeated i times in each row, and the print() statement automatically moves to the next line after each row.

When you call the inverted_triangle() function with size_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.

6. Loops

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?