Python Turtle Programming

Home » DIY & Tech Projects » Python Turtle Programming

Programming For Kids Using Python Turtle

You can let your kids learn their first coding lesson with a simple setup. Kids can learn how to write their first computer program through Python Turtle Graphics.

200+ Free Traffic Sources – The Complete List
Promote Your Website with Free Traffic
guide-free-traffic-sources
Get the Complete List as a PDF Document
fts-pdf-subscribe-form

Installing Python: Standalone, Python Shell, IDE

To learn Python coding, you will need a program to type in your codes. You have several options.

The first option is you can download and install the standalone program “Python Turtle.” which was developed by Ram Rachum.

You can download Python Turtle from:

http://pythonturtle.org/

The second option is to download and install Python on your Windows or Linux machine. Once it is set up, you can write codes through the Python Shell. The Python Shell is a window which looks similar to the command lines (of your Windows or Linux machine). You will access the “Turtle” module through Python Shell.

python-shell

You can download Python from:

https://www.python.org/

The third option is that on top of Python Shell, you are to download and install one of the IDEs. IDE stands for integrated development environment. One of the IDEs which is specifically developed for Python is Jetbrains’ Pycharm.

You can download Pycharm from:

https://www.jetbrains.com/pycharm/

If you have installed the simple program “Python Turtle”, then you are not required to install the conventional Python Shell or the IDE. But you are limited to only code and run very simple Python Turtle graphics programs.

The First Python Lesson

I am going to demonstrate the Python Turtle Graphics lessons through the Python Shell in most cases. Start the Python Shell, and enter the 3 lines.

import turtle
gordon = turtle.Pen()
gordon.shape("turtle")

The first line is to import the Turtle module. The second line we assign “gordon” (an instance of turtle) a pen to draw. This turtle is the main target of our program. The third line we give it a “turtle” shape.

After the first 3 lines, we get our turtle printed at the center of our screen. Python Turtle Graphics is a drawing program, and the screen is where our turtle will draw.

python-turtle-shape-250x250-1.png

Note the turtle is currently heading to the right (i.e. east). Let’s draw a straight line of length 100 by moving the turtle forward, and then turn the turtle 90 degrees to the left (i.e. counter-clockwise).

import turtle
gordon = turtle.Pen()
gordon.shape("turtle")
gordon.forward(100)
gordon.left(90)

python-turtle-forward-left-250x222-1.png

Let’s draw a square.

import turtle
gordon = turtle.Pen()
gordon.shape("turtle")
gordon.forward(100)
gordon.left(90)
gordon.forward(100)
gordon.left(90)
gordon.forward(100)
gordon.left(90)
gordon.forward(100)
gordon.left(90)

python-turtle-square-250x225-1.png

You repeated typing “forward(100)” and “left(90)” 4 times each, but you did not have to do so.

Now clear your screen and start over again.

gordon.reset()

With a “for” loop, you will achieve identical result. Note the last 2 lines (i.e. “forward(100)” and “left(90)”) must be indented for your program to work.

import turtle
gordon = turtle.Pen()
gordon.shape("turtle")
for i in range(4):
    gordon.forward(100)
    gordon.left(90)

Now make the turtle draw two squares which do not touch each other.

import turtle
gordon = turtle.Pen()
gordon.shape("turtle")
gordon.width(3)
for i in range(4):
    gordon.forward(20)
    gordon.left(90)
gordon.up()
gordon.forward(20)
gordon.down()
for i in range(4):
    gordon.forward(20)
    gordon.left(90)

draw-2-squares-250x209-1.png

This line increases the line width to 3.

gordon.width(3)

This line picks up the pen, so the turtle will not draw when moving.

gordon.up()

This line puts down the pen, so the turtle will start drawing again when moving.

gordon.down()

Now make the turtle draw multiple squares where all the squares are on different positions and have different sizes. Note we have achieved this by using a function.

import random
import turtle
gordon = turtle.Pen()
gordon.shape("turtle")
gordon.width(3)
gordon.speed(0)
def square(size):
    for i in range(100):
        gordon.forward(size)
        gordon.left(90)
for i in range(100):
    x = random.randrange(-200,200)
    y = random.randrange(-200,200)
    gordon.up()
    gordon.goto(x,y)
    gordon.down()
    square(random.randrange(10,200))

python-turtle-multiple-squares-blue-250x222-1.png

We import Python’s random number generation module.

import random

This line changes the line color to blue.

gordon.color("blue")

We speed up the drawing.

gordon.speed(0)

We declare function “square” which has a single parameter “size”.

def square(size):

We move the turtle to co-ordirnates x and y. Each iteration of the “for” loop changes the values of x and y. This way, the turtle is taken to different positions (e.g. x, y) to draw squares.

gordon.goto(x,y)

Let’s draw a different shape (e.g. Circle). Now draw 3 circles. The first circle has a diameter of 50 (i.e. diameter x 2 = 100). The second circle is half the size of the first circle. The third circle falls to the bottom half of the screen.

import turtle
gordon = turtle.Pen()
gordon.shape("turtle")
gordon.circle(100)
gordon.circle(50)
gordon.circle(-50)

draw-3-circles-250x242-1.png

Now we draw a maze.

import turtle
gordon = turtle.Pen()
gordon.shape("turtle")
gordon.color("blue")
gordon.width(3)
for i in range(20):
    gordon.forward(10*i)
    gordon.left(90)

python-turtle-maze

Lastly, we draw a spring (i.e. spiral) using multiple circles with different sizes.

import turtle
gordon = turtle.Pen()
gordon.shape("turtle")
gordon.speed(0)
for i in range(50):
    gordon.circle(i*3)
    gordon.left(10)

python-turtle-circles-spring-250x223-1.png

Articles on Technology Topics

Articles on Traffic & SEO Topics