• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / How to Draw a Triangle in Python Using turtle Module

How to Draw a Triangle in Python Using turtle Module

February 22, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  How to Remove All Punctuation from String in Python
  • 2.  Format Numbers as Dollars in Python with format()
  • 3.  Remove Parentheses from String Using Python
  • 4.  How to Check if Tuple is Empty in Python
  • 5.  PROC REG Equivalent in Python
  • 6.  How to Read CSV File from AWS S3 Bucket Using Python
  • 7.  Inverting Dictionary Variables in Python with Dictionary Comprehension
  • 8.  Get Length of File Using Python
  • 9.  Creating a List of Zeros in Python
  • 10.  Python Print List – Printing Elements of List to the Console

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy