Welcome to Computer Programming For Kids (CP4K)
Welcome to the Computer Programming for Kids blog!
We are the co-authors of the book "Hello World! Computer Programming for Kids and Other Beginners", released in March 2009. The book is published by Manning Publications. Manning has a web site for the book, which is where you can download the software and other files related to the book. You can purchase it here, at the Manning web site. If you use this link to purchase the print book or e-book from the Manning web site, you can use the discount code aupromo40 at checkout to get a 40% discount. You can also get it through online retailers like Amazon, as well as in many retail locations such as Chapters in Canada and Barnes & Noble in the U.S.
This blog is mostly about the book (for now), but anything related to computers and programming, particularly for kids and beginners, is fair game. We will post articles with extra material that didn't make it into the book, and reader feedback and suggestions are always welcome.
Sunday, December 11, 2011
We've Moved!
And check out our "12 Days of Python", with a chance to win a Kindle Touch!
Warren & Carter
Friday, July 15, 2011
College for Kids
Wednesday, April 20, 2011
LunarLander 2
Many of the games in the book are very simplified versions of classic computer games. LunarLander is an example of that. The original Lunar Lander arcade game was a “vector-graphics” game, where you have to land a lunar module on the moon. There is rocky lunar terrain with a few safe landing spots. You control the rotation and thrust of the lander to make a safe landing. Here’s a screen shot:
The LunarLander in “Hello World!” only moves vertically, not sideways. This was done to show a gravity simulation while keeping the code as simple as possible. After all, the point of the games in the book is to learn, not to make an accurate copy of an old arcade game. We encourage readers to take the games in the book as starting points and extend them in whatever way they like.
I thought it would be fun to take the first step in that direction with LunarLander. So, I made LunarLander2D. This turns LunarLander from a 1-D simulation (vertical only) to a 2-D simulation (vertical and horizontal), more like the arcade version.
To do that, there are two major things that needed to change:
- Need to keep track of both vertical and horizontal speed
- Need a way to rotate the lander, so you can thrust in different directions to steer
Of course, once you start doing rotation and applying thrust at different angles, you need some trigonometry to do the calculations. That’s the main reason we didn’t make LunarLander a 2-D simulation in the first place. We didn’t want to make trigonometry a requirement for the book.
I had to dust the cobwebs off my knowledge of trig. I found the trickiest part was figuring out how to draw the rocket flames at an angle when the lander is rotated. The code to do that is written in an “expanded” form. That is, the code could be made much more compact, but I’m trying to show step-by-step how I’m doing the calculation.
Here are the main differences from LunarLander to LunarLander2:
- The window is wider, to make room for the sideways motion.
- The lander is now a sprite, which makes it easier to do the rotation (using pygame.transform.rotate)
- The thrust is controlled by pressing/holding the spacebar instead of using the mouse to drag a slider (There are no longer any mouse events.)
- The rotation is controlled by the left and right arrow keys
- A good landing now depends on both speed and angle
The code is shown below, and you can easily cut-and-paste it into your favourite Python editor to try it out. (I use SPE.) Don’t forget to put a copy of the image file (lunarlander.png) in the same folder as the code when you try to run it.
Here's a screenshot:
This is still a very simplified version. Here are some ways it could be enhanced:
- Add some animation or different sprites when the lander lands roughly or crashes
- Add different messages depending on how the landing was
- Add more terrain and multiple landing spots
- Have multiple turns, where it gets more difficult each time (steeper terrain, smaller landing spots, less fuel, etc.)
Enjoy!
# LunarLander2d.py
# Copyright Warren Sande, 2011
# Based on LunarLander, Copyright Warren Sande, 2009
# Released under MIT license http://www.opensource.org/licenses/mit-license.php
# LunarLander 2D
# simulation game of landing a spacecraft
"""
The original LunarLander in "Hello World! Computer Programming
for Kids and Other Beginners"
was a very simple 1-D (y-axis, vertical motion only)
version of Lunar Lander, just to demonstrate a simple
gravity simulaiton using Pygame.
LunarLander 2D adds the x-axis (sideways motion).
You can now rotate the lander using
the right/left arrow keys, more like
the original arcade version.
It also changes the thrust control from a
mouse-based (dragging the slider) to key-based
(press/hold the spacebar).
Instead of the moonsurface image,
it uses a simple landing pad rectangle.
"""
# initialize - get everything ready
import pygame, sys
from math import *
pygame.init()
#wider screen than LunarLander, since we have sideways motion
screen = pygame.display.set_mode([800,600])
screen.fill([0, 0, 0])
ground = 540 #landing pad is ay y = 540
start = 90 # starting location 90 pixels from top of window
clock = pygame.time.Clock()
ship_mass = 5000.0
fuel = 5000.0
fps = 30 #default
x_loc = 20 # x-location in meters = dist from center of landing pad
y_loc = 40 # y-location in meters = height above the landing pad
x_offset = 40 # offsets so that when ship location is (0,0), ship is
y_offset = -108 # just touching landing pad, and centered.
x_speed = 2000.0 #pixels/frame
y_speed = -800.0
x_velocity = x_speed/(10.0*fps) #m/s
y_velocity = y_speed/(10.0*fps)
gravity = 1.5
thrust = 0
delta_v = 0
scale = 10 #scale factor from pixels to meters
throttle_down = False
left_down = False
right_down = False
#held_down = False
count = 0
x_pos = 0
y_pos = 0
"""
x_pos, y_pos in pixels - (0,0) is top left
x_loc, y_loc in meters - (0,0) is center of landing pad
x_speed, y_speed in pixels per frame.
x_velocity, y_velocity in m/s
scale factor = 10 (10 pixels = 1 meter)
pos (pixels) to loc (meters) is scaled by: scaleFactor (meters/pixel)
speed (pixels/frame) to velocity (m/s) is scaled by:
ScaleFactor * FPS (Frames Per Second)
"""
# sprite for the ship
class ShipClass(pygame.sprite.Sprite):
def __init__(self, image_file, position):
pygame.sprite.Sprite.__init__(self)
self.imageMaster = pygame.image.load(image_file)
self.image = self.imageMaster
self.rect = self.image.get_rect()
self.position = position
self.rect.centerx, self.rect.centery = self.position
self.angle = 0
def update(self):
self.rect.centerx, self.rect.centery = self.position
oldCenter = self.rect.center
self.image = pygame.transform.rotate(self.imageMaster, self.angle)
self.rect = self.image.get_rect()
self.rect.center = oldCenter
# calcualte position, motion, acceleration, fuel
def calculate_velocity():
global ship, thrust, fuel, x_velocity, y_velocity, x_loc, y_loc
global tot_velocity, scale, x_pos, y_pos, x_speed, y_speed
delta_t = 1/fps
#Calculate thrust based on spacebar being held down
if throttle_down:
thrust = thrust + 100
if thrust > 1000:
thrust = 1000
else:
if thrust > 0:
thrust = thrust - 200
if thrust < 0:
thrust = 0
fuel -= thrust /(10 * fps) # use up fuel
if fuel < 0: fuel = 0.0
if fuel < 0.1: thrust = 0.0
ythrust = thrust * cos(ship.angle*(pi/180))
xthrust = thrust * sin(ship.angle*(pi/180))
y_delta_v = delta_t * (-gravity + 50 * ythrust / (ship_mass + fuel))
y_velocity = y_velocity + y_delta_v
x_delta_v = delta_t * (-50 * xthrust/(ship_mass + fuel))
x_velocity = x_velocity + x_delta_v
x_speed = x_velocity * 10.0/fps #speed in pixels/frame, velocity in m/s
y_speed = y_velocity * 10.0/fps
y_loc = y_loc + y_velocity/fps # loc in meters, velocity in m/s
x_loc = x_loc - x_velocity/fps
tot_velocity = sqrt(x_velocity**2 + y_velocity**2)
ship.position[0] = x_pos = screen.get_width()/2 - (scale * x_loc) + x_offset
ship.position[1] = y_pos = screen.get_height() - (scale * y_loc) + y_offset
if right_down:
ship.angle = ship.angle - 2
if left_down:
ship.angle = ship.angle + 2
ship.update()
# display the text with the speed, height, etc.
def display_stats():
vv_str = "vertical speed: %.1f m/s" % y_velocity # in m/s
hv_str = "horizontal speed %.1f m/s" % x_velocity # in m/s
tv_str = "Total Velocity %.1f m/s" % tot_velocity # in m/s
h_str = "height: %.1f m" % y_loc #in meters
x_str = "position: %.1f m" % x_loc
ang_str = "Angle: %.1f" % ship.angle
t_str = "thrust: %i kg" % thrust
a_str = "acceleration: %.1f m/s/s" % (delta_v * fps)
f_str = "fuel: %i" % fuel
vv_font = pygame.font.Font(None, 26) #vertical speed
vv_surf = vv_font.render(vv_str, 1, (255, 255, 255))
screen.blit(vv_surf, [10, 50])
hv_font = pygame.font.Font(None, 26) #horizontal speed
hv_surf = hv_font.render(hv_str, 1, (255, 255, 255))
screen.blit(hv_surf, [10, 70])
hv_font = pygame.font.Font(None, 26) #horizontal speed
hv_surf = hv_font.render(hv_str, 1, (255, 255, 255))
screen.blit(hv_surf, [10, 90])
h_font = pygame.font.Font(None, 26) #y-location (height)
h_surf = h_font.render(h_str, 1, (255, 255, 255))
screen.blit(h_surf, [10, 120])
x_font = pygame.font.Font(None, 26) #x_location
x_surf = x_font.render(x_str, 1, (255, 255, 255))
screen.blit(x_surf, [10, 150])
ang_font = pygame.font.Font(None, 26) #angle
ang_surf = ang_font.render(ang_str, 1, (255, 255, 255))
screen.blit(ang_surf, [10, 180])
t_font = pygame.font.Font(None, 26) #thrust
t_surf = t_font.render(t_str, 1, (255, 255, 255))
screen.blit(t_surf, [10, 210])
a_font = pygame.font.Font(None, 26) #acceleration
a_surf = a_font.render(a_str, 1, (255, 255, 255))
screen.blit(a_surf, [10, 240])
f_font = pygame.font.Font(None, 26) #fuel
f_surf = f_font.render(f_str, 1, (255, 255, 255))
screen.blit(f_surf, [60, 330])
# display the ship's flames - size depends on the amount of thrust
def display_flames():
fsize = thrust / 30 #flame size
# for rotation, need to do some trig to draw the flame triangles
# in the correct orientation
# points a-f are the 6 vertices of the 2 flame triangles
# calcualte polar coordinates of un-rotated flames
a_len = sqrt(43*43 + 17*17)
b_len = sqrt( (43+fsize)*(43+fsize) + 13*13)
c_len = sqrt(43*43 + 9*9)
d_len = c_len
e_len = b_len
f_len = a_len
a_ang_orig = atan2(-17.0,-43.0)
b_ang_orig = atan2(-13.0,(-43-fsize))
c_ang_orig = atan2(-9.0,-43.0)
d_ang_orig = atan2(9.0,-43.0)
e_ang_orig = atan2(13.0,(-43.0-fsize))
f_ang_orig = atan2(17.0,-43.0)
# rotate flame by same angle as ship rotation
a_ang_rot = a_ang_orig + (270-ship.angle)*(pi/180)
b_ang_rot = b_ang_orig + (270-ship.angle)*(pi/180)
c_ang_rot = c_ang_orig + (270-ship.angle)*(pi/180)
d_ang_rot = d_ang_orig + (270-ship.angle)*(pi/180)
e_ang_rot = e_ang_orig + (270-ship.angle)*(pi/180)
f_ang_rot = f_ang_orig + (270-ship.angle)*(pi/180)
# calculate x-y coordinates of rotated flames
ax = int(a_len * cos(a_ang_rot))
ay = int(a_len * sin(a_ang_rot))
bx = int(b_len * cos(b_ang_rot))
by = int(b_len * sin(b_ang_rot))
cx = int(c_len * cos(c_ang_rot))
cy = int(c_len * sin(c_ang_rot))
dx = int(d_len * cos(d_ang_rot))
dy = int(d_len * sin(d_ang_rot))
ex = int(e_len * cos(e_ang_rot))
ey = int(e_len * sin(e_ang_rot))
fx = int(f_len * cos(f_ang_rot))
fy = int(f_len * sin(f_ang_rot))
#draw the flames
pygame.draw.polygon(screen, [255, 109, 14],
[(x_pos + ax, y_pos + ay),
(x_pos + bx, y_pos + by),
(x_pos + cx, y_pos + cy)], 0)
pygame.draw.polygon(screen, [255, 109, 14],
[(x_pos + dx, y_pos + dy),
(x_pos + ex, y_pos + ey),
(x_pos + fx, y_pos + fy)], 0)
# display final stats when the game is over
def display_final():
global x_velocity, y_velocity, tot_velocity, ship #, fuelbar
final1 = "Game over"
final2 = "You landed at:"
final3 = "%.1f m/s horizontal" % x_velocity
final4 = "%.1f m/s vertical" % y_velocity
final5 = "%.1f m/s Total" % tot_velocity
final6 = "%.1f degrees" % ship.angle
screen.fill([0, 0, 0])
pygame.draw.rect(screen, [0, 0, 255], [80, 350, 24, 100], 2)
pygame.draw.rect(screen, [0,255,0], [84,448-fuelbar,18, fuelbar], 0)
drawTerrain()
screen.blit(ship.image, ship.rect)
if (abs(tot_velocity) < 2 and abs(ship.angle) < 5):
final7 = "Nice landing!"
final8 = "I hear NASA is hiring!"
elif (abs(tot_velocity) < 55 and abs(ship.angle < 10)):
final7 = "Ouch! A bit rough, but you survived."
final8 = "You'll do better next time."
else:
final7 = "Yikes! You crashed a 30 Billion dollar ship."
final8 = "How are you getting home?"
pygame.draw.rect(screen, [0, 0, 0], [5, 5, 350, 280],0)
f1_font = pygame.font.Font(None, 70)
f1_surf = f1_font.render(final1, 1, (255, 255, 255))
screen.blit(f1_surf, [20, 50])
f2_font = pygame.font.Font(None, 40)
f2_surf = f2_font.render(final2, 1, (255, 255, 255))
screen.blit(f2_surf, [20, 110])
f3_font = pygame.font.Font(None, 26)
f3_surf = f3_font.render(final3, 1, (255, 255, 255))
screen.blit(f3_surf, [20, 150])
f4_font = pygame.font.Font(None, 26)
f4_surf = f4_font.render(final4, 1, (255, 255, 255))
screen.blit(f4_surf, [200, 150])
f5_font = pygame.font.Font(None, 26)
f5_surf = f5_font.render(final5, 1, (255, 255, 255))
screen.blit(f5_surf, [380, 150])
f6_font = pygame.font.Font(None, 26)
f6_surf = f6_font.render(final6, 1, (255, 255, 255))
screen.blit(f6_surf, [20, 180])
f7_font = pygame.font.Font(None, 26)
f7_surf = f7_font.render(final7, 1, (255, 255, 255))
screen.blit(f7_surf, [20, 210])
f8_font = pygame.font.Font(None, 26)
f8_surf = f8_font.render(final8, 1, (255, 255, 255))
screen.blit(f8_surf, [20, 240])
screen.blit(inst1_surf, [50, 550])
screen.blit(inst2_surf, [20, 575])
screen.blit(inst3_surf, [50, 600])
pygame.display.flip()
# draw the landing pad.
# note: This could be made more complex, but just a
# simple landing pad for now.
def drawTerrain():
pygame.draw.rect(screen, [180, 180, 180], [420, 535, 70, 5],0)
# make an instance of the ship sprite
ship = ShipClass('lunarlander.png', [500, 230])
# main loop
while True:
clock.tick(30)
fps = clock.get_fps()
if fps < 1: fps = 30
count += 1
if y_loc > 0.01:
calculate_velocity()
screen.fill([0, 0, 0])
display_stats()
pygame.draw.rect(screen, [0, 0, 255], [80, 350, 24, 100], 2)
fuelbar = 96 * fuel / 5000
pygame.draw.rect(screen, [0,255,0], [84,448-fuelbar,18, fuelbar], 0)
drawTerrain()
display_flames()
screen.blit(ship.image, ship.rect)
instruct1 = "Land softly without running out of fuel"
instruct2 = "Good landing: < 5 m/s Great landing: < 2m/s"
instruct3 = "And you must be within 10 degrees of vertical"
inst1_font = pygame.font.Font(None, 24)
inst1_surf = inst1_font.render(instruct1, 1, (255, 255, 255))
screen.blit(inst1_surf, [50, 550])
inst2_font = pygame.font.Font(None, 24)
inst2_surf = inst1_font.render(instruct2, 1, (255, 255, 255))
screen.blit(inst2_surf, [20, 575])
inst3_font = pygame.font.Font(None, 24)
inst3_surf = inst3_font.render(instruct3, 1, (255, 255, 255))
screen.blit(inst3_surf, [50, 600])
pygame.display.flip()
else: #game over - print final score
display_final()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
throttle_down = True
if event.key == pygame.K_RIGHT:
right_down = True
if event.key == pygame.K_LEFT:
left_down = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
throttle_down = False
if event.key == pygame.K_RIGHT:
right_down = False
if event.key == pygame.K_LEFT:
left_down = False
Monday, March 14, 2011
Hello World! Chinese Translation

http://www.turingbook.com/Books/ShowBook-629.aspx
Sunday, February 27, 2011
What to do if you're having problems.
- Proper version of Python and all required modules. Did you use our book's installer to get Python? If not, get the installer here and follow the instructions.
- Indentation. Make sure spacing is correct. This is very important in Python.
- Check for typos. A silly thing like a spelling error or typing _ instead of - might cause your program to have an error. Make sure it matches the one in the book.
Thursday, September 23, 2010
Cool stuff for Mac
Recently, I got a Mac. This means I'll be able to help a lot more with Mac problems on our author forums and on this blog. In related news, here's a cool trick for Macs only. You can make your program talk to you! Start by importing os. OS is a module that lets you interact with your computer's operating system. Then, type the following:
#For some reason, apostrophes don't work.
name = raw_input("What's your name?")
# It's good to put it on the screen as well,
# in case users don't have their speakers on.
Tuesday, June 29, 2010
Carter Teaches Random
Dave Briccetti, who, among other things, teaches programming to kids, is teaching a Python class for Grade 7-9 students this summer at College for Kids. He asked if we would be interested in doing a "guest lecture" to his class. So Carter presented an introduction to randomness and the Python Random module. You can see the video on Dave's site, here:
http://young-programmers.blogspot.com/2010/06/randomness-in-python-lesson-by-carter.html
Monday, June 7, 2010
Carter's VPython Podcast
For our next podcast, Carter will be answering questions from readers. So, please post any questions you have about Python or related modules in our Author Forum, at:
http://www.manning-sandbox.com/thread.jspa?threadID=38048&stqc=true
Warren and Carter
Thursday, March 11, 2010
Passing arguments as a list
If you are passing a number of arguments to a function, you could pass them as individual arguments, like this:
def printMyLuckyNumbers(num1, num2, num3, num4, num5):
print "Here are your lucky numbers:"
print num1, num2, num3, num4, num5
Then you would call the function like this:
printMyLuckyNumbers(3, 7, 10, 14, 27)
But that has a couple of disadvantages. First, if there are a lot of arguments, it gets messy to type all the variable names. Second, you might not know ahead of time how many arguments you want to pass.
So, another way, that solves both those problems, is to pass a list of arguments instead, like this:
def printMyLuckyNumbers(myNums):
print "Your lucky numbers are:"
for num in myNums:
print num,
Then you would call the function like this:
myLuckyNumbers = [3, 7, 10, 14, 27]
printMyLuckyNumbers(myLuckyNumbers)
In the first example, you are passing 5 separate arguments. In the second example, you are passing a single list. That list happens to have 5 items, in this example. But it would work with any number of items. For example, this would work just fine when calling the second version of the function:
myLuckyNums = [2, 3, 5, 7, 10, 27, 32, 45]
printMyLuckyNumbers(myLuckyNums)
These are very simple examples. You can pass things more complicated than lists. You can pass nested lists (lists of lists, or two-dimensional lists). You can pass objects, which can contain any data structure you care to define. You can pass lists of objects or objects containing lists (or dictionaries, or any other Python data type).
Thursday, January 14, 2010
Hello World! on HanselMinutes
You can find the podcast here.
Saturday, January 2, 2010
Hello World! German Translation
You can see some sample chapters here:
Table of Contents
Preface
Chapter 1
Oddly, our publisher, Manning, didn't tell us about the translation. We just stumbled across it ourselves. The publishing world sometimes works in strange and mysterious ways!
Tuesday, November 10, 2009
Carter Explains PythonCard on Young Programmer's Podcast
Carter recently did a Podcast on PythonCard for the Young Programmer's Podcast. Dave tells us it's one of the most popular items on the podcast!

You can see it here.
Tuesday, June 30, 2009
Carter and Warren on TV
We hosted a Book Launch party on June 24, 2009.
That day, we also did an interview on the Ottawa CTV affiliate, CJOH, with anchor Carol-Anne Meehan.
It ran on their 6:00 news. You can see it here:
http://watch.ctv.ca/news/clip186877#clip186877
You can also read the article EMC newspaper wrote about us. Reporter Sabine Gibbins talked to Carter, Warren, and illustrator Martin Murtonen the day of the launch.
We also met Martin for the first time that day. All our work together on the book had been done by e-mail and phone, even though we live in the same city!
Monday, June 29, 2009
Connect the Dots 2
One of our readers created another version that draws the dots and lines one at a time, instead of all at once. So it's kind of an "animated" version of the Connect the Dots program.
Once again, we want you to type this one in, not just cut-and-paste it from the download site. Here's the code for the animated version:

This code might look a little fuzzy, and some of the lines are cut off. That's because it's an image of the code, and that's the way Blogger displays the image. If you click on the code, you'll get a much more clear version with nothing cut off. But it's still an image, so you can't copy-and-paste the code.
As we mentioned in the book, the list of dot coordinates is available from the download site, so you don't have to type in all those numbers. (Just copy-and paste them from your browser to the code editor.) But you do have to type in the rest of the program if you want to see the picture.
Monday, April 20, 2009
Book in hand
We have been waiting a long time, and we finally have the printed books in our hands. It's pretty exciting to actually see it, hold it, and turn the pages.
There was a little glitch with the printing. Due to some kind of error at the printer, some of graphics didn't print properly in the first printing. Our Production Manager at Manning, Mary Piergies, told us this was the first time this happened in the 13 years she has been working there, and she has produced hundreds of titles. So, of course, it had to happen to our book! (What are the odds...?)
But the corrected copies have now been printed. Anyone who received one of the first printings will get a corrected copy. (None of them went out to retail stores or Amazon. Some went out to people who ordered from the Manning web site.) You can make sure you have the corrected copy if you look on the copyright page (the second page inside the front cover). Near the bottom there is the ISBN number. If it is the corrected version, it says "Second corrected printing" right above the ISBN number. Any printing after that is also fine (third, fourth, etc.).
The books should be on their way to retailers now, both online and "bricks and mortar" stores. You can already order it directly form Manning using this link.
Monday, April 6, 2009
Reviews
We've started getting some reviews of the book, and it is very gratifying to see that others are noticing and pointing out the very qualities that we tried hardest to include: fun and readable (but not condescending), well organized, suitable for use in schools and at home.
Here's what some reviewers are saying about the book:
Colin D. Sulin (on amazon.com):
I'm an IT veteran with nearly 25 years of programming experience in a variety of languages and technologies. This book is really one of the best that I have come across. I'm teaching my girls and this book is just perfect for the task. I'm not a Python programmer, but this book is the best thing you can get for getting kids going in computers. I highly recommend it.
Noel O'Blog:
I think this is a great book that fills a real niche. ... What's amazing is that it has set its sights so high, and yet manages to meet its goals. I think it would be great to see this book promoted as a way of teaching programming in primary schools. In the meanwhile if you know any 12+ kids interested in computers, give them an opportunity to develop a fascinating hobby and get them this book.
JR Peck (on Slashdot)
The book is formatted with lots of visuals and fly-outs that give information on how computers operate and how programming languages deal with information processing. My daughter and I have already had interesting discussions on subjects like integers and floats. An example that draws a sine wave led to a great teachable moment about amplitude and wave length. Then there is the constant need for approaching problem solving in a structured manner using logic. I think that taking on programming brings a wide number of benefits.
Bucket-O-Cool:
Hello World! Computer Programming for Kids and Other Beginners is a clear and easy-to-digest voyage through the beginnings of programming with Python. ... By having a junior author involved in the learning process, the reader can quickly identify with the questions being asked to either reinforce the concept that they have learned, or if required, cling to it like a life preserver until they come to a better understanding. It’s a good writing strategy that not only supports an even paced read and lesson, but makes the task at hand genuinely more enjoyable.
Dan Appleman (Gadgets Examiner):
"Hello World” is easy to read, well written and logically organized. ... In an unusual twist, the authors chose to use Python as their computer language. In this they made an outstanding decision. ... Beginners learn best with a kind of language called an interpreter – that allows you to experiment interactively, and Python is a fine choice. Plus the Python language is similar enough to other languages so that a kid who learns it will be able to move to others if necessary. The programming concepts that are taught in the book are easily transferrable to other languages.
The book also has a nice selection of sample programs, with an emphasis on games, but with enough variety that a broad set of concepts can be covered. And the games include graphics – which adds to the appeal.... So this is one book that I’d encourage you not just to buy for your kids, but to read and work on with them.
Cuberick.com:
These days a search for "programming for kids" on Amazon wouldn't return any interesting results, until now. A new book called "Hello World! Computer Programming for Kids and Other Beginners" has just come out. It is a book in the spirit of those old BASIC programming books. It covers the fundamentals of programming using the Python language and has game programs that you can get started with including a lunar lander game and a ski-free clone. If you want to learn programming or teach it to a kid, this is your book. Enjoy!
David Brin:
Hello World! helps challenge young people to take on technology, making it fun, without dumbing-down the adventure.
Sue Gee (I-programmer):
I'm very pleased to discover this new book written by a father and son team to help make up for a very real deficiency in the educational system. Learning to program is a mind expanding experience so while the book has been written to appeal to a young audience it has a lot to offer all beginners. It adopts a logical approach and presents clear explanations backed up by well chosen examples. This book is a very good introduction to programming and can be recommended to anyone, young or old, who wants to start learning this vital and highly enjoyable skill.
Steven Gilham (on amazon.co.uk)
Hello World! is a gentle and humorous introduction to the idea of programming, using Python, a language well suited to filling the niche that BASIC dialects did a generation ago. Even to a crusty and cynical old-timer, like myself, it makes entertaining reading -- not only is the writing style light and engaging, but you can nod sagely and think "Been there, done that" at all the "In the good old days" asides.
Trying out on members of the target audience, it has proven a hit. In its catchy and absorbing manner, it explains the fundamentals (the simple things like variable names being, well, just names) clearly and the exercises concentrate on making fun things happen sooner rather than later. The infectious enthusiasm of the author even managed to hook my non-programmer wife, and get her trying out the examples.
Illustrations
Although we worked very closely with Martin to get all the illustrations just right, we never met him, until our Book Launch party, even though we live in the same city! All our collaboration was by phone and e-mail.
That's one of the things about modern communications, and computers in particular. There are many people who work together or are friends who have never met in person. In fact, we have never met any of the people who worked on our book - editors, typesetters, production staff, and so on (except for a few reviewers who we knew before we started). One day we hope to meet some of the people who made our book come to life!
Sunday, April 5, 2009
Python Help and Your Modules
Carter: Hi! It's Carter here. This blog post is a guide to adding help to your Python modules. We will be starting with our TempConv module from Chapter 15:
# this is the file "my_module.py"
# we're going to use it in another program
def c_to_f(celsius):
fahrenheit = celsius * 9.0 / 5 + 32
return fahrenheit
First, let's see what help() brings up in Python:
>>> import my_module
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named my_module
Oops! We forgot that Python can't find our module unless it's in a folder where python can find it. Navigate to this folder in Windows Explorer (or your favourite file browser):
C:\Python25\Lib\site-packages\
Among other things, you should see a Pygame folder, a PythonCard folder, and easygui.py (you might have to scroll down). Save my_module.py there, restart IDLE (or SPE, or Python Shell), and then try importing it again to make sure Python can find it.
Now, let's see what help() brings:
>>> import my_module
>>> help(my_module)
Help on module my_module:
NAME
my_module
FILE
c:\python25\lib\site-packages\my_module.py
DESCRIPTION
# this is the file "my_module.py"
# we're going to use it in another program
FUNCTIONS
c_to_f(celsius)
# this is the file "my_module.py"
# we're going to use it in another program
Not very user-friendly. Let's try changing the comments.
# My Module
# Our first module.
def c_to_f(celsius):
#converts celsius to fahrenheit
fahrenheit = celsius * 9.0 / 5 + 32
return fahrenheit
And now restart IDLE again (this is getting annoying...) and call help():
>>>import my_module
>>>help(my_module)
Help on module my_module:
NAME
my_module
FILE
c:\python25\lib\site-packages\my_module.py
DESCRIPTION
#My Module
#Our first module.
FUNCTIONS
c_to_f(celsius)
Huh. Why didn't c_to_f's comment appear? The answer is that help() uses strings, but supports comments at the start of the program. Let's change my_module again:
"""My Module
Our first module."""
def c_to_f(celsius):
"converts celsius to fahrenheit"
fahrenheit = celsius * 9.0 / 5 + 32
return fahrenheit
And now restart IDLE and use help again:
>>> import my_module
>>> help(my_module)
Help on module my_module:
NAME
my_module
FILE
c:\python25\lib\site-packages\my_module.py
DESCRIPTION
My Module
Our first module.
FUNCTIONS
c_to_f(celsius)
converts celsius to fahrenheit
There, now that looks a lot better. Also, Python classes display similar help. Try typing this into my_module:
"""My Module
Our first module."""
def c_to_f(celsius):
"converts celsius to fahrenheit"
fahrenheit = celsius * 9.0 / 5 + 32
return fahrenheit
class SampleObject:
"Sample object"
def __init__(self, number):
"Initializes object"
self.number = number
def addOne(self):
"Adds one to the number"
self.number += 1
And the help() is:
>>> import my_module
>>> help(my_module)
Help on module my_module:
NAME
my_module
FILE
c:\python25\lib\site-packages\my_module.py
DESCRIPTION
My Module
Our first module.
CLASSES
SampleObject
class SampleObject
Sample object
Methods defined here:
__init__(self, number)
Initializes object
addOne(self)
Adds one to the number
FUNCTIONS
c_to_f(celsius)
converts celsius to fahrenheit
One more thing: For help() to work correctly, your string also has to be on the first line of your program (or function, or class, or method), and be indented correctly.
And that's how you can make your modules look great in help()!

