
Hello Techies,
The triangle shape in python is the most frequently asked question in most interview processes. In this blog, we will learn how to draw a triangle in Python.
Table of Contents
Triangle shape in Python
#1. Triangle shape in Python using Star Pattern (Hollow triangle pattern in python)
To draw this pattern we are using loop and range function of Python. Basically in this program we are specifically using 4 rows and 7 columns. You can modify them to suit your needs.
# Program to draw a triangle shape in python # using star pattern for row in range(1, 5): for col in range(1, 8): if row == 4 or row+col == 5 or col - row == 3: print("*", end="") else: print(end=" ") print()
After executing the above code, the following results will be obtained –
* * * * * *******
#2. Triangle shape in Python using Star Pattern with user input
We are using Python’s loop and range function to draw this pattern. Basically in this program, we are taking input from the user and then creating a triangle shape.
# Program to draw a triangle shape in python # using star pattern with user input input_number = int(input("Enter the number of rows:")) for row in range(1, input_number+1): for col in range(1, 2*input_number): if row == input_number or row+col == input_number+1\ or col - row == input_number-1: print("*", end="") else: print(end=" ") print()
After executing the above code, the following results will be obtained –
Enter the number of rows:5 * * * * * * * *********
#3. Python number triangle
We are using Python’s loop and range function to draw this pattern. Basically in this program, we are writing the program for the number triangle.
# Program to draw a Python number triangle for i in range(1, 5): row = str(i)+" " print(" "*(5-i)+row*(i))
After executing the above code, the following results will be obtained –
1 2 2 3 3 3 4 4 4 4
#4. 1 121 12321 triangle in python
We are using Python’s loop and range function to draw this pattern. Basically in this program we are writing the program for 1 121 12321 triangle.
Method 1
# Program to draw a 1 121 12321 triangle # in python odd = 1 space = 3 for i in range(1, 5): k = 0 for j in range(1, space + 1): print(" ", end="") for j in range(1, odd + 1): if (j <= i): k = k + 1 else: k = k - 1 print(k, end="") print() odd=odd+2 space=space-1
After executing the above code, the following results will be obtained –
1 121 12321 1234321
Method 2
# Program to draw a 1 121 12321 triangle # in python space = 3 for i in range(1, 5): for j in range(1, space + 1): print(" ", end="") print((pow(10,i)//9)**2) space = space - 1
After executing the above code, the following results will be obtained –
1 121 12321 1234321
#5. Mirrored right triangle star pattern in python
Method 1: Draw right triangle in Python using for loop
# Program to draw a Mirrored # right triangle star pattern # using for loop for i in range(1, 5): for j in range(1, i+1): print("*", end="") print()
After executing the above code, the following results will be obtained –
* ** *** ****
Method 2: Draw right triangle in Python using while loop
# Program to draw a Mirrored # right triangle star pattern # using while loop i=1 while(i<5): print (''*(5-i)+'*'*i) i += 1
After executing the above code, the following results will be obtained –
* ** *** ****
#6. Left triangle star pattern in Python
Method 1: Draw left triangle in Python using for loop
# Program to draw # left triangle star pattern # using for loop for i in range(4, -1, -1): print (' '*i, end='') for j in range(1, 6-i): print("*", end="") print()
After executing the above code, the following results will be obtained –
* ** *** **** *****
Method 2: Draw left triangle in Python using while loop
# Program to draw # left triangle star pattern # using while loop i=1 while(i<6): print (' '*(5-i)+'*'*i) i += 1
After executing the above code, the following results will be obtained –
* ** *** **** *****
#7. Triangle in python using turtle library
In this program, we are using the turtle library to draw the triangle shape.
# Program to draw Triangle in python # using turtle library import turtle board = turtle.Turtle() board.forward(100) # draw base board.left(120) board.forward(100) board.left(120) board.forward(100) turtle.done()
After executing the above code, the following results will be obtained –
See their official site for more information about the Turtle Library.
I have covered a different pattern of triangles in this blog. Comment below if you want any other type of triangle program in python.
