How to draw different shapes using the Python Turtle library?

PythonNumpyServer Side ProgrammingProgramming

In this program, we will draw different shapes using the Turtle library in Python. Turtle is a python feature like a drawing board, which lets you command a turtle to draw all over it. The different shapes that we are going to draw are square, rectangle, circle and a hexagon.

Algorithm

Step 1: Take lengths of side for different shapes as input.

Step 2: Use different turtle methods like forward() and left() for drawing different shapes.

Example Code

import turtle
t = turtle.Turtle()

#SQUARE
side = int(input("Length of side: "))
for i in range(4):
   t.forward(side)
   t.left(90)

#RECTANGLE
side_a = int(input("Length of side a: "))
side_b = int(input("Length of side b: "))
t.forward(side_a)
t.left(90)
t.forward(side_b)
t.left(90)
t.forward(side_a)
t.left(90)
t.forward(side_b)
t.left(90)

#CIRCLE
radius = int(input("Radius of circle: "))
t.circle(radius)

#HEXAGON
for i in range(6):
   t.forward(side)
   t.left(300)

Output

SQUARE:
Length of side: 100
RECTANGLE:
Length of side a: 100
Length of side b: 20
CIRCLE:
Radius of circle: 60

HEXAGON:Length of side: 100
raja
Published on 16-Mar-2021 09:42:59
Advertisements