⚡ TL;DR
Print the inverted right triangle star pattern in Python. Complete working code, expected output, algorithm explanation, and practice variations for beginners.
The Inverted Right Triangle Star pattern is a fundamental loop exercise for Python beginners. Here’s the complete implementation with working code and output.
Pattern to Print
* * * * *
* * * *
* * *
* *
*Implementation
def inverted_triangle(n):
for i in range(n, 0, -1):
print("* " * i)
inverted_triangle(5)Output
* * * * *
* * * *
* * *
* *
*How It Works
Decreasing stars per row — loop from n down to 1.
Practice Variations
- Try printing the pattern with a different character (e.g.,
#or+) - Modify the code to accept the pattern size as user input
- Combine this pattern with its inverse to create a diamond or hourglass
Complexity
- Time: O(n²) — total characters printed grows quadratically
- Space: O(n) — longest row is O(n) characters
Related Patterns
- Inverted Right Triangle Star in Dart
- Inverted Right Triangle Star in Go
- Inverted Right Triangle Star in Swift
FAQ
How do you print the Inverted Right Triangle Star pattern in Python?
Use nested loops: the outer loop walks through the rows while the inner loop prints the characters or values for each row. The complete Python implementation with expected output is shown in the sections above.
What is the time complexity of the Inverted Right Triangle Star pattern in Python?
The time complexity is O(n²) and the space complexity is O(n), since the pattern is built with a fixed number of loop counters and printed row by row.
Happy coding!
