The Wayback Machine - https://web.archive.org/web/20240930150527/https://www.geeksforgeeks.org/turtle-programming-python/
Open In App

Turtle Programming in Python

Last Updated : 21 Mar, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

“Turtle” is a Python feature like a drawing board, which lets us command a turtle to draw all over it! We can use functions like turtle.forward(…) and turtle.right(…) which can move the turtle around. Commonly used turtle methods are :
 

Method Parameter Description
Turtle() None Creates and returns a new turtle object
forward() amount Moves the turtle forward by the specified amount
backward() amount Moves the turtle backward by the specified amount
right() angle Turns the turtle clockwise
left() angle Turns the turtle counterclockwise
penup() None Picks up the turtle’s Pen
pendown() None Puts down the turtle’s Pen
up() None Picks up the turtle’s Pen
down() None Puts down the turtle’s Pen
color() Color name Changes the color of the turtle’s pen
fillcolor() Color name Changes the color of the turtle will use to fill a polygon
heading() None Returns the current heading
position() None Returns the current position
goto() x, y Move the turtle to position x,y
begin_fill() None Remember the starting point for a filled polygon
end_fill() None Close the polygon and fill with the current fill color
dot() None Leave the dot at the current position
stamp() None Leaves an impression of a turtle shape at the current location
shape() shapename Should be ‘arrow’, ‘classic’, ‘turtle’ or ‘circle’

Plotting using Turtle

To make use of the turtle methods and functionalities, we need to import turtle.”turtle” comes packed with the standard Python package and need not be installed externally. The roadmap for executing a turtle program follows 4 steps:  

  1. Import the turtle module
  2. Create a turtle to control.
  3. Draw around using the turtle methods.
  4. Run turtle.done().

So as stated above, before we can use turtle, we need to import it. We import it as : 

from turtle import *
# or
import turtle

After importing the turtle library and making all the turtle functionalities available to us, we need to create a new drawing board(window) and a turtle. Let’s call the window as wn and the turtle as skk. So we code as: 

wn = turtle.Screen()
wn.bgcolor("light green")
wn.title("Turtle")
skk = turtle.Turtle()

Now that we have created the window and the turtle, we need to move the turtle. To move forward 100 pixels in the direction skk is facing, we code: 

skk.forward(100)

We have moved skk 100 pixels forward, Awesome! Now we complete the program with the done() function and We’re done! 

turtle.done()

So, we have created a program that draws a line 100 pixels long. We can draw various shapes and fill different colors using turtle methods. There’s plethora of functions and programs to be coded using the turtle library in python. Let’s learn to draw some of the basic shapes. 
 

Shape 1: Square

Python




# Python program to draw square 
# using Turtle Programming
import turtle 
skk = turtle.Turtle()
  
for i in range(4):
    skk.forward(50)
    skk.right(90)
      
turtle.done()


Output:

Shape 2: Star

Python3




# Python program to draw star
# using Turtle Programming
import turtle
star = turtle.Turtle()
  
star.right(75)
star.forward(100)
  
for i in range(4):
    star.right(144)
    star.forward(100)
      
turtle.done()


Output:

Shape 3: Hexagon

Python




# Python program to draw hexagon
# using Turtle Programming
import turtle 
polygon = turtle.Turtle()
  
num_sides = 6
side_length = 70
angle = 360.0 / num_sides 
  
for i in range(num_sides):
    polygon.forward(side_length)
    polygon.right(angle)
      
turtle.done()


Output:

Shape 4: parallelogram

Python




import turtle
  
# Initialize the turtle
t = turtle.Turtle()
  
# Set the turtle's speed
t.speed(1)
  
# Draw the parallelogram
for i in range(2):
    t.forward(100)
    t.left(60)
    t.forward(50)
    t.left(120)


Output:

 

Shape 5 : Circle

Python




import turtle
  
# Set up the turtle screen and set the background color to white
screen = turtle.Screen()
screen.bgcolor("white")
  
# Create a new turtle and set its speed to the fastest possible
pen = turtle.Turtle()
pen.speed(0)
  
# Set the fill color to red
pen.fillcolor("red")
pen.begin_fill()
  
# Draw the circle with a radius of 100 pixels
pen.circle(100)
  
# End the fill and stop drawing
pen.end_fill()
pen.hideturtle()
  
# Keep the turtle window open until it is manually closed
turtle.done()


Output:

 

Visit pythonturtle.org to get a taste of Turtle without having python pre-installed. The shell in PythonTurtle is a full Python shell, and you can do with it almost anything you can with a standard Python shell. You can make loops, define functions, create classes, etc. 

Some amazing Turtle Programs

1. Spiral Square Outside In and Inside Out 

Python




import turtle  #Inside_Out
wn = turtle.Screen()
wn.bgcolor("light green")
skk = turtle.Turtle()
skk.color("blue")
  
def sqrfunc(size):
    for i in range(4):
        skk.fd(size)
        skk.left(90)
        size = size + 5
  
sqrfunc(6)
sqrfunc(26)
sqrfunc(46)
sqrfunc(66)
sqrfunc(86)
sqrfunc(106)
sqrfunc(126)
sqrfunc(146)


Output: 
 

https://www.youtube.com/watch?v=QPf

2. User Input Pattern 

Python




# Python program to user input pattern
# using Turtle Programming
import turtle   #Outside_In
import turtle
import time
import random
  
print ("This program draws shapes based on the number you enter in a uniform pattern.")
num_str = input("Enter the side number of the shape you want to draw: ")
if num_str.isdigit():
    squares = int(num_str)
  
angle = 180 - 180*(squares-2)/squares
  
turtle.up
  
x = 0 
y = 0
turtle.setpos(x, y)
  
  
numshapes = 8
for x in range(numshapes):
    turtle.color(random.random(), random.random(), random.random())
    x += 5
    y += 5
    turtle.forward(x)
    turtle.left(y)
    for i in range(squares):
        turtle.begin_fill()
        turtle.down()
        turtle.forward(40)
        turtle.left(angle)
        turtle.forward(40)
        print (turtle.pos())
        turtle.up()
        turtle.end_fill()
  
time.sleep(11)
turtle.bye()


3. Spiral Helix Pattern 

Python




# Python program to draw 
# Spiral  Helix Pattern
# using Turtle Programming
  
import turtle
loadWindow = turtle.Screen()
turtle.speed(2)
  
for i in range(100):
    turtle.circle(5*i)
    turtle.circle(-5*i)
    turtle.left(i)
  
turtle.exitonclick()


Output: 

4. Rainbow Benzene 

Python




# Python program to draw 
# Rainbow Benzene
# using Turtle Programming
import turtle
colors = ['red', 'purple', 'blue', 'green', 'orange', 'yellow']
t = turtle.Pen()
turtle.bgcolor('black')
for x in range(360):
    t.pencolor(colors[x%6])
    t.width(x//100 + 1)
    t.forward(x)
    t.left(59)


Output: 

 

Trees using Turtle Programming

 References: 



Similar Reads

turtle.forward() method in Python-Turtle
The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.forward() The turtle.forward() method is used to move the turtle forward by the value of the argument that it takes. It gives
1 min read
turtle.setpos() and turtle.goto() functions in Python
The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.setpos() This method is used to move the turtle to an absolute position. This method has Aliases: setpos, setposition, goto. S
1 min read
Turtle Race Game Using Python - Turtle Graphics Library
Turtle graphics is a popular way to introduce programming concepts to beginners. It's a fun and interactive module in Python that lets you create simple drawings and animations using a "turtle" that moves around the screen. In this tutorial, we'll create an exciting turtle race game where you can bet on the color of a turtle and watch as they race
4 min read
Draw house using Turtle programming in Python
Prerequisite: Turtle Programming in Python “Turtle” is a Python feature like a drawing board, which lets us command a turtle to draw all over it! ”turtle” comes packed with the standard Python package and need not be installed externally. Step 1: Import turtle and math module in Python. import turtle import math Step 2: Choose a background color fo
2 min read
Draw Circle in Python using Turtle
Turtle is a Python feature like a drawing board, which lets us command a turtle to draw all over it! We can use functions like turtle.forward(…) and turtle.right(…) which can move the turtle around. Turtle is a beginner-friendly way to learn Python by running some basic commands and viewing the turtle do it graphically. It is like a drawing board t
3 min read
turtle.right() method in Python
The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.right() The turtle.right() method is used to change the direction of the turtle by the value of the argument that it takes. It
2 min read
Draw a Hut using turtle module in Python
Turtle is a inbuilt module in Python, which has many functions like forward(), backward(), right() and left() etc. You can draw a hut on the screen just by using the turtle module in Python. In this article, we will create a hut using the turtle module. Approach: Import turtleSet the background color.Define a function to draw the front portion of t
2 min read
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: C/C++ Code # draw square in Python Turtle import t
3 min read
Draw Color Filled Shapes in Turtle - Python
Prerequisite: Python Turtle 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. To move turtle, there are some functions i.e forward(), backward(), etc. To fill the colors in the shapes drawn by turtle, turtle provides three funct
4 min read
Draw any polygon in Turtle - Python
Prerequisite: Python Turtle Basics Turtle is an inbuilt module of python. It enables us to draw any drawing by a turtle and methods defined in the turtle module and by using some logical loops. turtle drawings are basically drawn using four methods defined in the turtle module. forward(x): moves the turtle(pen) in the forward direction by x unit. b
2 min read
Python - Hilbert Curve using turtle
Fractal is a curve or a figure which repeats itself. It comprises a recursive pattern that repeats itself up to a desired level of nesting. Turtle graphics are provided in the turtle module which is used for drawing various shapes and patterns in Python. A Hilbert curve is a curve that is formed by connecting a sequence of U-shaped curves arranged
4 min read
Draw Colourful Star Pattern in Turtle - Python
In this article we will use Python's turtle library to draw a spiral of stars, filled with randomly generated colours. We can generate different patterns by varying some parameters. modules required: turtle: turtle library enables users to draw picture or shapes using commands, providing them with a virtual canvas. turtle comes with Python's Standa
2 min read
Y Fractal tree in Python using Turtle
A fractal is a never-ending pattern. Fractals are infinitely complex patterns that are self-similar across different scales. They are created by repeating a simple process over and over in an ongoing feedback loop. Driven by recursion, fractals are images of dynamic systems – the pictures of Chaos. In this article, we will draw a colorful Y fractal
2 min read
Draw a Tic Tac Toe Board using Python-Turtle
The Task Here is to Make a Tic Tac Toe board layout using Turtle Graphics in Python. For that lets first know what is Turtle Graphics. Turtle graphics In computer graphics, turtle graphics are vector graphics using a relative cursor upon a Cartesian plane. Turtle is drawing board like feature which let us to command a turtle and draw using it. Feat
2 min read
Create digital clock using Python-Turtle
Turtle is a special feature of Python. Using Turtle, we can easily draw on a drawing board. First, we import the turtle module. Then create a window, next we create a turtle object and using the turtle methods we can draw in the drawing board. Prerequisites: Turtle Programming in Python Installation: To install this module type the below command in
3 min read
How to draw color filled star in Python-Turtle?
Prerequisite: Turtle Programming Basics, Draw Color Filled Shapes in Turtle 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 the turtle, there are some functions i.e forward(), backward(), etc. Approach: The following st
2 min read
Draw Cube and Cuboid in Python using Turtle
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 the turtle, there are some functions i.e forward(), backward(), etc. Drawing Cube Following steps are used: First draw the front sq
2 min read
Draw Shape inside Shape in Python Using Turtle
Prerequisites: Turtle Programming in Python Turtle is a Python feature like a drawing board, which let us command a turtle to draw all over it! We can use many turtle functions which can move the turtle around. Turtle comes in the turtle library. The turtle module can be used in both object-oriented and procedure-oriented ways. Some of the commonly
3 min read
Draw Colored Solid Cube using Turtle in Python
Turtle is an inbuilt module in Python. It provides: Drawing using a screen (cardboard).Turtle (pen). To draw something on the screen, we need to move this turtle (pen) and to move the turtle(pen), there are some functions like the forward(), backward(), etc. Prerequisite: Turtle Programming Basics Drawing Colored Solid Cube In this section, we will
2 min read
Draw smiling face emoji using Turtle in Python
Prerequisite: Python Turtle 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. To move turtle, there are some functions i.e forward(), backward(), etc. In this article, we will see how to draw a smiling face emoji using the Turtl
2 min read
Draw Clock Design using Turtle in Python
Prerequisites: Turtle Programming in Python 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. To draw Clock Design : Following steps are used : Import turtl
2 min read
Draw Concentric Circles with VIBGYOR Using Turtle in 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. To draw Concentric VIBGYOR : Following steps are used : Importing
2 min read
Colored Flower by circles using Turtle in 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. To draw Flower : Following steps are used : Import TurtleSet scree
1 min read
Draw Panda Using Turtle Graphics in Python
Turtle is an inbuilt module in Python. It provides: Drawing using a screen (cardboard).Turtle (pen). To draw something on the screen, we need to move the turtle (pen), and to move the turtle, there are some functions like the forward(), backward(), etc. Prerequisite: Turtle Programming Basics Draw Panda Using Turtle Graphics In this section, we wil
2 min read
Draw Heart Using Turtle Graphics in Python
Turtle is an inbuilt module in Python. It provides: Drawing using a screen (cardboard).Turtle (pen). To draw something on the screen, we need to move the turtle (pen) and to move the turtle, there are some functions like the forward(), backward(), etc Prerequisite: Turtle Programming Basics Draw Heart Using Turtle Graphics In this section, we will
2 min read
Draw Rainbow using Turtle Graphics in Python
Turtle is an inbuilt module in Python. It provides: Drawing using a screen (cardboard).Turtle (pen). To draw something on the screen, we need to move the turtle (pen), and to move the turtle, there are some functions like the forward(), backward(), etc. Prerequisite: Turtle Programming Basics Draw Rainbow Using Turtle Graphics In this section, we w
2 min read
Draw Ellipse Using Turtle in 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. Approach: The following steps are used : Import turtleDivide the e
2 min read
Draw Graph Grid Using Turtle in 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. Approach: Following steps are used : Import turtleSet screenMake t
2 min read
Draw Dot Patterns Using Turtle in 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. 1) Draw Dot Square Following steps are used : Import turtleMake tu
2 min read
Draw Chess Board Using Turtle in 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. For drawing Chess Board following steps are used : Import turtle a
2 min read
Article Tags :
Practice Tags :