ebook img

Python for Kids PDF

348 Pages·2012·11.455 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

PP YY TT HH OO NN F O R K I D S F O R K I D S A Playful Introduction to Programming J a s o n R . B r i g g s Python for Kids 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. First printing 16 15 14 13 12 1 2 3 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 book distributors or translations, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 38 Ringold 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 A catalog record of this book is available from the Library of Congress. 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. 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.