To draw a triangle in Python, we can use the Python turtle module.

import turtle

t = turtle.Turtle()

def draw_triangle(side_length):
    for i in range(0,3):
        t.forward(side_length)
        t.right(120)

draw_triangle(100)

The turtle module in Python allows us to create graphics easily in our Python code.

We can use the turtle module to make all sorts of shapes in Python. For example, we can draw circles and draw rectangles easily in Python with the turtle module.

One other shape which is easy to make is a triangle.

Triangles have three sides. To draw a triangle in Python, we need to have our turtle draw the three sides.

We can create a simple triangle by defining a function that takes in an integer representing side length. Then we can loop three times, using the forward() function to create the side, and then rotating the cursor 120 degrees with the right() function.

Below is a simple example of how to use Python to make a triangle.

import turtle

t = turtle.Turtle()

def draw_triangle(side_length):
    for i in range(0,3):
        t.forward(side_length)
        t.right(120)

draw_triangle(100)

turtle triangle

How to Draw a Triangle with Different Colors in Python

With turtle colors in Python, we can change the colors of our shapes easily.

The main function you can use to change the color of a line is with the turtle pencolor() function.

Below is an example and the output of how to draw a green triangle using pencolor() in Python.

import turtle

t = turtle.Turtle()

t.pencolor("green")

def draw_triangle(side_length):
    for i in range(0,3):
        t.forward(side_length)
        t.right(120)

draw_triangle(100)

turtle green triangle

With turtle, you can also easily fill shapes with color.

To fill a shape, there are a few steps to take. We use the fillcolor() function to define the fill color of our shape, and then use the begin_fill() and end_fill() functions to define when to begin and end filling shapes with the fill color.

Just like the pencolor() function, the fillcolor() function takes any valid color given a color mode.

Let’s take the example from above and fill our triangle with the color ‘light blue’ using fillcolor(), begin_fill() and end_fill() in Python.

import turtle

t = turtle.Turtle()

t.fillcolor("light blue")

t.pencolor("green")

t.begin_fill()

def draw_triangle(side_length):
    for i in range(0,3):
        t.forward(side_length)
        t.right(120)

draw_triangle(100)

t.end_fill()

turtle light blue fill triangle green

How to Draw an Equilateral Triangle in Python with turtle Module

With the turtle module, we can easily create an equilateral triangle in Python. The angles of an equilateral triangle are all 60 degrees and the sides all have the same length.

Therefore, it is easy to create an equilateral triangle in Python.

We can easily define a function which will take in an integer argument representing side length. The function will then loop three times, creating a side of the given side length, and then rotating 120 degrees to create the next side.

Below is code to create an equilateral triangle with the turtle module in Python.

import turtle

t = turtle.Turtle()

def draw_equilateral_triangle(side_length):
    for i in range(0,3):
        t.forward(side_length)
        t.right(120)

draw_equilateral_triangle(100)

turtle equilateral triangle

How to Draw a Right Triangle in Python with turtle Module

You can also draw a right triangle in Python with the turtle module.

Drawing a right triangle is a little bit more difficult, as there are a few extra calculations which need to be performed.

First, let’s create a right triangle where the height and length of the triangle are equal. This is easy, as we know that the angles are 45, 45 and 90 in a right triangle with the same height and length.

The only thing we need to do is calculate the length of the hypotenuse. We can calculate the length of the hypotenuse with the Pythagorean theorem.

Below is how to create a right triangle with the turtle module in Python.

import turtle

t = turtle.Turtle()

def draw_right_triangle(side_length):
    hypotenuse = (side_length ** 2 + side_length ** 2) ** (1/2) 
    t.forward(side_length)
    t.right(90)
    t.forward(side_length)
    t.right(135)
    t.forward(hypotenuse)

draw_right_triangle(100)

turtle right triangle

If we have a right triangle with unequal sides, then we need to also calculate the angle to rotate. We can calculate the angle to rotate using the Python atan2() function and using the degrees function to get the angle in degrees.

Below is how to create a right triangle with unequal sides with the turtle module in Python.

import turtle
import math

t = turtle.Turtle()

def draw_right_triangle(height, length):
    hypotenuse = (height** 2 + length ** 2) ** (1/2) 
    angle = 180 - math.degrees(math.atan2(length,height))
    t.forward(length)
    t.right(90)
    t.forward(height)
    t.right(angle)
    t.forward(hypotenuse)

draw_right_triangle(100,200)

turtle right triangle unequal sides

Hopefully this article has been helpful for you to learn how to draw a triangle in Python.

Categorized in:

Python,

Last Update: March 1, 2024