If you have any query feel free to chat us!
Happy Coding! Happy Learning!
To print a triangle pattern in Python, you can use nested loops to control the number of stars (or any other character) in each row. Here's an example of how to create different triangle patterns using nested loops:
- Right-angled Triangle (with stars):
pythonCopy code
def right_angle_triangle(size): for i in range(1, size + 1): print("* " * i) # Example usage: size_of_triangle = 5 right_angle_triangle(size_of_triangle)
Output:
markdownCopy code
* * * * * * * * * * * * * * *
- Inverted Right-angled Triangle (with stars):
pythonCopy code
def inverted_right_angle_triangle(size): for i in range(size, 0, -1): print("* " * i) # Example usage: size_of_triangle = 5 inverted_right_angle_triangle(size_of_triangle)
Output:
markdownCopy code
* * * * * * * * * * * * * * *
- Equilateral Triangle (with stars):
pythonCopy code
def equilateral_triangle(size): for i in range(1, size + 1): print(" " * (size - i) + "* " * i) # Example usage: size_of_triangle = 5 equilateral_triangle(size_of_triangle)
Output:
markdownCopy code
* * * * * * * * * * * * * * *
In each example, the nested loops are used to control the number of stars in each row based on the current row index (
i
). Therange()
function is used to create the appropriate range for each type of triangle pattern. Theprint()
statement is used to print stars (or spaces) in the required format to create the triangle pattern.You can modify the
size_of_triangle
variable to create triangle patterns of different sizes or replace the "*" character with any other character or string to create patterns using different symbols.
Comments: 0