Python for Kids V413HAV

Embed Size (px)

Text of Python for Kids V413HAV

PYTHONFO R K I D SJason R. Briggs

A P l ay f u l I n t r o d u c t i o n to P r o g r a m m i n g

Python for Kids

PythonforKidsA P l ay fu l I n t r o d u c t i o n to P r o g r a m m i n g

B y Ja s o n R . B r i g g s

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 123456789

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; info@nostarch.com; 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 ContentsAbout the Author, Illustrator, and Technical Reviewers . . . . . . . . . . . . . . . xv Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix

Part I: Learning to ProgramChapter 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: Pythons 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 exitChapter 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 DetAILAbout the Author, Illustrator, and technical Reviewers Acknowledgments Introduction xv xvii xix

Why Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx How to Learn to Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Who Should Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Whats in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii The Companion Website. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Have Fun! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii

Part I: Learning to Program1 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 Youve 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 Wont 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 Pythons 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 withif and else 53

if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 A Block Is a Group of Programming Statements . . . . . . . . . . . . . . . 54 Conditions Help Us Compare Things. . . . . . . . . . . . . . . . . . . . . . . . . 57 i