Fo r k i d s ag e d + ( a n d th e i r pa r e nts) Teach Your Kids to Code is a parent’s and teacher’s guide to teaching kids basic programming and problem solving using Python, the powerful language used in college courses and by tech companies like Google and IBM Step-by-step explanations will have kids learning computational thinking right away, while visual and game-oriented examples hold their attention Friendly introductions to fundamental programming concepts such as variables, loops, and functions will help even the youngest programmers build the skills they need to make their own cool games and applications Whether you’ve been coding for years or have never programmed anything at all, Teach Your Kids to Code will help you show your young programmer how to: Create fun, playable games like War, Yahtzee, and Pong Add interactivity, animation, and sound to their apps Teach Your Kids to Code is the perfect companion to any introductory programming class or after-school meet-up, or simply your educational efforts at home Spend some fun, productive afternoons at the computer with your kids—you can all learn something! AB O UT TH E AUTH O R Dr Bryson Payne has taught computer science at the University of North Georgia for more than 15 years He has also taught middle school math and programming, and continues to work with K–12 schools to promote technology education Explore geometry by drawing colorful shapes with Turtle graphics Write programs to encode and decode messages, play Rock-Paper-Scissors, and calculate how tall someone is in Ping-Pong balls T H E F I N E ST I N G E E K E N T E RTA I N M E N T ™ T eac h You r Kids to T o C ode A Parent-friendly Guide to Python Programming Bryson Payne PAYNE w w w.nostarch.com SHELVE IN: PROGRAMMING LANGUAGES/PYTHON $29.95 ($34.95 CDN) Te ea ac ch h Y Yo ou ur r K Kiid ds s t to o C Co od de e T Programming so easy a p Pa r e n t can it! www.allitebooks.com Advance Praise for Teach Your Kids to Code “The text is clear, the graphics are engaging, and the apps are awesome This is the programming guide for parents and kids to enjoy together.” —Aaron Walker, Cybersecurity Expert, NASA “The energy and excitement Bryson brings to teaching is captured perfectly in Teach Your Kids to Code, with colorful, captivating games and graphics that help develop real-world skills.” —Bindy Auvermann, Executive Director, Next Generation Youth Development, Inc “Provides the building blocks of a great future in the rapidly changing world of technology.” —JoAnne Taylor, former Vice President, Global Telecommunications, IBM “The concepts in Teach Your Kids to Code can help any young person enhance their college prospects and expand their career opportunities, and Dr Payne presents these skills through fun, challenging games and apps.” —Dr Raj Sunderraman, Department Chair of Computer Science, Georgia State University “Every child on the planet should have this book, and so should every parent.” —James E Daniel, Jr., Founder, App Studios, LLC “An innovative, motivating guide Builds skills that can last a lifetime.” —Dr Steven Burrell, Vice President for Information Technology & CIO, Georgia Southern University “The kind of book I wish I’d had as a kid.” —Scott Hand, Software Engineer, CareerBuilder www.allitebooks.com “Dr Bryson Payne is a computer scientist and professor of the highest caliber, and with Teach Your Kids to Code, he brings the power of computers within easy reach for readers young and old.” —Dr Antonio Sanz Montemayor, Informatics Professor, Universidad Rey Juan Carlos, Spain “A brilliant combination of engaging, imaginative apps and valuable, lifelong skills.” —Ted Cunningham, author of The Power of Home “Teach Your Kids to Code and the logical thinking it introduces will help build the next generation of technology leaders.” —N Dean Meyer, author and executive coach “This book can jump-start your child’s success in a high-tech world.” —Ken Coleman, leadership author and former radio host of The Ken Coleman Show “Dr Payne set us on the path that led us to our dream jobs! With Teach Your Kids to Code, he’s providing parents and teachers everywhere the chance to the same for the next generation of creative problem-solvers.” —Shah and Susan Rahman, Riot Games “Bryson helps people improve their lives with technology His book does the same.” —Ash Mady, Technical Manager, RedHat, Inc “Enjoyable and accessible to parents and children alike.” —Steve McLeod, Deputy CIO, University of North Georgia “Dr Payne used robots, games, and fun programs to motivate me in college, and Teach Your Kids to Code extends that same passion for coding cool apps beyond the walls of the campus.” —Bobby Brown, Lead Developer, GetUWired www.allitebooks.com Teach Your Kids to Code www.allitebooks.com www.allitebooks.com Teach You r Ki ds to Code A Pa r e n t- F r i e n d ly G u i d e to Python Programming B y B r ys o n Pay n e San Francisco www.allitebooks.com Teach Your Kids to Code Copyright © 2015 by Bryson Payne All rights reserved No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher 19 18 17 16 15 ISBN-10: 1-59327-614-1 ISBN-13: 978-1-59327-614-0 Publisher: William Pollock Production Editor: Riley Hoffman Cover Illustration: Josh Ellingson Illustrator: Miran Lipovac� a Developmental Editors: Tyler Ortman and Leslie Shen Technical Reviewers: Michelle Friend and Ari Lacenski Copyeditor: Rachel Monaghan Compositor: Riley Hoffman Proofreader: Paula L Fleming Indexer: BIM Indexing & Proofreading Services For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc directly: No Starch Press, Inc 245 8th Street, San Francisco, CA 94103 phone: 415.863.9900; info@nostarch.com www.nostarch.com Library of Congress Cataloging-in-Publication Data Payne, Bryson Teach your kids to code : a parent-friendly guide to Python programming / by Bryson Payne 1st edition pages cm Includes index Summary: "A guide to teaching basic programming skills for parents and teachers, with step-by-step explanations, visual examples, and exercises Covers programming concepts including loops, lists, functions, and variables, and how to build games and applications" Provided by publisher ISBN 978-1-59327-614-0 ISBN 1-59327-614-1 Python (Computer program language) Study and teaching (Elementary) Computer programming Study and teaching (Elementary) Python (Computer program language) Study and teaching (Middle school) Computer programming Study and teaching (Middle school) I Title QA76.73.P98P39 2015 005.13'3 dc23 2015006794 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc Other product and company names mentioned herein may be the trademarks of their respective owners Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark The information in this book is distributed on an “As Is” basis, without warranty While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it www.allitebooks.com To Alex and Max, my two favorite coders www.allitebooks.com About the Author Dr Bryson Payne is a tenured professor of computer science at the University of North Georgia, where he has taught aspiring coders for more than 15 years His students have built successful careers at Blizzard Entertainment, Riot Games, Equifax, CareerBuilder, and more He was the first department head of computer science at UNG, and he holds a PhD in computer science from Georgia State University In addition, he works extensively with K–12 schools to promote technology education Dr Payne has been programming for more than 30 years The first program he sold was to RUN magazine (Commodore 64) for their “Magic” column in 1985, for $10 Dr Payne lives north of Atlanta, Georgia, with his wife, Bev, and two sons, Alex and Max About the Illustrator Miran Lipovac� a is the author of Learn You a Haskell for Great Good! He enjoys boxing, playing bass guitar, and, of course, drawing He has a fascination with dancing skeletons and the number 71, and when he walks through automatic doors he pretends that he’s actually opening them with his mind About the Technical Reviewer Ari Lacenski is a developer of Android applications and Python software She lives in San Francisco She writes about Android programming at http://gradlewhy.ghost.io/, mentors with Women Who Code, and plays songs about space pirates on guitar www.allitebooks.com www.allitebooks.com Glossary Many of the terms you encounter in learning to code are everyday words that you already understand Some terms, though, are brand new or have special meaning to computer programmers This glossary defines several of the newer terms you’ll come across in the book, as well as familiar words that take on new meanings in the world of coding algorithm A set of steps for performing a task, like a recipe animation The illusion of motion created when similar images are displayed quickly one after the other, as in a cartoon app Short for application, a computer program that does something useful (or fun!) append To add something to the end; for example, adding letters onto the end of a string or adding elements to the end of a list or array argument A value passed to a function; in the statement range(10), 10 is an argument array An ordered list of values or objects, usually of the same type, accessed by their index, or position in the list assignment Setting the value of a variable, as in x = 5, which assigns the value to the variable x block A group of programming statements Boolean A value or expression that can be either true or false class A template defining the functions and values to be contained in any objects of that type code Statements or instructions written by a programmer in a language that computers can understand collision detection Checking to see if two virtual objects are touching, or colliding, on the screen, like the ball and paddle in Pong concatenate To combine two strings of text into a single string conditional expression A statement that allows the computer to test a value and perform different actions depending on the outcome of that test constant A named value in a computer program that stays the same, like math.pi (3.1415 ) declaration A statement or group of statements that tell a computer what a variable or function name means 296 Glossary element A single item in a list or array event An activity that a computer can detect, like a mouse click, value change, keypress, timer tick, and so on Statements or functions that respond to events are called event handlers or event listeners expression Any valid set of values, variables, operators, and functions that produces a value or result file A collection of data or information stored by a computer on some kind of storage device, like a hard disk, DVD, or USB drive for loop A programming statement that allows a block of code to be repeated for a given range of values frame A single image in a moving sequence for animation, video, or computer graphics frames per second (fps) The rate or speed that images are drawn on the screen in an animation, video game, or movie function A named, reusable set of programming statements to perform a specific task import To bring reusable code or data into a program from another program or module index An element’s position in a list or array initialize To give a variable or object its first, or initial, value input Any data or information entered into a computer; input can come from a keyboard, mouse, microphone, digital camera, or any other input device iterative versioning Repeatedly making small changes or improvements to a program and saving it as a new version, like Game1, Game2, and so on keyword A special, reserved word that means something in a particular programming language list A container for an ordered group of values or objects loop A set of instructions that is repeated until a condition is reached Glossary 297 module A file or set of files with related variables, functions, and classes that can be reused in other programs nested loop A loop inside another loop object A variable containing information about a single instance of a class, such as a single sprite from the Sprite class operator A symbol or set of symbols that represents an action or comparison and returns a result, such as +, -, *, //, , ==, and so on parameter An input variable to a function, specified in the function’s definition pixel Short for picture element, the small dots of color that make up images on a computer screen program A set of instructions written in a language computers can understand pseudorandom A value in a sequence that seems to be random or unpredictable, and is random enough to simulate rolling dice or flipping coins random numbers An unpredictable sequence of numbers evenly distributed over a certain range range An ordered set of values between a known start and end value; in Python, the range function returns a sequence of values, such as through 10 RGB color Short for red-green-blue color, a way of representing colors by the amount of red, green, and blue light that can be mixed to re-create each color shell A text-based command line program that reads commands from the user and runs them; IDLE is Python’s shell sort To put elements of a list or array in a certain order, such as alphabetical order string A sequence of characters, which can include letters, numbers, symbols, punctuation, and spacing syntax The spelling and grammar rules of a programming language 298 Glossary transparency In graphics, the ability to see through portions of an image variable In a computer program, a named value that can change A programming statement that allows a block of code to be repeated as long as a condition is true while loop Glossary 299 Index Note: Page numbers followed by f, n, or t indicate figures, notes, and tables, respectively Symbols += (addition and assignment operator), 100, 187 + (addition operator), 35, 35t \ (backslash), 240 / (division operator), 35, 35t, 39 = (equal sign), 32 == (equal to operator), 62, 79–80, 82–83, 82t > (greater than operator), 62, 82–83, 82t >= (greater than or equal to operator), 82–84, 82t () (grouping operator), 35, 35t # (hash mark), // (integer division operator), 49–50, 114, 156 < (less than operator), 62, 82–84, 82t