In this tutorial, we will learn how to draw a simple triangle using Python. We will make use of the turtle graphics library, a powerful Python tool used to create geometrical shapes easily.
This tutorial will cover the basics of drawing a triangle along with an introduction to the turtle graphics library. By the end of this tutorial, you should be able to create simple geometrical shapes using Python.
Step 1: Installing the Turtle graphics library
Before we can draw our triangle, we first need to install the Turtle graphics library in Python. The turtle library comes pre-installed with Python, so there is no need to install any additional packages.
Step 2: Setting up the turtle environment
To start drawing with the turtle graphics library, we first need to import the module and create a turtle object. The turtle object will be used to draw our triangle. Additionally, we need to set up the drawing window where our turtle will move and create shapes. Here’s how you can do that:
1 2 3 4 5 6 7 |
import turtle # Create a new turtle object pen = turtle.Turtle() # Set up the drawing window window = turtle.Screen() |
Step 3: Drawing the triangle
Now that our environment is set up, we can proceed to draw the triangle. In this step, we will move the turtle object forward by a certain distance and then turn it by 120 degrees. We will repeat this process three times to create the triangle shape.
1 2 3 4 5 6 |
for i in range(3): # Move the turtle forward by 100 units pen.forward(100) # Turn the turtle 120 degrees to the left pen.left(120) |
Step 4: Finalizing the drawing
After drawing the triangle, we need to make sure the turtle drawing window stays open so we can see the result of our drawing. We can accomplish this by calling the window.mainloop()
function, which will keep the drawing window open until it is manually closed by the user.
1 2 |
# Keep the drawing window open window.mainloop() |
Full code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import turtle # Create a new turtle object pen = turtle.Turtle() # Set up the drawing window window = turtle.Screen() # Draw the triangle for i in range(3): # Move the turtle forward by 100 units pen.forward(100) # Turn the turtle 120 degrees to the left pen.left(120) # Keep the drawing window open window.mainloop() |
Output

Conclusion
In this tutorial, we learned how to draw a triangle using Python and the turtle graphics library. By following these steps, you should now be able to create simple geometrical shapes using Python.
The turtle graphics library provides many other useful methods and functions for more complex shapes and drawings, and you can explore the official turtle documentation to learn more about its capabilities.