ebook img

Python for kids : a playful introduction to programming PDF

348 Pages·2013·13.557 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Python for kids : a playful introduction to programming

playPython_fullcvr_9.pdf 1 9/25/2013 5:46:14 PM For kids aged 10+ (and their parents) rReEaAlL pPrrooggrraammmmiinngg.. PP YY TT HH OO NN rReEaALl eEaAsSyY.. PP Illustrations by Miran Lipovaca YY F O R K I D S F O R K I D S Python is a powerful, expressive program- As you strike out on your programming TT ming language that’s easy to learn and fun to adventure, you’ll learn how to: HH use! But books about learning to program in  Use fundamental data structures like lists, Python can be kind of dull, gray, and boring, tuples, and maps OO and that’s no fun for anyone. A Playful Introduction to Programming  Organize and reuse your code with func- Python for Kids brings Python to life and brings you (and your parents) into the world of tions and modules NN programming. The ever-patient Jason R. Briggs  Use control structures like loops and will guide you through the basics as you experi- conditional statements J a s o n R . B r i g g s ment with unique (and often hilarious) example  Draw shapes and patterns with Python’s FF C programs that feature ravenous monsters, secret turtle module M OO agents, thieving ravens, and more. New terms  Create games, animations, and other Y are defined; code is colored, dissected, and graphical wonders with tkinter CM RR explained; and quirky, full-color illustrations Why should serious adults have all the fun? MY keep things on the lighter side. Python for Kids is your ticket into the amaz- CY Chapters end with programming puzzles CMY designed to stretch your brain and strengthen ing world of computer programming. KK K your understanding. By the end of the book ABOUT THE AUTHOR II you’ll have programmed two complete games: Jason R. Briggs has been a programmer since DD a clone of the famous Pong and “Mr. Stick Man the age of eight, when he first learned BASIC on Races for the Exit”—a platform game with a Radio Shack TRS-80. He has written software SS jumps, animation, and much more. professionally as a developer and systems archi- tect and served as Contributing Editor for Java Python runs on Windows, Developer’s Journal. His articles have appeared OS X, Linux, OLPC Laptops, in JavaWorld, ONJava, and ONLamp. Python and even Raspberry pi! for Kids is his first book. B r i $34.95 ($36.95 CDN) PROGRAMSHELVE IN gg MIN: s G LAN G UAG wwTwH.En oFsINtaErScTh .IcNom ES/PYTH GEEK ENTERTAINMENT™ ON Praise for Python for Kids “Jason Briggs manages to successfully describe programming to kids without sounding like he’s dumbing down the content. The lessons are well-constructed and leave the reader with a feeling of accomplishment in each chapter.” —Marziah Karch, Wired.com “An excellent introduction to programming for anyone interested in learning to program, regardless of their age. The material is extremely well organized and presented, and makes for a great resource for either home or school.” —Roy Wood, GeekDad “This book offers a good introduction to computer programming. . . . An excellent family bonding experience.” —Patrice Gans, Education Week “By the end of the book, you have a fully-functional platform game running, and most likely a head full of ideas about your next game. Python for Kids is just as good an introduction for adults learning to code.” —Matthew Humphries, Geek.com “Easy to read, provides sound programming advice and very clear explanations of how programming languages work. A great book for anyone who wants to break into programming without pangs of inadequacy.” —Sandra Henry-Stocker, ITworld pfk_TITLE_COPY.indd 1 9/26/2013 4:50:51 PM P y t h o n fo r   K i d s A Playful Introduction to Programming By Jason R. Briggs San Francisco PYTHON FOR KIDS. Copyright © 2013 by Jason R. Briggs. 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. Printed in Korea Fourth printing 16 15 14 13 4 5 6 7 8 9 ISBN-10: 1-59327-407-6 ISBN-13: 978-1-59327-407-8 Publisher: William Pollock Production Editor: Serena Yang Cover and Interior Design: Octopod Studios Illustrator: Miran Lipovaca Developmental Editor: William Pollock Technical Reviewers: Josh Pollock and Maria Fernandez Copyeditor: Marilyn Smith Compositor: Serena Yang Proofreader: Greg Teague 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; fax: 415.863.9950; [email protected]; http://www.nostarch.com/ Library of Congress Cataloging-in-Publication Data Briggs, Jason R. Python for kids : a playful introduction to programming / by Jason R. Briggs. pages cm Audience: 10+ Includes index. ISBN 978-1-59327-407-8 -- ISBN 1-59327-407-6 1. Computer software--Juvenile literature. 2. Computer programming--Juvenile literature. 3. Python (Computer program language)--Juvenile literature. I. Title. QA76.52.B75 2013 005.13'3--dc23 2012044047 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. Production Date: September 26, 2013 Plant & Location: Printed by We SP, South Korea Job / Batch #: 072313 pfk_TITLE_COPY.indd 4 9/25/2013 5:30:07 PM BRIef Contents About the Author, Illustrator, and Technical Reviewers. . . . . . . . . . . . . . . xv Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvii Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Part I: Learning to Program Chapter 1: Not All Snakes Slither. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Chapter 2: Calculations and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15 Chapter 3: Strings, Lists, Tuples, and Maps. . . . . . . . . . . . . . . . . . . . . . . . .25 Chapter 4: Drawing with Turtles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43 Chapter 5: Asking Questions with if and else. . . . . . . . . . . . . . . . . . . . . . . .53 Chapter 6: Going Loopy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67 Chapter 7: Recycling Your Code with Functions and Modules . . . . . . . . . .81 Chapter 8: How to Use Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . .93 Chapter 9: Python’s Built-in Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . .109 Chapter 10: Useful Python Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129 Chapter 11: More Turtle Graphics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145 Chapter 12: Using tkinter for Better Graphics. . . . . . . . . . . . . . . . . . . . . .163 Part II: Bounce! Chapter 13: Beginning Your First Game: Bounce!. . . . . . . . . . . . . . . . . . .193 Chapter 14: Finishing Your First Game: Bounce! . . . . . . . . . . . . . . . . . . .205 Part III : Mr. stick Man Races for the exit Chapter 15: Creating Graphics for the Mr. Stick Man Game . . . . . . . . . .221 Chapter 16: Developing the Mr. Stick Man Game . . . . . . . . . . . . . . . . . . .233 Chapter 17: Creating Mr. Stick Man. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251 Chapter 18: Completing the Mr. Stick Man Game. . . . . . . . . . . . . . . . . . .259 Afterword: Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .285 Appendix: Python Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .293 Glossary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .307 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .313 Contents In DetAIL About the Author, Illustrator, and technical Reviewers xv Acknowledgments xvii Introduction xix Why Python?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx How to Learn to Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Who Should Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi What’s in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxii The Companion Website. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Have Fun! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Part I: Learning to Program 1 not All snakes slither 3 A Few Words About Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 Installing Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5 Installing Python on Windows 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5 Installing Python on Mac OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7 Installing Python on Ubuntu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 Once You’ve Installed Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10 Saving Your Python Programs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12 What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13 2 Calculations and Variables 15 Calculating with Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16 Python Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17 The Order of Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18 Variables Are Like Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19 Using Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21 What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23 3 strings, Lists, tuples, and Maps 25 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26 Creating Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26 Handling Problems with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . .27 Embedding Values in Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30 Multiplying Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31 Lists Are More Powerful Than Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32 Adding Items to a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35 Removing Items from a List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35 List Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38 Python Maps Won’t Help You Find Your Way . . . . . . . . . . . . . . . . . . . . . . .39 What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41 Programming Puzzles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41 #1: Favorites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41 #2: Counting Combatants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42 #3: Greetings! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42 4 Drawing with turtles 43 Using Python’s turtle Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44 Creating a Canvas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44 Moving the Turtle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46 What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51 Programming Puzzles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51 #1: A Rectangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51 #2: A Triangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51 #3: A Box Without Corners. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51 5 Asking Questions with if and else 53 if Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54 A Block Is a Group of Programming Statements . . . . . . . . . . . . . . .54 Conditions Help Us Compare Things. . . . . . . . . . . . . . . . . . . . . . . . .57 if-then-else Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58 if and elif Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59 Combining Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61 Variables with No Value—None . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61 The Difference Between Strings and Numbers. . . . . . . . . . . . . . . . . . . . . . .62 What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65 viii Contents in Detail

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.