
Le’ts learn how to draw a triangle in Python. We can easily do that using the turtle module.
These are the methods that we will use to create a triangle.
- Turtle(): It will instantiate a new turtle object.
- forward(): It takes a number and moves the turtle (pen) by that given distance.
- left(): It turns the turtle to a given angle in the anti-clockwise direction. By default, it takes an angle in degrees.
- right(): It turns the pen in the clockwise direction.
- fillcolor(): This method sets the color of the shape to be drawn.
- begin_fill(): We need to call this method before drawing the shape that needs to be filled.
- end_fill(): We need to call this method after we have drawn the shape. It fills it with the given color.
Now that we know all the required methods, let’s get started and draw a basic triangle.
import turtle turt = turtle.Turtle() #instantiate a new object turt.fillcolor("cyan") #set the color of the triangle to cyan turt.begin_fill() turt.left(120) turt.forward(150) turt.left(120) turt.forward(150) turt.left(120) turt.forward(150) turt.end_fill() turtle.done()#pauses the program
Initially, we create a new turtle object. Then, we set the color of the shape to cyan and call the begin_fill() method. After that, we start drawing the triangle.
First, we rotate the pen 120o anti-clockwise, and then we move it forward 150 pixels in that direction. This is what we get.

Draw a triangle in Python Step 1
Then, we need to go down. For that, we again rotate 120o anti-clockwise and then 150 pixels forward.

Draw a triangle in Python Step 2
The last step is to draw a horizontal line to complete the triangle. We need to repeat the same step. The final triangle is given below.

Draw a triangle in Python Step 3
Instead of repeating the same lines of code, we can use a loop. Let’s see.
import turtle turt = turtle.Turtle() #instantiate a new object turt.fillcolor("cyan") #set the color of the triangle to cyan turt.begin_fill() for i in range(0, 3): turt.left(120) turt.forward(150) turt.end_fill() turtle.done()
The above triangle is equilateral. Let’s now draw a right-angled triangle.
import turtle turt = turtle.Turtle() #instantiate a new object turt.fillcolor("cyan") #set the color of the triangle to cyan turt.begin_fill() turt.left(90) turt.forward(150) turt.left(225) turt.forward(212.13) turt.left(225) turt.forward(150) turt.end_fill() turtle.done()
Output

Triangle in Python
Hey guys! It’s me, Marcel, aka Maschi. I earn a full-time income online and on MaschiTuts I gladly share with you guys how I stay on top of the game! I run several highly profitable blogs & websites and love to speak about these project whenever I get a chance to do so. I do this full-time and wholeheartedly. In fact, the moment I stopped working an 8-to-5 job and finally got into online business as a digital entrepreneur, is problably one of the best decisions I ever took in my life. And I would like to make sure that YOU can get on this path as well! Don’t let anyone tell you that this can’t be done. Sky’s the limit, really…as long as you BELIEVE in it! And it all starts right here..at Maschituts!