Python Turtle : Drawing using Turtle library in python

  • Post author:
  • Reading time:4 mins read

Sharing is Caring !

Facebook
Twitter
LinkedIn
WhatsApp

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()  

Turtle Functions

Description

import turtle

Use Turtle module in python

turtle.speed(x)

animation speed of the turtle, 1=lowest, 10=fastest, 0=Off.

turtle.shape('turtle')

Set the shape, other options are arrow, square, circle, triangle and classic.

turtle.forward(x)

go forward by x.

turtle.backward(x)

go backward by x.

turtle.right(angle)

turn right by angle degrees.

turtle.left(angle)

turn left by angle degrees.

turtle.home()

go home(0,0) and face North.

turtle.goto(x,y)

go to position(x,y).

turtle.setheading(degrees)

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.

turtle.resizemode('auto')

use this at start of the program to change the size of the turtle when the pen size changes. Useful for stamping.

turtle.circle(radius)

draw a circle with the given radius.

turtle.circle(radius,angle)

draw a circle with the given radius and display according to given angle.

turtle.begin_fill()

This function can be used before drawing the shape that needs to be filled with color.

turtle.end_fill()

This function can be used after drawing the shape to be filled.

turtle.pendown()

put the pen down to draw.

turtle.penup()

lift the pen up from the screen.

turtle.pensize(x)

set the size of the pen to x.

turtle.pencolor(string)

set the color of the pen to the string.

turtle.fillcolor(string)

set the fill color to the string given.

turtle.color(string)

set both the fill color and the pen color to the given string.

turtle.color(string1,string2)

set the color of the fill as string1 and color of the pen as string2.

turtle.stamp()

stamp the current turtle shape onto the screen.

turtle.clearstamps()

Clear all stamps from the screen.

stampID=turtle.stamp()

stamps turtle onto the screen, sets the variable stampID to an integer, unique to each stamp.

turtle.clearstamp(stampID)

clear the stamp with the given stampID number.

Facebook
Twitter
LinkedIn
WhatsApp

Farees Ahmed

The aim of this blog is to serve as a quick reference guide for the Curious. Appreciate your feedback and comments !