An introduction to Python for new programmers.
I was recently asked to run a programming taster session for a friend’s 7 year old son. Having been a relative latecomer to programming myself, and entirely self-taught, I was really happy for the opportunity to share my knowledge with someone so young.
I wanted to choose a language and project that would give us instant feedback and allow me to build on simple concepts and combine them into something impressive in less than an hour.
These instructions assume an Apple latop running OS X.
- In Finder, go to the Utilites folder
- Open Terminal
idleat the terminal window
- Hit return to start IDLE
Turtles all the way
Python comes bundled with the
turtle package, a library for visualising the
results of your code as drawings on screen.
Before using any library it has to be imported. You can do this by
import turtle into the Python prompt (the flashing cursor after
>>>) and hitting return.
That’s all there is to it! Now we can now refer to code in the turtle package.
We’re going to create a turtle, and we have to give our turtle a name
so we can keep track of them. The name can be anything you like:
jamesTheGiant, as long as you keep using the same
name in your program.
We’re going to stick with Tim because it’s short and easy to type.
tim = turtle.Turtle()
This means “create a new Turtle and assign it to the variable
Now we have a pet, what can we do with him?
Tim the Turtle already knows how to do lots of things, but today we’re going to make him go forwards and turn right.
Can you see he’s left behind a trail showing where he’s been? If we send tim around the screen we’ll end up with a line drawing of his motion.
Now we have a line, can we make a square?
tim.right(90) tim.forward(100) tim.right(90) tim.forward(100) tim.right(90) tim.forward(100)
We can, but this is lots of typing! Let’s remove the repetition.
Loops are a way of saying that we want to run the same code a certain number of times.
for i in range(4): # Do the following 4 times tim.forward(100) tim.right(90)
4 means that we want to run
tim.right(90) four times.
This is excellent, but what if we wanted to draw lots of squares, rotating a little bit in between? We could nest our loops, putting one inside the other.
for n in range(8): # Draw 8 squares tim.rotate(45) for i in range(4): tim.forward(100) tim.right(90)
Hmm. This works but it’s starting to get a little hard to read. It would be nice if we could write code more like how we describe what we want to do: “I want to draw 8 squares”.
Functions allow programmers to wrap up code into easy to understand
units, and we can give them names that describe what they do. We’ve
already been using functions without realising:
are functions that are defined for turtles. If we
want to draw a square, we could define a function called
def drawSquare(): for i in range(4): tim.forward(100) tim.right(90)
Once we’ve defined a function, we can use it just by referring to its name.
for n in range(8): # Draw 8 squares tim.rotate(45) drawSquare() # We're using our new function!
It’s great that we can draw squares now, but what if we want to change how big they are? Lots of squares all the same size aren’t very exciting.
Fortunately functions can take parameters that control the way they
behave. Just like the numbers we gave to
right(90) we can define a function parameter called
length that specifies how
long each side is.
def drawSquare(length): # Define a parameter called length for i in range(4): tim.forward(length) # Use the parameter when we draw a side tim.right(90)
Now we can draw squares in lots of different sizes:
drawSquare(100) drawSquare(125) drawSquare(250)
Putting it all together
Download the finished code here. You can open it in IDLE, and select ‘Run Module’ from the ‘Run’ menu to execute the code without typing it in yourself.
Well done Tim!