Write Your First Python Program Using Turtle: A Coding Lesson for Kids


Never tried coding in Python before? Perhaps you have coded in Scratch, or maybe you have never done any coding before?


Coding using a language like Python can look very different to block coding like Scratch, but don't let that put you off. In this article you will learn how to write your first program using Python. Once you complete this lesson, you will then be able to continue exploring Python and write code to draw anything you can think of.


This lesson has two versions:

— The written article you are reading now that will guide you through the lesson

— A video that will also guide you through the same steps but in a different format


You can choose to use either the written text or the video, or even both if you prefer. Whatever works best for you.




Here is what you will learn:

— You will understand how Python works

— You will learn how to make drawings using turtle, a Python module

— You will learn about the basic grammar for coding in Python


To write and run your Python code you can go to this page: https://www.codetoday.co.uk/code I recommend you have both this page and the coding platform open at the same time and swap between them, or you can even have them open side-by-side.


Are you ready to start coding?


Python is a language. Like other languages that we speak, such as English, Python has words that have a certain meaning, and grammar which sets the rules on how to use these words. The punctuation is also very important in coding languages such as Python.


The first thing you need is to fetch a book from the library where Python keeps all its commands:


import turtle

This brings in a book called turtle which has lots of commands that you can use to draw in Python. It's a strange name for a book used to draw! You will see why it's called turtle soon!


To be able to draw, you will need to create a 'drawing turtle', this is a computer turtle (not a real one, of course!) that will move around the screen drawing lines and other shapes following your commands.


So time to create a turtle now:


fred = turtle.Turtle() 

Does this line look a bit strange? Don't worry I'll explain what everything means. First, remember that this is not English or Maths, but Python. We are writing in a new language, so the rules are different to English or other languages.


First, I chose a name for the turtle. I called him fred, but you can choose another name is you prefer. There are some rules on naming the turtle, such as you cannot have spaces in the names. So, keep it simple for now! We also do not use capital letters at the start of the name.


The equals sign shows Python that you're going to tell it who fred is. To do this you then wrote turtle.Turtle(). The first turtle is the book with commands you have imported from the library on the first line. The second Turtle is the one that creates the drawing turtle. Notice how the second Turtle has a capital T and brackets at the end. These things are really important in Python, so we need to be very precise when we write code in Python.


Time to Draw


Let's ask fred to move and draw a line now. It would be nice if we could say:


"Hey Fred, could you please move forward a bit. Thank you."


That's English, but you need to speak to the computer in Python. Here's how we can translate the line above into Python:


fred.forward(100)

You have just asked fred to move forward by 100 steps, or pixels. Again, notice the punctuation marks we have used, and where. Try writing the three lines of code we have written so far in the coding platform (https://www.codetoday.co.uk/code) and press the play button.


Now, get some practice with getting fred to move around the screen, and get him to draw a square. You will see there is a new command in the code below, something else that turtles know how to do in Python:


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