Table of contents
1.
Introduction
2.
Steps to draw a Square turtle
2.1.
Approach 1
2.2.
Code
2.2.1.
Input
2.2.2.
Output
2.3.
Approach 2
2.4.
Code
2.4.1.
Input
2.4.2.
Output
3.
Steps to draw a Rectangle turtle
3.1.
Approach
3.2.
Code
3.2.1.
Input
3.2.2.
Output
4.
Frequently Asked Questions
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Square And Rectangle in Turtle

Introduction

Python Turtle is a helpful tool for learning Python's basic programming syntax. It can be used to teach children programming. It functions similarly to a canvas or easel board, allowing you to draw various figures by sending directions to the Turtle.

The turtle library in Python may be used to create different forms on a canvas. The programmer can direct a pen in a particular direction by specifying its heading and distance. A rectangle or square is not a built-in primitive in Turtle. However, in Turtle, creating a function to draw a rectangle (or square) is simple.

Steps to draw a Square turtle

Approach 1

  • Import a turtle and create an object from it.
     
  • You may take the length(x) of the square by the user or by yourself.
     
  • Move the turtle x units forward.
     
  • Turn the turtle 90 degrees to the left.
     
  • Iterate it four times so that it makes a closed figure.
     

Code

# draw a square in Python Turtle
# import turtle library
import turtle
  
ob = turtle.Turtle()
 
x = int(input("Enter the length of the Square: "))
 
# drawing first side
ob.forward(x) # Move the turtle forward by x units
ob.left(90) # Turn turtle by 90 degree
 
# drawing second side
ob.forward(x) # Move the turtle forward by x units
ob.left(90) # Turn turtle by 90-degree
 
# drawing third side
ob.forward(x) # Move the turtle forward by x units
ob.left(90) # Turn turtle by 90 degree
 
# drawing fourth side
ob.forward(x) # Move the turtle by x units
ob.left(90) # Turn the turtle by 90 degree
You can also try this code with Online Python Compiler
Run Code

 

Input

50

Output

You can compile it with online python compiler.

Approach 2

  • Import a turtle and create an object from it.
     
  • You may take the length(x) of the square by the user or by yourself.
     
  • Iterate it four times by loop.
     
  • Move the turtle x units forward.
     
  • Turn the turtle 90 degrees to the left.
     

Code

# draw Square in Python Turtle using loop
# import turtle library
import turtle
  
ob = turtle.Turtle() # taking input 
x = int(input("Enter the length of square: "))
 
for _ in range(4): # Iterating by loop
  ob.forward(x) # Forward turtle by x units
  ob.left(90) # Turn turtle by 90 degree
You can also try this code with Online Python Compiler
Run Code


Input

50

Output

Steps to draw a Rectangle turtle

Approach

  • Import a turtle and create an object from it.
     
  • You may take the rectangle's length(x) and breadth (y) by the user or by yourself.
     
  • Move the turtle x units forward and turn left by 90 degrees.
     
  • Move the turtle y units forward and turn left by 90 degrees.
     
  • Iterate it two times so that it makes a closed figure.
     

Code

# draw Rectangle in Python Turtle
# import turtle library
import turtle
  
ob = turtle.Turtle()
 
x = int(input("Enter the length of the Rectangle: "))
y = int(input("Enter the width of the Rectangle: "))
 
# drawing first side
ob.forward(x) # Forward turtle by x units
ob.left(90) # Turn turtle by 90 degree
 
# drawing second side
ob.forward(y) # Forward turtle by y units
ob.left(90) # Turn turtle by 90-degree
 
# drawing third side
ob.forward(x)# Forward turtle by x units
ob.left(90) # Turn turtle by 90 degree
 
# drawing fourth side
ob.forward(y) # Forward turtle by y units
ob.left(90) # Turn turtle by 90-degree
You can also try this code with Online Python Compiler
Run Code

 

Input

50

60

Output

You can also read about Palindrome Number in Python here.

Frequently Asked Questions

  1. The command for resetting the pen (turtle) is?
    We may reset the pen using the command turtle.reset(). After running this command, we get a blank page with an arrow on it. On this screen, we may then do any operation we want. 
     
  2. Which functions don't take any argument at all?
    The operations fillcolor(), goto(), and setheading() all take arguments, although the function position() doesn't. The position() function returns the turtle's current position.
     
  3. In Python, what does the turtle represent?
    The turtle is the name of the onscreen pen that you use to sketch with, and it is also the name of the library. In brief, the Python turtle library is a fun and engaging approach for new programmers to develop a feel for Python programming. Turtle is mostly used to introduce children to computer technology.
     
  4. Is turtle a pre-installed library?
    Yes, turtle comes in the standard python package, so it does not need to be downloaded explicitly. You can import it directly into your program.
     
  5. What is the purpose of the turtle module?
    The turtle module provides object-oriented and procedure-oriented turtle graphics primitives. It requires a version of Python with Tk support because it utilizes Tkinter for the underlying graphics.

Conclusion

In this article, we draw the square and rectangle using turtle in python. We hope that this blog will help you understand the concept of the turtle library and one of the applications, i.e., to draw the different types of shapes; if you like to learn more about it, check out our other blogs on the Python Libraries.

Recommended Readings: 

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Happy Coding!

Live masterclass