How to draw square and rectangle in Python Turtle?

Two functions that are useful for us for drawing square and rectangle are- forward() and left(). Before we draw any of the shapes, we must know the basic properties of them. Let's start with a square. All the sides of a square are equal. And the angle between two adjacent sides is 90°. Opposite sides are parallel to each other.

Now, we know the basic features of the square. It's time to draw a square in Python Turtle. Let's assume the side of a square is 100 units.

#Program to draw square in Python Turtle
import turtle

t = turtle.Turtle()
t.forward(100) #Forward turtle by 100 units
t.left(90) #Turn turtle by 90 degree
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)

Output of the above program

Draw square in python turtle

Explanation of the above code-

import turtle
t = turtle.Turtle()

Here, we are importing the turtle module. Then, we have created a new drawing board and assigned it to an object t.

forward(100)
left(90)

We have moved the turtle in the forward direction by 100 units. Because the side of a square is 100 units. Then, we have turned the turtle by 90° as the angle between adjacent sides is 90°. This finishes one side of the square. The same statements are repeated three more times to draw the remaining three sides.

Using loops to draw square in Turtle

You can see in the above code that we have used the same statements(forward(100) and left(90)) four times. So, instead of writing them again and again, we can use a loop that will run 4 times.

#Using loop to draw square in Python Turtle
import turtle

t = turtle.Turtle()
for i in range(4): # for loop will run 4 times
  t.forward(100) #Forward turtle by 100 units
  t.left(90) #Turn turtle by 90 degree

When you run this program, you will the same output.

Drawing Rectangle in Python Turtle

We know that opposite sides of a rectangle are equal. The angle between two adjacent sides of a rectangle is 90° By keeping these properties in mind, we will draw the rectangle. Suppose the length of the rectangle is 150 units and its breadth is 80 units. Run the below code to obtain rectangle in turtle.

#Program to draw rectangle in Python Turtle
import turtle

t = turtle.Turtle()
t.forward(150) #Forward turtle by 150 units
t.left(90) #Turn turtle by 90 degree
t.forward(80) #Forward turtle by 80 units
t.left(90) #Turn turtle by 90 degree
t.forward(150) #Forward turtle by 150 units
t.left(90) #Turn turtle by 90 degree
t.forward(80) #Forward turtle by 80 units
t.left(90) #Turn turtle by 90 degree

Output of the above program

Draw rectangle in python turtle

Explanation of the above code-

t.forward(150)
t.left(90)
t.forward(80)
t.left(90)

We have moved the turtle in the forward direction by 150 units. Because the length of a rectangle is 150 units. Then, we have turned the turtle by 90° as the angle between adjacent sides is 90°. This finishes one side of the rectangle. Then we forwarded the turtle by 80 units and turned it by 90° This completes second side of the rectangle. The same statements are repeated one more times to draw the remaining two sides.

Using loops to draw rectangle in Turtle

Can you think, which set of statements we have to place in for loop? We will place forward(150), left(90), forward(80) and left(90) in a for loop and run it for 2 times.

#Using loop to draw rectangle in Python Turtle
import turtle

t = turtle.Turtle()
for i in range(2):
  t.forward(150) #Forward turtle by 150 units
  t.left(90) #Turn turtle by 90 degree
  t.forward(80) #Forward turtle by 80 units
  t.left(90) #Turn turtle by 90 degree

Recommended Posts