How to Write a Python Turtle Program to Draw a Square?

Estimated read time 1 min read

To write a Python turtle program to draw a square, you can use the Turtle graphics library in Python. Here’s an example program:

import turtle

# Create a turtle object
t = turtle.Turtle()

# Draw a square
for i in range(4):
    t.forward(100)
    t.left(90)

# Exit on click
turtle.exitonclick()

In this program, we first import the turtle module and create a turtle object t. We then use a for loop to draw the square by moving the turtle forward by 100 units and turning it left by 90 degrees for each side. Finally, we use the exitonclick function to exit the turtle graphics window when the user clicks on it.

You can customize this program to draw squares of different sizes, colors, and line thicknesses by using other functions provided by the turtle module, such as t.pensize() to set the line thickness, t.color() to set the pen color, and t.begin_fill() and t.end_fill() to fill the shape with color.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply