How to Draw a Triangle in Python Turtle

How to Draw a Triangle in Python Turtle

Have you ever wondered, How to create graphics and animations in Python? Yes, We can be able to achieve it by using an inbuilt Python module called Turtle. In this article, We are going to learn to Draw a Triangle in Python Turtle

The Turtle module provides a set of functions that allow you to control a virtual turtle on a screen, which can be used to draw shapes, patterns, and other attractive graphics.

Before proceeding further, Let’s understand the basic action that the virtual turtle can able to do

Basic Actions:

  • Draw a line – forward() & backward()
  • To change the angle – right() & left()
  • Move without drawing a line – penup() 
  • Move while drawing a line – pendown()

With the above basic actions, We are going to create a triangle. For more action, you can check the office turtle module page

A step-by-step guide to draw a Triangle

To get started, We need to import the necessary modules, In this case, it is the turtle module

Import Module:

Open a new Python project and import the module

import turtle

Create Turtle Object:

Next, we need to create a turtle object that we will use to draw the triangle. Add the  below line to create a turtle object

t = turtle.Turtle()

This creates a new turtle object named “t”. We can now use this object to control the turtle.

Drawing the Triangle

To draw a triangle, we need to move the turtle forward and turn it. Here’s the code to draw a triangle:

t.forward(100) 

t.left(120) 

t.forward(100) 

t.left(120) 

t.forward(100)

The first line of code moves the turtle forward by 100 units. The second line of code turns the turtle 120 degrees to the left. The third line of code moves the turtle forward by 100 units again. The fourth line of code turns the turtle 120 degrees to the left. The fifth line of code moves the turtle forward by 100 units again.

The end result is a triangle with sides of 100 units each. You can experiment with different values to draw triangles of different sizes.

Hiding the Turtle

After drawing the triangle, we might want to hide the turtle so that it doesn’t appear on the screen anymore. To do this, we can add the following code:

t.hideturtle()

This hides the turtle from view.

Exiting the Window

Finally, we need to tell Python Turtle to exit the window when we click on it. To do this, we can add the following code:

turtle.exitonclick()

The above line of code waits for a click event on the window, and then exits the window when a click is detected.

Final Code to create a triangle:

Here’s the complete code to draw a triangle using Python Turtle:

import turtle 

# Create a turtle object 

t = turtle.Turtle()

# Draw a triangle 

t.forward(100) 

t.left(120) 

t.forward(100) 

t.left(120) 

t.forward(100) 

# Hide the turtle 

t.hideturtle() 

# Exit the window when clicked 

turtle.exitonclick()

Output:

Create a Triangle

Draw Multiple Triangles

Let’s make this interesting by creating multiple triangles in series, For this, we are going to edit the above code to draw multiple Traisngle

Create a function:

I am creating a function called triangle() to easy coding as we are going to draw multiple triangles creating a function can reduce the number of lines

def triangle():

# Draw a triangle

   t.forward(100)

   t.left(120)

   t.forward(100)

   t.left(120)

   t.forward(100)

Draw the next triangle:

To draw the next triangle series to each other, We are going to introduce penup and pen down action to move the turtle without drawing any new lines. 

triangle()

t.penup()

t.left(120)

t.forward(100)

t.pendown()

triangle()

In the above code, We are calling the custom function triangle() to draw a triangle. Once done, We are lifting the pen using t.penup(), So that it will not write while moving. Next, With the help of left(120) forward(100) action, We are placing the turtle in the correct position for drawing the next triangle. 

Once done, Calling pendown() to draw the line and call the custom function triangle() to draw  the next triangle. By repeating the action, We can create multiple triangles 

Final Code to draw multiple triangles:

import turtle

# Create a turtle object

t = turtle.Turtle()

def triangle():

# Draw a triangle

   t.forward(100)

   t.left(120)

   t.forward(100)

   t.left(120)

   t.forward(100)

triangle()

#Move the turtle to the next position

t.penup()

t.left(120)

t.forward(100)

t.pendown()

triangle()

#Move the turtle to the next position

t.penup()

t.left(120)

t.forward(100)

t.pendown()

triangle()

# Hide the turtle

t.hideturtle()

# Exit the window when clicking

turtle.exitonclick()

Output:
Multiple Triangle

The end result is multiple triangles in series with sides of 100 units each. You can try the experiment with different values to draw triangles of different sizes.

Conclusion

In this article, we have learned to draw single and multiple triangles using Python Turtle. We covered the basics of creating a turtle object, moving the turtle, turning the turtle, hiding the turtle, and exiting the window. With these basic concepts, you can start creating more complex shapes and animations using Python Turtle. 

These exercises would be helpful for young coding members to gain more interest in Learning python 

Good luck with your Learning !!

Related Topics:

How to Save Dictionary as JSON Python

Resolve “ImportError: Attempted Relative Import with No Known Parent Package”

AttributeError: ‘dict’ object has no attribute ‘append’

Coding Spell Python Journey | About Us

Jerry Richard R
Follow me