logo资料库

《Python for kids(英文版)》.pdf

第1页 / 共345页
第2页 / 共345页
第3页 / 共345页
第4页 / 共345页
第5页 / 共345页
第6页 / 共345页
第7页 / 共345页
第8页 / 共345页
资料共345页,剩余部分请下载后查看
About the Author, Illustrator, and Technical Reviewers
Acknowledgments
Introduction
Why Python?
How to Learn to Code
Who Should Read This Book
What’s in This Book
The Companion Website
Have Fun!
Part I: Learning to Program
1: Not All Snakes Slither
A Few Words About Language
Installing Python
Installing Python on Windows 7
Installing Python on Mac OS X
Installing Python on Ubuntu
Once You’ve Installed Python
Saving Your Python Programs
What You Learned
2: Calculations and Variables
Calculating with Python
Python Operators
The Order of Operations
Variables Are Like Labels
Using Variables
What You Learned
3: Strings, Lists, Tuples, and Maps
Strings
Creating Strings
Handling Problems with Strings
Embedding Values in Strings
Multiplying Strings
Lists Are More Powerful Than Strings
Adding Items to a List
Removing Items from a List
List Arithmetic
Tuples
Python Maps Won’t Help You Find Your Way
What You Learned
Programming Puzzles
#1: Favorites
#2: Counting Combatants
#3: Greetings!
4: Drawing with Turtles
Using Python’s turtle Module
Creating a Canvas
Moving the Turtle
What You Learned
Programming Puzzles
#1: A Rectangle
#2: A Triangle
#3: A Box Without Corners
5: Asking Questions with if and else
i f Statements
A Block Is a Group of Programming Statements
Conditions Help Us Compare Things
if-then-else Statements
if and elif Statements
Combining Conditions
Variables with No Value—None
The Difference Between Strings and Numbers
What You Learned
Programming Puzzles
#1: Are You Rich?
#2: Twinkies!
#3: Just the Right Number
#4: I Can Fight Those Ninjas
6: Going Loopy
Using for Loops
While We’re Talking About Looping . . .
What You Learned
Programming Puzzles
#1: The Hello Loop
#2: Even Numbers
#3: My Five Favorite Ingredients
#4: Your Weight on the Moon
7: Recycling Your Code with Functions and Modules
Using Functions
Parts of a Function
Variables and Scope
Using Modules
What You Learned
Programming Puzzles
#1: Basic Moon Weight Function
#2: Moon Weight Function and Years
#3: Moon Weight Program
8: How to Use Classes and Objects
Breaking Things into Classes
Children and Parents
Adding Objects to Classes
Defining Functions of Classes
Adding Class Characteristics as Functions
Why Use Classes and Objects?
Objects and Classes in Pictures
Other Useful Features of Objects and Classes
Inherited Functions
Functions Calling Other Functions
Initializing an Object
What You Learned
Programming Puzzles
#1: The Giraffe Shuffle
#2: Turtle Pitchfork
9: Python’s Built-in Functions
Using Built-in Functions
The abs Function
The bool Function
The dir Function
The eval Function
The exec Function
The float Function
The int Function
The len Function
The max and min Functions
The range Function
The sum Function
Working with Files
Creating a Test File
Opening a File in Python
Writing to Files
What You Learned
Programming Puzzles
#1: Mystery Code
#2: A Hidden Message
#3: Copying a File
10: Useful Python Modules
Making Copies with the copy Module
Keeping Track of Keywords with the keyword Module
Getting Random Numbers with the random Module
Using randint to Pick a ­Random Number
Using choice to Pick a Random Item from a List
Using shuffle to Shuffle a List
Controlling the Shell with the sys Module
Exiting the Shell with the exit function
Reading with the stdin Object
Writing with the stdout Object
Which Version of Python Am I Using?
Doing Time with the time Module
Converting a Date with asctime
Getting the Date and Time with localtime
Taking Some Time Off with sleep
Using the pickle Module to Save Information
What You Learned
Programming Puzzles
#1: Copied Cars
#2: Pickled Favorites
11: More Turtle Graphics
Starting with the Basic Square
Drawing Stars
Drawing a Car
Coloring Things In
A Function to Draw a Filled Circle
Creating Pure Black and White
A Square-Drawing Function
Drawing Filled Squares
Drawing Filled Stars
What You Learned
Programming Puzzles
#1: Drawing an Octagon
#2: Drawing a Filled Octagon
#3: Another Star-D rawing Function
12: Using tkinter for Better Graphics
Creating a Clickable Button
Using Named Parameters
Creating a Canvas for Drawing
Drawing Lines
Drawing Boxes
Drawing a Lot of Rectangles
Setting the Color
Drawing Arcs
Drawing Polygons
Displaying Text
Displaying Images
Creating Basic Animation
Making an Object React to Something
More Ways to Use the Identifier
What You Learned
Programming Puzzles
#1: Fill the Screen with Triangles
#2: The Moving Triangle
#3: The Moving Photo
Part II
Bounce!
Part II: Bounce!
13: Beginning Your First Game: Bounce!
Whack the Bouncing Ball
Creating the Game Canvas
Creating the Ball Class
Adding Some Action
Making the Ball Move
Making the Ball Bounce
Changing the Ball’s Starting Direction
What You Learned
14: Finishing Your First Game: Bounce!
Adding the Paddle
Making the Paddle Move
Finding Out When the Ball Hits the Paddle
Adding an Element of Chance
What You Learned
Programming Puzzles
#1: Delay the Game Start
#2: A Proper “Game Over”
#3: Accelerate the Ball
#4: Record the Player’s Score
Part III: Mr. Stick Man Races for the Exit
15: Creating Graphics for the Mr. Stick Man Game
Mr. Stick Man Game Plan
Getting GIMP
Creating the Game Elements
Preparing a Transparent Image
Drawing Mr. Stick Man
Drawing the Platforms
Drawing the Door
Drawing the Background
Transparency
What You Learned
16: Developing the Mr. Stick Man Game
Creating the Game Class
Setting the Window Title and ­Creating the Canvas
Finishing the _init_ Function
Creating the mainloop Function
Creating the Coords Class
Checking for Collisions
Sprites Colliding Horizontally
Sprites Colliding Vertically
Putting It All Together: Our Final Collision-Detection Code
Creating the Sprite Class
Adding the Platforms
Adding a Platform Object
Adding a Bunch of ­Platforms
What You Learned
Programming Puzzles
#1: Checkerboard
#2: Two-Image Checkerboard
#3: Bookshelf and Lamp
17: Creating Mr. Stick Man
Initializing the Stick Figure
Loading the Stick ­Figure Images
Setting Up Variables
Binding to Keys
Turning the Stick Figure Left and Right
Making the Stick Figure Jump
What We Have So Far
What You Learned
18: Completing the Mr. Stick Man Game
Animating the Stick Figure
Creating the Animate Function
Getting the Stick Figure’s Position
Making the Stick Figure Move
Testing Our Stick Figure Sprite
The Door!
Creating the DoorSprite Class
Detecting the Door
Adding the Door Object
The Final Game
What You Learned
Programming Puzzles
#1: “You Win!”
#2: Animating the Door
#3: Moving Platforms
Afterword: Where to Go from Here
Games and Graphics Programming
PyGame
Programming Languages
Java
C/C++
C#
PHP
Objective-C
Perl
Ruby
JavaScript
Final Words
Appendix: Python Keywords
Glossary
Index
For kids aged 10+ (and their parents) REAL Prog r am m i ng . REAL EASY. Illustrations by Miran Lipovaca Python is a powerful, expressive program- ming language that’s easy to learn and fun to use! But books about learning to program in Python can be kind of dull, gray, and boring, and that’s no fun for anyone. Python for Kids brings Python to life and brings you (and your parents) into the world of programming. The ever-patient Jason R. Briggs will guide you through the basics as you experi- ment with unique (and often hilarious) example programs that feature ravenous monsters, secret agents, thieving ravens, and more. New terms are defined; code is colored, dissected, and explained; and quirky, full-color illustrations keep things on the lighter side. Chapters end with programming puzzles designed to stretch your brain and strengthen your understanding. By the end of the book you’ll have programmed two complete games: a clone of the famous Pong and “Mr. Stick Man Races for the Exit”—a platform game with jumps, animation, and much more. As you strike out on your programming adventure, you’ll learn how to: M Use fundamental data structures like lists, tuples, and maps M Organize and reuse your code with func- tions and modules M Use control structures like loops and conditional statements M Draw shapes and patterns with Python’s turtle module M Create games, animations, and other graphical wonders with tkinter Why should serious adults have all the fun? Python for Kids is your ticket into the amaz- ing world of computer programming. ABOUT THE AUTHOR Jason R. Briggs has been a programmer since the age of eight, when he first learned BASIC on a Radio Shack TRS-80. He has written software professionally as a developer and systems archi- tect and served as Contributing Editor for Java Developer’s Journal. His articles have appeared in JavaWorld, ONJava, and ONLamp. Python for Kids is his first book. www.nostarch.com TH E FI N EST I N G E E K E NTE RTAI N M E NT ™ L A N G U A G E S / P Y T H O N $34.95 ($36.95 CDN) S H E L V E I N P R O G R A M M N G I : PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R . B r i g g s P Y T H O N F O R K I D S B r i g g s PYTHON FOR KIDSreal programming.real easy.PYTHON FOR KIDS
Python for Kids
Python for Kids 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; 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 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 Acknowledgments xv 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
Programming Puzzles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 #1: Are You Rich? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 #2: Twinkies! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 #3: Just the Right Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 #4: I Can Fight Those Ninjas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 6 Going Loopy 67 Using for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 While We’re Talking About Looping... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Programming Puzzles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 #1: The Hello Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 #2: Even Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 #3: My Five Favorite Ingredients. . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 #4: Your Weight on the Moon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 7 Recycling Your Code with functions 81 and Modules Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Parts of a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Variables and Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Using Modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Programming Puzzles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 #1: Basic Moon Weight Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 #2: Moon Weight Function and Years . . . . . . . . . . . . . . . . . . . . . . . . 90 #3: Moon Weight Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 8 How to Use Classes and objects 93 Breaking Things into Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Children and Parents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Adding Objects to Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Defining Functions of Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Adding Class Characteristics as Functions . . . . . . . . . . . . . . . . . . . . 97 Why Use Classes and Objects?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Objects and Classes in Pictures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Other Useful Features of Objects and Classes . . . . . . . . . . . . . . . . . . . . . . 102 Inherited Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Functions Calling Other Functions . . . . . . . . . . . . . . . . . . . . . . . . . 104 Contents in Detail ix
分享到:
收藏