Draw Square and Rectangle in Turtle – Python

Prerequisite: Turtle Programming Basics

turtle is an inbuilt module in Python. It provides drawing using a screen (cardboard) and turtle (pen). To draw something on the screen, we need to move the turtle (pen). To move turtle, there are some functions i.e forward(), backward(), etc.

Drawing Square:

filter_none

edit
close

play_arrow

link
brightness_4
code

# draw square in Python Turtle
import turtle
   
t = turtle.Turtle()
  
s = int(input("Enter the length of the side of the Squre: "))
  
# drawing first side
t.forward(s) # Forward turtle by s units
t.left(90) # Turn turtle by 90 degree
  
# drawing second side
t.forward(s) # Forward turtle by s units
t.left(90) # Turn turtle by 90 degree
  
# drawing third side
t.forward(s) # Forward turtle by s units
t.left(90) # Turn turtle by 90 degree
  
# drawing fourth side
t.forward(s) # Forward turtle by s units
t.left(90) # Turn turtle by 90 degree

chevron_right


Input :



100

Output :

Second approach (Using Loop) :

filter_none

edit
close

play_arrow

link
brightness_4
code

# draw Squre in Python Turtle
import turtle
   
t = turtle.Turtle()
  
s = int(input("Enter the length of the side of squre: "))
  
for _ in range(4):
  t.forward(s) # Forward turtle by s units
  t.left(90) # Turn turtle by 90 degree

chevron_right


Input :

100

Output :

Drawing Rectangle:

filter_none

edit
close

play_arrow

link
brightness_4
code

# draw Rectangle in Python Turtle
import turtle
   
t = turtle.Turtle()
  
l = int(input("Enter the length of the Rectangle: "))
w = int(input("Enter the width of the Rectangle: "))
  
# drawing first side
t.forward(l) # Forward turtle by l units
t.left(90) # Turn turtle by 90 degree
  
# drawing second side
t.forward(w) # Forward turtle by w units
t.left(90) # Turn turtle by 90 degree
  
# drawing third side
t.forward(l) # Forward turtle by l units
t.left(90) # Turn turtle by 90 degree
  
# drawing fourth side
t.forward(w) # Forward turtle by w units
t.left(90) # Turn turtle by 90 degree

chevron_right


Input :

100
120

Output :

Second approach (Using Loop) :

filter_none

edit
close

play_arrow

link
brightness_4
code

# draw Rectangle in Python Turtle
import turtle
   
t = turtle.Turtle()
  
l = int(input("Enter the length of the Rectangle: "))
w = int(input("Enter the width of the Rectangle: "))
  
for _ in range(4):
    
  # drawing length
  if _% 2 == 0:
    t.forward(l) # Forward turtle by l units
    t.left(90) # Turn turtle by 90 degree
  
  # drawing width
  else:
    t.forward(w) # Forward turtle by w units
    t.left(90) # Turn turtle by 90 degree

chevron_right


Input :

100
120

Output :




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.