How to Draw a Triangle in Python Turtle
Python has a simple pen drawing library called turtle. Using simple movement commands, we can draw shapes using the python turtle library. When teaching python to children, turtle is a good library to introduce to get children excited about the language and its features.
The basic actions used in the following examples are,
- Draw a line with pen - forward() command
- Move without drawing - penup(), pendown() commands
- Turn the pen to an angle - left(), right() commands
The following python program draws a simple equilateral triangle,
import turtle board = turtle.Turtle() board.forward(100) # draw base board.left(120) board.forward(100) board.left(120) board.forward(100) turtle.done()
The following python program draws a right angled triangle,
import turtle board = turtle.Turtle() board.forward(100) # draw base board.left(90) board.forward(100) board.left(135) board.forward(142) turtle.done()
The following python program draws a star shape by drawing two identical isosceles triangles,
import turtle board = turtle.Turtle() # first triangle for star board.forward(100) # draw base board.left(120) board.forward(100) board.left(120) board.forward(100) board.penup() board.right(150) board.forward(50) # second triangle for star board.pendown() board.right(90) board.forward(100) board.right(120) board.forward(100) board.right(120) board.forward(100) turtle.done()