Python For Kids (Level1-Level 2) - 1st Week
Python For Kids (Level1-Level 2) - 1st Week
Python to kids
KIDS
STUDENT GUIDE BOOK – 1A
(AGES 11 - 14)
Sue’s Academy
Keep calm and
Computing
code python and
Mathematics
Learning Center
Python for kids | Sue’s Academy
Chapter 1
Python Basics: Setup Your Environment
To get you started using Python on your computer, we’ll go through these three steps
together:
1. Download Python.
2. Install Python on your computer.
3. Test Python with a simple program.
Python is free and easy to get from the Python website, shown in Figure 1-1.
Find the file you just downloaded (it’s probably in your Downloads folder) and
double click it to run and install Python and the IDLE editor. IDLE is the program we’ll
use to type and run our Python programs.
In your Start menu or Applications folder, find the IDLE program and run it. You’ll
see a text-based command window like the one shown in Figure 1-2. This is called the
Python shell. A shell is a window or screen that lets the user enter commands or lines of
code.
Figure 1-2. The IDLE Python shell — our command center for learning Python
The >>> is called a prompt, and it means that the computer is ready to accept your first
command. The computer is asking you to tell it what to do. Type print ("Hello, world!")
and press ENTER on your keyboard. You should see the Python shell respond by printing
the text in quotes that you entered inside the parentheses: Hello, world! That’s it —
you’ve written your first program!
Let’s write a slightly longer program in Python. In the new, blank window, type the
following three lines of code:
# YourName.py
name = input("What is your name?\n")
print("Hi, ", name)
The first line is called a comment. Comments, which begin with a hash mark (#),
are programming notes or reminders that the computer ignores. In this example, the
comment is just a note to remind us of the program’s name.
The second line asks the user to input their name and remembers it as name. The
third line prints "Hi, " followed by the user’s name.
Notice that there’s a comma (,) separating the quoted text "Hi, " from the name.
PROGRAMMING CHALLENGES
MadLib.py
adjective = input("Please enter an adjective: ")
noun = input("Please enter a noun: ")
verb = input("Please enter a verb ending in -ed: ")
print("Your MadLib:")
print("The", adjective, noun, verb, "over the lazy brown dog.")
More Challenge!
#2: MORE MAD LIBS! Let’s make our Mad Lib game a little more interesting. Start a
new version of MadLib.py by saving it as MadLib2.py.
Add another input line that asks for a type of animal. Then, change the print
statement by removing the word dog and adding the new animal variable after the
end of the quoted sentence (add a comma before your new variable inside the
print statement). You can change the sentence more, if you’d like. You could wind
up with The funny chalkboard burped over the lazy brown gecko — or something
even funnier!
Chapter 2
Turtle Graphics: Drawing with Python
In this chapter, we’ll write short, simple programs to create beautifully complex visuals.
To do this, we’ll use turtle graphics. In turtle graphics, you write instructions that tell a virtual,
or imaginary, turtle to move around the screen. The turtle carries a pen, and you can instruct
the turtle to use its pen to draw lines wherever it goes. By writing code to move the turtle around
in cool patterns, you can make it draw amazing pictures. Using turtle graphics, not only can you
create impressive visuals with a few lines of code, but you can also follow along with the turtle
and see how each line of code affects its movement. This will help you understand the logic of
your code.
SquareSpiral1.py
# SquareSpiral1.py - Draws a square spiral
import turtle
t = turtle.Pen()
for x in range(100):
t.forward(x)
t.left(90)
When we run this code, we get a pretty neat picture (Figure 2-1).
Turtle on a Roll
Let’s see what happens when we change one of the numbers in the program. One way to
learn new things about a program is to see what happens when you change one part of it. You
won’t always get a pretty result, but you can learn even when something goes wrong. Change
just the last line of the program to t.left(91) and save it as SquareSpiral2.py.
SquareSpiral2.py
import turtle
t = turtle.Pen()
for x in range(100):
t.forward(x)
t.left(91)
Figure 2-2. The square spiral program with one tiny change becomes a spiral staircase
I mentioned that a 90-degree left turn creates a perfect square. Turning just a little more
than 90 degrees — in this case, 91 degrees every turn — throws the square off just a bit. And
because it’s already off a bit when it makes the next turn, our new shape looks less and less like
a square as the program continues. In fact, it makes a nice spiral shape that starts to swirl to the
left like a staircase, as you can see in Figure .
This is also a nice visual to help you understand how being off by just one number can
drastically change the result of your program. One degree doesn’t seem like a big deal, unless
you’re off by one degree 100 times (which adds up to 100 degrees), or 1,000 times, or if you’re
using a program to land an airplane …
Challenge
Change the number of lines the program draws to 200, or 500, or 50, by changing the
value in parentheses after range.
Also try changing the angle in the last line to 91, 46, 61, or 121, and so on.
Remember to save the program each time. Then run it to see how your changes
affect what the program draws
Turtle Roundup
t.forward(x), we saw earlier that is command, or function, moves the turtle’s
pen forward x pixels and draws a straight line segment; then the turtle turns and does it again.
What if we changed that line of code to draw something more complex, like a circle?
Fortunately for us, the command to draw a circle of a certain size, or radius, is as simple
to code as the command to draw a straight line. Change t.forward(x) to t.circle(x), as shown in
the following code.
CircleSpiral1.py
import turtle
t = turtle.Pen()
for x in range(100):
t.circle(x)
t.left(91)
Figure 2-3. Just one more change gives us a beautiful set of four spiraling circles.
SquareSpiral3.py
import turtle t = turtle.Pen()
t.pencolor("red")
for x in range(100):
t.forward(x)
t.left(91)
Run the program, and you’ll see a more colorful version of our square spiral
A Four-Color Spiral
Let’s think through the algorithm — that is, the set of steps — that will turn our one-color
spiral into a four-color spiral. Most of the steps are the same as in our previous spiral programs,
but there are a few added twists:
1. Import the turtle module and set up a turtle.
2. Tell the computer which colors we’d like to use.
3. Set up a loop to draw 100 lines in our spiral.
4. Pick a different pen color for each side of the spiral.
5. Move the turtle forward to draw each side.
6. Turn the turtle left to get ready to draw the next side.
First, we need a list of color names instead of a single color, so we’re going to set up a list
variable called colors and put four colors in the list, like this:
colors = ["red", "yellow", "blue", "green"]
This list of four colors will give us one color for each side of our square. Notice we put the
list of colors inside square brackets, [ and ]. Make sure that each color name is inside quote
marks just like the words we printed out in Chapter 1, because these color names are strings, or
text values, that we will pass to the pencolor function shortly. As noted, we’re using a variable
called colors to store our list of four colors, so whenever we want to get a color from the list,
we’ll use the colors variable to stand for the color of the pen. Remember, variables store values
that change. It’s right in their name: they vary!
The next thing we need to do is change the pen color every time we step through the
drawing loop. To do this, we need to move the t.pencolor() function into the group of
instructions under the for loop. We also need to tell the pencolor function that we want to use
one of the colors from the list.
ColorSquareSpiral.py
Type the following code and run it.
import turtle
t = turtle.Pen()
colors = ["red", "yellow", "blue", "green"]
for x in range(100):
t.pencolor(colors[x%4])
t.forward(x)
t.left(91)
The list of four colors makes sense, and we can see them in the running example (Figure
2-5). So far, so good.
Figure 2-5. A much more colorful version of our square spiral program
even more, to make larger squares in our spiral. See Figure 2-6 for the new version of our picture
with 200 lines on a black background.
Figure 2-6. Our square spiral has come a long way from its simple beginnings.
Always willing to help make my programs more awesome, Alex asked for one more
change: what if we replaced the line segments with circles now? Wouldn’t that be the coolest
picture of all? Well, yes, I have to agree — it is even cooler.
Here’s the full code.
ColorCircleSpiral.py
import turtle
t = turtle.Pen()
turtle.bgcolor("black")
colors = ["red", "yellow", "blue", "green"]
for x in range(100):
t.pencolor(colors[x%4])
t.circle(x)
t.left(91)
You can see the result in Figure 2-7.
Figure 2-7. Alex’s awesome color circle spiral — eight lines of code, simple and elegant
ColorSpiral.py
import turtle
t = turtle.Pen()
turtle.bgcolor("black")
# You can choose between 2 and 6 sides for some cool shapes!
sides = 6
colors = ["red", "yellow", "blue", "orange", "green", "purple"]
for x in range(360):
t.pencolor(colors[x%sides])
t.forward(x * 3/sides + x)
t.left(360/sides + 1)
t.width(x*sides/200)
(a)
(b) (c)
(d ) (e)
Figure 2-8. Five colorful shapes created by changing the variable sides from 6 (a) down to 2 (e)
You can change the value of sides from 6 down to 2. Then save and run the program as
many times as you’d like.
Programming Challenges
#1: CHANGING THE NUMBER OF SIDES
We used a variable, sides, in the ColorSpiral.py program in One Variable to Rule Them All,
but we didn’t vary it much or change its value except for editing, saving, and running the program
again. Try changing the value of sides to another number, say 5. Save and run the program to
see how this affects your drawing. Now try 4, 3, 2, and even 1! Now, add two or more colors, in
quotes, separated by commas, to the list of colors in the sixth line of the program. Increase the
value of sides to use this new number of colors — try 8 or 10 or more!
#2: HOW MANY SIDES?
What if you want to let a user decide the number of sides while the program runs? Using
what you learned in Chapter 1, you can ask the user for a number of sides and store that input
in the variable sides. Our only extra step is to evaluate the number the user enters. We can find
out which number the user typed with the eval() function, like this:
sides = eval(input("Enter a number of sides between 2 and 6: "))
Replace the line sides = 6 in ColorSpiral.py with the preceding line. Your new program will
ask how many sides the user wants to see. Then, the program will draw the shape the user asks
for. Give it a try!
#3: RUBBER-BAND BALL
Try changing the ColorSpiral.py program into a more tangled and abstract shape just by
adding an extra turn inside the end of the drawing loop. Add a line like t.left(90) to the bottom
of the for loop to make the angles sharper (remember to indent, or space over, to keep the
statement in the loop). The result, shown in Figure 2-9, looks like a geometric toy or perhaps a
ball made of colored rubber bands
Figure 2-9. Adding an extra 90 degrees to each turn in ColorSpiral.py turns it into
RubberBandBall.py.