Printing Triangle Pattern 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 46:- Printing Triangle Pattern In Python

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:

  1. 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

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

  1. 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

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

  1. 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). The range() function is used to create the appropriate range for each type of triangle pattern. The print() 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.

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?