FOR KIDSFOR KIDS
PYTHON
FOR KIDS
A Playful Introduction to Programming
Jason R. Briggs
Contents in Detail
About the Author 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
Python for Kids ?2012, Jason R. Briggs
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
viiiContents in Detail
Python for Kids ?2012, Jason R. Briggs
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
and Modules
81
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
Python for Kids ?2012, Jason R. Briggs
Contents in Detailix
Initializing an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Programming Puzzles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
#1: The Giraffe Shuffle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 #2: Turtle Pitchfork . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
9 Python's Built-In Functions
109
Using Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 The abs Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 The bool Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 The dir Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 The eval Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 The exec Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 The float Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 The int Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 The len Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 The max and min Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 The range Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 The sum Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Working with Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Creating a Test File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Opening a File in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Writing to Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Programming Puzzles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
#1: Mystery Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 #2: A Hidden Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 #3: Copying a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
10
Useful Python Modules
129
Making Copies with the copy Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Keeping Track of Keywords with the keyword Module . . . . . . . . . . . . . . . 133 Getting Random Numbers with the random Module . . . . . . . . . . . . . . . . 133
Using randint to Pick a R andom Number . . . . . . . . . . . . . . . . . . . 134 Using choice to Pick a Random Item from a List . . . . . . . . . . . . . . 135 Using shuffle to Shuffle a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Controlling the Shell with the sys Module . . . . . . . . . . . . . . . . . . . . . . . . . 136 Exiting the Shell with the exit function . . . . . . . . . . . . . . . . . . . . . 136 Reading with the stdin Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Writing with the stdout Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Which Version of Python Am I Using? . . . . . . . . . . . . . . . . . . . . . . 138
xContents in Detail
Python for Kids ?2012, Jason R. Briggs
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- for kidsfor kids
- sams teach yourself python in 24 hours
- introduction to python
- beginning programming with python for dummies 2nd edition
- optimization with gurobi and python
- comp150 practical programming in python
- stats 507 data analysis in python
- getting started with python programming
- maths with python documentation read the docs
Related searches
- quizzes for girls kids only
- online stories for kids for free
- math for kids 4th grade for free
- worksheets for preschool kids printable
- games for kids free online for boys
- bible lessons for elementary kids free
- story books for kids for free
- websites for kids to read for free
- best app for monitoring kids phone
- printable fun activities for kids for kindergarten
- minecraft for kids for free
- by kids for kids inventions