Turtle Programming in Python

18 Oct, 2021
“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 : 
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:  
Import the turtle moduleCreate a turtle to control.Draw around using the turtle methods.Run turtle.done().
Import the turtle module
Create a turtle to control.
Draw around using the turtle methods.
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 Programmingimport turtleskk = 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 Programmingimport turtlestar = 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 Programmingimport turtlepolygon = turtle.Turtle() num_sides = 6side_length = 70angle = 360.0 / num_sides for i in range(num_sides):    polygon.forward(side_length)    polygon.right(angle)     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. You can access these codes for wonderful turtle programs here  
1. Spiral Square Outside In and Inside Out 
Python
# Python program to draw# Spiral Square Outside In and Inside Out# using Turtle Programmingimport turtle   #Outside_Inwn = turtle.Screen()wn.bgcolor("light green")wn.title("Turtle")skk = turtle.Turtle()skk.color("blue") def sqrfunc(size):    for i in range(4):        skk.fd(size)        skk.left(90)        size = size-5 sqrfunc(146)sqrfunc(126)sqrfunc(106)sqrfunc(86)sqrfunc(66)sqrfunc(46)sqrfunc(26)
Python
import turtle  #Inside_Outwn = 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 Programmingimport turtle   #Outside_Inimport turtleimport timeimport 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 = 0y = 0turtle.setpos(x, y)  numshapes = 8for 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 turtleloadWindow = turtle.Screen()turtle.speed(2) for i in range(100):    turtle.circle(5*i)    turtle.circle(-5*i)    turtle.left(i) turtle.exitonclick()
Output: 
Turtle Programming in Python - YouTubeAmartya R Saikia277 subscribersTurtle Programming in PythonWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 5:01•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=_FxzEAIWvtE" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
4. Rainbow Benzene 
Python
# Python program to draw# Rainbow Benzene# using Turtle Programmingimport turtlecolors = ['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
Trees in Python using Turtle - YouTubeAmartya R Saikia277 subscribersTrees in Python using TurtleWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 0:29•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=NA1yaRGAjbY" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
 References: 
Turtle documentation for Python 3 and 2
eecs.wsu.edu [PDF] !
This article is contributed by Amartya Ranjan Saikia. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
anshitaagarwal
raaschatterjee11
allanturning94
sumitgumber28
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Python OOPs Concepts