• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Python turtle Module to Draw Square

Using Python turtle Module to Draw Square

February 23, 2022 Leave a Comment

To draw a square in Python, we can use the Python turtle module.

import turtle

t = turtle.Turtle()

def draw_square(length):
    for i in range(0,4):
        t.forward(length)
        t.right(90)

draw_square(100)

The turtle module in Python allows us to create graphics easily in our Python code.

We can use the turtle module to make all sorts of shapes in Python. For example, we can draw circles and draw triangles easily in Python with the turtle module.

Squares are easy to draw because every side has the same length. We can define a function which takes in the side length and loops four times, drawing each side and rotating 90 degrees until the loop is done.

Below is an example of how to draw a square in Python with the turtle module.

import turtle

t = turtle.Turtle()

def draw_square(length):
    for i in range(0,4):
        t.forward(length)
        t.right(90)

draw_square(100)

turtle square

How to Draw a Square with Different Colors in Python

With the turtle colors in Python, we can change the colors of our shapes easily.

The main function you can use to change the color of a line is with the turtle pencolor() function.

Below is an example and the output of how to draw a green square using pencolor() in Python.

import turtle

t = turtle.Turtle()

t.pencolor("green")

def draw_square(length):
    for i in range(0,4):
        t.forward(length)
        t.right(90)

draw_square(100)

turtle green square

With turtle, you can also easily fill shapes with color.

To fill a shape, there are a few steps to take. We use the fillcolor() function to define the fill color of our shape, and then use the begin_fill() and end_fill() functions to define when to begin and end filling shapes with the fill color.

Just like the pencolor() function, the fillcolor() function takes any valid color given a color mode.

Let’s take the example from above and fill our square with the color ‘light blue’ using fillcolor(), begin_fill() and end_fill() in Python.

import turtle

t = turtle.Turtle()

t.fillcolor("light blue")

t.pencolor("green")

t.begin_fill()

def draw_square(length):
    for i in range(0,4):
        t.forward(length)
        t.right(90)

draw_square(100)

t.end_fill()

turtle square green light blue fill

How to Draw a Rectangle in Python Using turtle Module

One other shape which is easy to make is a rectangle. With the function above, we can modify it to easily define a function which will draw a rectangle easily in Python.

Rectangles have four sides with different lengths for the height and width.

We can create a simple rectangle by defining a function that takes in two integers representing side length and side height. Then we can loop four times, using the forward() function to create a side representing either the length or height, then rotating the cursor 90 degrees with the right() function.

Below is a simple example of how to use Python to make a rectangle.

import turtle

t = turtle.Turtle()

def draw_rectangle(length, height):
    for i in range(0,4):
        if i % 2 == 0: 
            t.forward(length)
            t.right(90)
        else: 
            t.forward(height)
            t.right(90)

draw_rectangle(100, 200)

turtle rectangle

Hopefully this article has been helpful for you to learn how to draw a square in Python.

Other Articles You'll Also Like:

  • 1.  Get First Digit in Number Using Python
  • 2.  Using Python to Compare Strings Alphabetically
  • 3.  Using Python to Sum the Digits of a Number
  • 4.  Get pandas Column Names as List in Python
  • 5.  Remove All Instances of Value from List in Python
  • 6.  Calculate Compound Interest in Python
  • 7.  Get Substring Between Two Characters with Python
  • 8.  Using if in Python Lambda Expression
  • 9.  Check if Variable is String in Python
  • 10.  Change Python Turtle Background Color with screensize() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy