How can a kid get started with coding in Python? A tutorial using turtle


Python tutorial for kids

Is Python for kids difficult? This is a how-long-is-a-piece-of-string type of question. Python is a vast and very powerful language. Several aspects of it, when one reaches the intermediate and advanced stages, are difficult, yes.


But that doesn't mean getting started with Python should be. The trick is to start with engaging projects and to learn things in a good order, using engaging project. So if you've never coded using Python before, why not have a go with this gentle tutorial.


First question: where do I write and run my Python code? Before wasting time and effort trying to install Python on your computer, just head to this page to use a web-based coding platform: codetoday.co.uk/code


Type your code in the blank area, and press the Run (Play) button whenever you want to run your code.


Getting started

Just like human languages, coding languages have words, grammar and punctuation.


Have a look at the first few lines of the code shown below, which when run, draws a simple house:

import turtle

The first line is asking the program to access the library where Python keeps all of its commands and bring in a set of commands known as `turtle`. This is a graphics package which gives us several command that allow us to draw anything we wish.

But first we must create something to draw with:

turtle.Turtle()

is the rather strange command that creates the graphical sprite, known as a Turtle, that allows us to draw on the screen (imagine a turtle moving and leaving a trail behind it). Notice the lowercase and uppercase letters and the punctuation marks (including the empty brackets); these details are very important in programming.


Rather than just writing turtle.Turtle() however, we can (and should) write:

fred = turtle.Turtle()

This means we have given the Turtle a name, making it a unique object in the program. This is very important as without a name we are not able to call the Turtle or ask it to do things for us.


Some of the things we can ask fred our Turtle to do is to move, change colour, change the thickness of the line it draws, turn left and right and more.


Have a look at the next few lines of now:


# draw the main outline of the house (red)
fred.color('red')
fred.pensize(5)

fred.forward(100)
fred.left(90)
fred.forward(100)
fred.left(90)
fred.forward(100)
fred.left(90)
fred.forward(100)
fred.left(90)

The first line starts with # (whose official name is octothorpe). This means that the computer program will ignore this line. This is a line written for us humans and is used to explain what sections of code are doing. You can see that this line is written in normal English and not in Python.


The next lines are all Python lines though. Look at the patterns. What do you notice about all of these lines?


All of the lines start with fred, followed by a dot; this means we are asking fred to do something for us. These commands ask fred to change its colour (although note that the command color is written in the American spelling in Python, with a missing 'u'), change the thickness of the drawing pen, move forward and turn left.

Run the program (by putting it in the coding platform and pressing the play button above the code) and see whether you can identify which part of the drawing these lines refer to.


Let's look at the next block of code now. Notice how the comments (the lines that start with #) give us a clue to what this bit of code does:


# draw the door (orange)
fred.forward(40)
fred.color('orange')

fred.begin_fill()
fred.forward(20)
fred.left(90)
fred.forward(40)
fred.left(90)
fred.forward(20)
fred.left(90)
fred.forward(40)
fred.end_fill()

Now coding is not about reading code written by someone else and copying it. Coding is about exploring, experimenting, and making mistakes. Yes, mistakes are the most important part of learning coding (and most things in life!)

Here is the full code so far:

import turtle

fred = turtle.Turtle()

# draw the main outline of the house (red)
fred.color('red')
fred.pensize(5)

fred.forward(100)
fred.left(90)
fred.forward(100)
fred.left(90)
fred.forward(100)
fred.left(90)
fred.forward(100)
fred.left(90)

# draw the door (orange)
fred.forward(40)
fred.color('orange')

fred.begin_fill()
fred.