Sharing is Caring !
Python Turtle is a fun way to introduce coding to kids.
To be able to draw colorful shapes on the computer screen will definitely develop an interest and curiosity among the young learners.
This aim of this article is to serve as a quick reference guide for using turtle library in python for graphics programming.
The easiest way to start using turtle is by using the online IDE trinket.
You can directly write your code in the trinket window on the left side of the page and then click on the play/ execute tab to run the code.
You can see the results on the right side of the webpage.
I have listed below the commonly used turtle functions.
Sample code to draw a circle
import turtle # Initializing the turtle t = turtle.Turtle() r = 50 t.circle(r)
Sample code to draw a square
# draw square in Python Turtle import turtle t = turtle.Turtle() s = int(input("Enter the length of the side of the Squre: ")) # drawing first side t.forward(s) # Forward turtle by s units t.left(90) # Turn turtle by 90 degree # drawing second side t.forward(s) # Forward turtle by s units t.left(90) # Turn turtle by 90 degree # drawing third side t.forward(s) # Forward turtle by s units t.left(90) # Turn turtle by 90 degree # drawing fourth side t.forward(s) # Forward turtle by s units t.left(90) # Turn turtle by 90 degree
Sample code to draw tangent circles
import turtle t = turtle.Turtle() # radius for smallest circle r = 10 # number of circles n = 10 # loop for printing tangent circles for i in range(1, n + 1, 1): t.circle(r * i)
Sample code to draw spiral circles
import turtle t = turtle.Turtle() # taking radius of initial radius r = 10 # Loop for printing spiral circle for i in range(100): t.circle(r + i, 45)
Sample code to draw co centric circles
import turtle t = turtle.Turtle() # radius of the circle r = 10 # Loop for printing concentric circles for i in range(50): t.circle(r * i) t.up() t.sety((r * i)*(-1)) t.down()
Use Turtle module in python
animation speed of the turtle, 1=lowest, 10=fastest, 0=Off.
Set the shape, other options are arrow, square, circle, triangle and classic.
go forward by x.
go backward by x.
turn right by angle degrees.
turn left by angle degrees.
go home(0,0) and face North.
go to position(x,y).
compass orientation, 0=North, 90=East, 180= South, 270=West.
x,y = turtle.pos()
set the variables x and y according to turtle current position.
use this at start of the program to change the size of the turtle when the pen size changes. Useful for stamping.
draw a circle with the given radius.
draw a circle with the given radius and display according to given angle.
This function can be used before drawing the shape that needs to be filled with color.
This function can be used after drawing the shape to be filled.
put the pen down to draw.
lift the pen up from the screen.
set the size of the pen to x.
set the color of the pen to the string.
set the fill color to the string given.
set both the fill color and the pen color to the given string.
set the color of the fill as string1 and color of the pen as string2.
stamp the current turtle shape onto the screen.
Clear all stamps from the screen.
stamps turtle onto the screen, sets the variable stampID to an integer, unique to each stamp.
clear the stamp with the given stampID number.