Table Of ContentPraise for the First Edition of Hello! World
A great book for little kids and big kids alike.
—Gordon Colquhoun, Computer Consultant, Avalon Consulting Services
Python for growing-ups.
—Dr. John Grayson, Author of Python and Tkinter Programming
A fun book to read and learn from!
—Dr. André Roberge, President, Université Sainte-Anne
The authors have created a friendly educational programming book that makes learning fun
and painless.
—Bryan Weingarten, Software Architect
I highly recommend this book!
—Horst Jens, Python Instructor and Author of Programming While Playing
Python is a wonderful language for teaching beginners to program. It is great to see a
kid-focused Python book!
—Jeffrey Elkner, Educator
If you teach your child one thing, teach her the golden rule. If you teach her two things, then
teach her the golden rule and computer programming. This book is all you need for the latter.
—Josh Cronemeyer, Senior Software Consultant, Thoughtworks
I enjoyed the interaction with Carter in the book … My students are really going to enjoy the
Digital Pet program! Reminds me of the Tamagotchi Virtual Pet that I had years ago.
—Kari J Stellpflug, Educator, Rochester Public Schools, Rochester MN
Computer programming is a powerful tool for children 'to learn learning.' … Children who
engage in programming transfer that kind of learning to other things.
—Nicholas Negroponte, One Laptop Per Child Project
Download from Wow! eBook <www.wowebook.com>
Advance Praise for the
Second Edition of Hello! World
It made programming seem as easy as frying bacon.
—Elisabet Gordon, 10th-grade student, Eagle Harbor High School
A great intro to the world of Python for everyone. This book is so much fun!
—Mason Jenkins, 7th-grade student, Myron B. Thompson Academy
For kids from 8 to 88. The book not only covers programming in Python in a fun
way, but also sets the groundwork for good practices that can be used for other
programming languages as well.
—Ben Ooms, Software Engineer, Sogeti
If you want to learn programming or teach it to a kid, this is your book.
—Cuberick.com
A very good introduction to programming for anyone, young or old, who wants to
start learning this vital and highly enjoyable skill.
—Sue Gee, I-Programmer
Warren and Carter start simply, at the beginning, and take kids or adults all the
way to making fun 2D graphical games and simulations. Python is my first choice
for a real programming language for new programmers, and using this book is a
great way to learn it. I’ve been recommending this book to my students since the
First Edition came out.
—Dave Briccetti, Software Developer and Teacher, Dave Briccetti Software LLC
Download from Wow! eBook <www.wowebook.com>
Hello World!
Second Edition
Computer Programming for Kids
and Other Beginners
WARREN SANDE
CARTER SANDE
MANNING
SHELTER ISLAND
Download from Wow! eBook <www.wowebook.com>
For online information and ordering of this and other Manning books, please visit
www.manning.com. The publisher offers discounts on this book when ordered in quantity.
For more information, please contact:
Special Sales Department
Manning Publications Co.
20 Baldwin Road
PO Box 761
Shelter Island, NY 11964
Email: orders@manning.com
©2014 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in
any form or by means electronic, mechanical, photocopying, or otherwise, without prior written
permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in the book, and Manning
Publications was aware of a trademark claim, the designations have been printed in initial caps
or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have
the books we publish printed on acid-free paper, and we exert our best efforts to that end.
Recognizing also our responsibility to conserve the resources of our planet, Manning books are
printed on paper that is at least 15% recycled and processed without elemental chlorine.
Manning Publications Co. Development editor: Cynthia Kane
20 Baldwin Road Copyeditor: Tiffany Taylor
PO Box 261 Proofreader: Toma Mulligan
Shelter Island, NY 11964 Illustrator: Martin Murtonen
Typesetter: Marija Tudor
Cover designer: Leslie Haimes
ISBN 978-1-617290-92-3
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – EBM – 18 17 16 15 14 13
Download from Wow! eBook <www.wowebook.com>
Contents
Preface xi
Acknowledgments xvii
About this book xix
1 Getting Started 1
Installing Python 1 Starting Python with IDLE 2 Instructions,
■ ■
please 3 Interacting with Python 5 Time to program 7
■ ■
Running your first program 8 If something goes wrong 9
■
Our second program 12
2 Remember This: Memory and Variables 15
Input, processing, output 15 Names 17 What’s in a
■ ■
name? 21 Numbers and strings 22 How “variable” are
■ ■
they? 24 The new me 25
■
3 Basic Math 28
The four basic operations 29 Operators 31 Order of
■ ■
operations 31 Two more operators 33 Really big and
■ ■
really small 35
v
Download from Wow! eBook <www.wowebook.com>
vi CONTENTS
4 Types of Data 40
Changing types 40 Getting more information: type() 44
■
Type-conversion errors 44 Using type conversions 44
■
5 Input 46
raw_input() 47 The print command and the comma 48
■
Inputting numbers 50 Input from the Web 52
■
6 GUIs—Graphical User Interfaces 55
What’s a GUI? 55 Our first GUI 56 GUI input 57 Pick your
■ ■ ■
flavor 58 The number-guessing game … again 61
■
Other GUI pieces 62
7 Decisions, Decisions 65
Testing, testing 65 Indenting 67 Am I seeing double? 68
■ ■
Other kinds of tests 69 What happens if the test is false? 70
■
Testing for more than one condition 72 Using and 73
■
Using or 74 Using not 74
■
8 Loop the Loop 78
Counting loops 79 Using a counting loop 81 A shortcut—
■ ■
range() 82 A matter of style—loop variable names 84 Counting
■ ■
by steps 87 Counting without numbers 89 While we’re on the
■ ■
subject … 89 Bailing out of a loop—break and continue 90
■
9 Just for You—Comments 94
Adding comments 94 Single-line comments 95 End-of-line
■ ■
comments 95 Multiline comments 96 Triple-quoted
■ ■
strings 96 Commenting style 97 Comments in this book 97
■ ■
Commenting out 98
10 Game Time 99
Skier 99
11 Nested and Variable Loops 104
Nested loops 104 Variable loops 106 Variable nested
■ ■
loops 107 Even more variable nested loops 108 Using nested
■ ■
loops 110 Counting calories 113
■
Download from Wow! eBook <www.wowebook.com>
CONTENTS vii
12 Collecting Things Together—Lists and Dictionaries 117
What’s a list? 117 Creating a list 118 Adding things to a
■ ■
list 118 What’s the dot? 119 Lists can hold anything 120
■ ■
Getting items from a list 120 “Slicing” a list 121 Modifying
■ ■
items 124 Other ways of adding to a list 124 Deleting from a
■ ■
list 126 Searching a list 127 Looping through a list 128
■ ■
Sorting lists 129 Mutable and immutable 133 Lists of lists:
■ ■
tables of data 133 Dictionaries 136
■
13 Functions 143
Functions—the building blocks 143 Calling a function 145
■
Passing arguments to a function 147 Functions with more than
■
one argument 149 Functions that return a value 151
■
Variable scope 153 Forcing a global 156 A bit of advice on
■ ■
naming variables 157
14 Objects 159
Objects in the real world 160 Objects in Python 160
■
Object = attributes + methods 162 What’s the dot? 162
■
Creating objects 162 An example class—HotDog 168
■
Hiding the data 172 Polymorphism and inheritance 173
■
Thinking ahead 175
15 Modules 178
What’s a module? 178 Why use modules? 178 Buckets of
■ ■
blocks 179 How do we create modules? 179 How do we use
■ ■
modules? 180 Namespaces 181 Standard modules 185
■ ■
16 Graphics 189
Getting some help—Pygame 189 A Pygame window 190
■
Drawing in the window 191 Individual pixels 200 Images 205
■ ■
Let’s get moving! 207 Animation 208 Smoother
■ ■
animation 210 Bouncing the ball 211 Wrapping the ball 213
■ ■
17 Sprites and Collision Detection 217
Sprites 217 Bump! Collision detection 223 Counting
■ ■
time 228
Download from Wow! eBook <www.wowebook.com>
viii CONTENTS
18 A New Kind of Input—Events 233
Events 233 Keyboard events 235 Mouse events 240
■ ■
Timer events 241 Time for another game—PyPong 244
■
19 Sound 256
More help from Pygame—mixer 256 Making sounds vs. playing
■
sounds 257 Playing sounds 257 Controlling volume 260
■ ■
Repeating music 262 Adding sounds to PyPong 263 More
■ ■
wacky sounds 264 Adding music to PyPong 267
■
20 More GUIs 272
Working with PyQt 272 Making our GUI do something 277
■
The return of event handlers 279 Moving the button 280
■
More useful GUIs 280 TempGUI 281 What’s on the menu? 287
■ ■
21 Print Formatting and Strings 293
New lines 294 Horizontal spacing—tabs 296 Inserting variables
■ ■
in strings 298 Number formatting 299 Formatting, the new
■ ■
way 304 Strings ’n’ things 305
■
22 File Input and Output 313
What’s a file? 314 Filenames 314 File locations 315
■ ■
Opening a file 319 Reading a file 320 Text files and binary
■ ■
files 322 Writing to a file 323 Saving your stuff in files:
■ ■
pickle 327 Game time again—Hangman 329 Try it out 336
■ ■
23 Take a Chance—Randomness 337
What’s randomness? 338 Rolling the dice 338 Creating a deck
■ ■
of cards 343 Crazy Eights 348
■
24 Computer Simulations 361
Modeling the real world 361 Lunar Lander 362 Keeping
■ ■
time 367 Time objects 368 Saving time to a file 372
■ ■
Virtual Pet 374
25 Skier Explained 385
The skier 385 The obstacles 389
■
Download from Wow! eBook <www.wowebook.com>
CONTENTS ix
26 Python Battle 399
Python Battle 399 Creating a Python Battle Robot 401 A more
■ ■
complicated robot 403 The coordinate system 404
■
27 What’s Next? 409
For younger programmers 409 Python 410 Game
■ ■
programming and Pygame 410 Other game programming
■
(non-Python) 411 Keep it BASIC 411 Mobile apps 411
■ ■
Look around 411
Appendix A Variable Naming Rules 413
Appendix B Differences Between Python 2 and 3 415
Appendix C Answers to Self-Test Questions 419
List of Code Listings 451
Index 455
Download from Wow! eBook <www.wowebook.com>
Description:SUMMARYA gentle but thorough introduction to the world of computer programming, it's written in language a 12-year-old can follow, but anyone who wants to learn how to program a computer can use it. Even adults. Written by Warren Sande and his son, Carter, and reviewed by professional educators, thi