Python Turtle Programming for Kids


Python Turtle Programming

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.

Python Turtle Program


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:

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:

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 Lession

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.

Turtle on Center of Screen

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)

Turtle Moved Forward and Left

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)

Square

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)

2 Squares

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

Multiple Squares Different Sizes

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)

3 Circles

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)

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)

Spring (Spiral)








Developed by Gordon Choi
Copyright @ 2006-2017 www.gordonchoi.com
Analytics Powered by Folks Analytics

Gordon Choi’s Books:
The China Mobile SEO Book
Analytics Book
Mobile Website Book