for kidsfor kids - no starch press · (game), 193–216 adding an element of change, 212–213...

7
PYTHON FOR KIDS PYTHON FOR KIDS A Playful Introduction to Programming Jason R. Briggs

Upload: doannga

Post on 13-May-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

PYTHON FOR KIDSPYTHON FOR KIDS

A Playful Introduction to Programming

J a s o n R . B r i g g s

symbols and numbers+ (addition operator), 17\ (backslash)

to separate lines of code, 235

in strings, 29, 126{} (braces), for creating

maps, 39[] (brackets), for creating

lists, 33: (colon)

in if statements, 54in lists, 34in maps, 39

/ (division operator), 17–18. (dot operator), 106–107* (multiplication operator),

16–18() ( parentheses)

with classes and objects, 96

for creating tuples, 38% ( percent sign)

as modulo operator, 149as placeholder operator,

30–31, 175- (subtraction operator), 172D (two-dimensional)

graphics, 1633D (three-dimensional)

graphics, 164

aabs function, 109–111adding items to lists, 35 adding objects to classes, 96addition operator (+), 17Adobe Flash, 286Alice, 286

alpha channel, 222, 224and keyword, 61, 294Android mobile phones, 288animation, 164, 183, 198

defined, 308in Mr. Stick Man Races

for the Exit, 225, 260–264

with sprites, 222animation frames,

defined, 309append function, 35as keyword, 294assert keyword, 294AssertionError, 295

Bbackslash (\)

to separate lines of code, 235

in strings, 29, 126BASIC, 4BlitzBasic, 286blocks of code, 54–55, 72

defined, 308body of a function, 83bool function, 111Boolean, 111

defined, 308Bounce! (game), 193–216

adding an element of change, 212–213

ball, 196–198changing direction, 202hitting the paddle,

209–212making bounce, 200moving, 198

canvas, 194paddle, 206

moving, 207–208

braces ({}), for creating maps, 39

brackets ([]), for creating lists, 33

break keyword, 78, 295built-in functions, 109

abs, 109–111bool, 111dir, 113eval, 114exec, 116float, 64, 116int, 63, 117len, 118max, 119min, 120open, 125range function, 121–122

in for loops, 68–69, 119with list function, 82

sum, 122

CC programming

language, 288C++ programming

language, 289C# programming

language, 289calculating, 16, 115calling a function, 83

defined, 308canvases

creating with tkinter module, 167–168

creating with turtle module, 44

defined, 308characteristics, of classes,

97–98child classes, 95

defined, 308

index

Python for Kids©2012, Jason R. Briggs

314 INDEX

class keyword, 94, 296classes, 94

adding objects to, 96class functions calling

other functions, 104child classes, 95, 308defining functions, 97described using turtle

module, 100inheriting functions,

103–104parent classes, 95

classifying things using classes and objects, 94

clicking a button, 308collision detection, 209,

239–244in Bounce!, 209–212in Mr. Stick Man Races

for the Exit, 266–272

collisions, defined, 308colon (:)

in if statements, 54in lists, 34in maps, 39

colorschanging with the

itemconfig function, 189

setting with tkinter module,

174–175with turtle module,

152–155, 159using the color chooser in

tkinter module, 176command-line console, 13conditions, 57–58

and keyword, 61combining, 61defined, 308operators, 57or keyword, 61

continue keyword, 296

converting dates, 140numbers from strings, 63numbers to strings, 64

coordinates, 168Coords class, 238copy module, 130

deep copy, 132shallow copy, 132

copying and pasting, in IDLE, 22

creating fileson Mac OS X, 123–124on Ubuntu Linux,

124–125on Windows, 123

creating lists of numbers, 48, 82

creating variables, 19

Ddata, defined, 308data types

Boolean, 111floating-point

numbers, 116integers, 64, 116strings, 26–32

datesconverting, 140as objects, 140–141

def keyword, 98, 297degrees, 47–48

in arcs, 178–179defined, 308in stars, 147–148

del keyword, 35, 297delaying programs, 141deleting items

from lists, 35from maps, 40

dialogs, defined, 308dict. See mapsdimensions, defined, 308dir function, 113directories, defined, 309

division operator (/), 17–18

dot operator (.), 106–107drawing

for Mr. Stick Man Races for the Exit

background, 229door, 228Mr. Stick Man, 225platforms, 227

with tkinter module, 163–190

arcs, 177–179boxes, 170–174, 206lines, 168–169ovals (circles), 196–197polygons, 179–180

with turtle module, 43–50, 145–160

8-point star, 147boxes, 146, 206car, 151filled circle, 153filled square, 157filled stars, 158line, 169spiral star, 148

eelif keyword, 59, 297. See

also if statementselse keyword, 58, 298. See

also if statementsembedding values in strings,

30, 175EOL (end-of-line), 27errors

AssertionError, 295defined, 309indentation, 56, 71, 303highlighting in IDLE,

56, 71NameError, 85, 299, 300SyntaxError, 27, 28, 56, 71SystemExit, 137TypeError, 37, 38, 41ValueError, 64, 118

Python for Kids©2012, Jason R. Briggs

INDEX 315

escaping strings, 29eval function, 114event bindings, with tkinter

module, 186, 208event objects, 255–256events, defined, 309except keyword, 298exceptions, defined, 309exec function, 116execute, defined, 309expressions, 115, 149

ffile locations, 126file objects

close function, 127creating files

on Mac OS X, 123–124on Ubuntu Linux,

124–125on Windows, 123

opening fileson Mac OS X, 126on Ubuntu Linux, 126on Windows, 125

read function, 126write function, 127

filescreating, 123–125opening, 125–126reading from, 125, 127writing to, 126

finally keyword, 298float function, 64, 116floating-point numbers, 116for keyword, 298for loops, 68

comparing code without using loops, 69

inside loops, 73and lists, 70and range function, 68and turtle module, 147

format placeholder, 30–31, 175

frames, animation, 309from keyword, 298

functions, 11, 35, 82. See also built-in functions

append, 35calling, 83

defined, 308different values, 86

defined, 309list, 69, 82parts of a function, 83print, 12sleep, 141str, 64

Ggames. See Bounce!;

Mr. Stick Man Races for the Exit

GIF images, 182, 226GIMP (GNU Image

Manipulation Program), 222

global keyword, 299graphics

isometric, 164three-dimensional

(3D), 164two-dimensional

(2D), 163

Hhelp function, 114hexadecimal numbers, 175

defined, 309horizontal, defined, 309HTML, 289

iidentifiers, 169, 184, 188

defined, 309IDLE (integrated devel-

opment environ-ment), 10

copying and pasting, 22error highlighting, 56, 71setting up on Mac OS X, 8setting up on Windows, 6starting, 11

if keyword, 300if statements, 54. See also

elif keyword; else keyword

imagesdefined, 309displaying with tkinter

module, 181flipping, in GIMP, 227GIF, 182, 226

import keyword, 301importing modules, 44, 87

defined, 309in keyword, 301indentation

consistent spacing, 57, 72

errors, 56, 71, 303in IDLE, 56, 69, 71indenting blocks, 54

index positions, in lists, 33inheritance, 103–104initialization, defined, 310installation, defined, 310installing Python, 5

on Mac OS X, 7on Ubuntu Linux, 9on Windows, 5

instances, 96defined, 310

int function, 63, 117integers, 64, 116integrated development

environment. See IDLE

is keyword, 301isometric graphics, 164iterators, 69, 121

JJava programming

language, 288 JavaScript programming

language, 291joining lists, 36

Python for Kids©2012, Jason R. Briggs

316 INDEX

Kkeyword module, 133keywords, 293–306

and, 294as, 294assert, 294break, 78, 295class, 94, 296continue, 296def, 98, 297defined, 310del, 35, 297elif, 59, 297. See also if

statementselse, 58, 298. See also if

statementsexcept, 298finally, 298for, 298from, 298global, 299if, 300import, 301in, 301is, 301lambda, 302not, 302or, 61, 302pass, 95, 303raise, 304return, 305try, 305while, 305with, 306yield, 306

Llambda keyword, 302len function, 118Linux. See Ubuntu Linuxlists, 33

adding items to, 35changing, 33deleting items from, 35and for loops, 70index positions, 33

joining, 36length of, 118minimum value of, 120of numbers, creating,

48, 82printing contents of, 33and range function, 82subsets of, 34type errors, 37, 38

loopsdefined, 310for loops. See for loopswhile loops, 75–78

MMac OS X

creating files on, 123–124file locations on, 126installing Python on, 7opening files on, 126setting up IDLE on, 8

main loops, 197, 236maps, 39

deleting values from, 40length of, 118obtaining values of, 40replacing values in, 40type errors, 41

mathematical operationsaddition, 17division, 17–18modulo, 149multiplication, 16–18

with strings, 31with variables, 84

subtraction, 17max function, 119memory, defined, 310min function, 120modules, 87

copy, 130deep copy, 132shallow copy, 132

defined, 310importing, 44, 87keyword, 133

pickle, 142dump function, 142load function, 143

random. See random modulesys. See sys moduletime. See time moduletkinter. See tkinter

moduleturtle. See turtle module

modulo operator (%), 149Monty Python’s Flying

Circus, 4Mr. Stick Man Races for the

Exit (game)background, drawing,

229–230collision detection,

239–244Coords class, 238door, drawing, 228–229DoorSprite class, 274–275Game class, 234–238platforms

adding, 245–248drawing, 227–228

sprites, creating, 244–245Mr. Stick Man, 252–256

animating, 260-272binding to keys, 255drawing, 225–227loading images,

252–253moving, 255–257

multiline strings, 27, 116multiplication, 16–18

with strings, 31with variables, 84

nNameError, 85, 299, 300None, 61–62not keyword, 302null, defined, 310numbers,

converting from strings, 63

converting to strings, 64

Python for Kids©2012, Jason R. Briggs

INDEX 317

floating-point, 116integers, 64, 116vs. strings, 62and ValueError, 64, 118

oObjective-C programming

language, 290objects, 88, 95–96

adding to classes, 96defined, 310identifiers, 188initializing, 105reading from files, 143standard input, 88standard output, 138writing to files, 142

opening fileson Mac OS X, 126on Ubuntu Linux, 126on Windows, 125

operators, 17defined, 310modulo (%), 149order of operations, 19placeholder (%), 30

or keyword, 61, 302order of operations, 18OS X. See Mac OS X

Pparameters, 83

defined, 310named, 167

parent classes, 95defined, 310

parentheses (), 18with classes and

objects, 96for creating tuples, 38

pass keyword, 95, 303percent sign (%)

as module operator, 149as placeholder operator,

30–31, 175

PERL programming language, 290

PHP programming language, 289–290

pickle module, 142dump function, 142load function, 143

pixels, 47defined, 310

placeholders, 30, 175printing

contents of lists, 33contents of variables, 20

programming languages, 4, 288–291

for mobile phone development, 288, 290

for website development, 289, 290, 291

programsdefined, 310delaying, 141running, 13saving, 12

prompt, 11PyGame2, 286–287Python, 4

console, using, 46installing, 5

on Mac OS X, 7on Ubuntu Linux, 9on Windows, 5

saving programs, 12shell. See shell

rrandom module, 133

choice function 135creating random

rectangles, 172randint function, 134shuffle function, 136, 202

range function, 121–122in for loops, 68–69, 119with list function, 82

raise keyword, 304reading input, 89reading objects from

files, 143replacing map values, 40resetting variables, 62return keyword, 305Ruby programming

language, 290running programs, 13

ssaving programs, 12scope

defined, 310of variables, 84, 85

Scratch, 286shell, 11. See also IDLE

creating a new window, 13defined, 311

sleep function, 141software, 3

defined, 311sprites, defined, 222, 311.

See also Bounce! (game); Mr. Stick Man Races for the Exit (game)

standard input (stdin), 88standard output (stdout), 138str function, 64strings, 26

defined, 311embedding values in,

30, 175escaping, 29multiline, 27, 116multiplying, 31vs. numbers, 62syntax errors in strings,

27, 28and whitespace, 112

subset of a list, 34subtraction, 17sum function, 122

Python for Kids©2012, Jason R. Briggs

318 INDEX

syntax, 27defined, 311

SyntaxError, 27, 28, 56, 71sys module, 88, 136

exit function, 136stdin object, 137stdout object, 138version function, 138

SystemExit, 137

ttime module, 87, 138

asctime function, 140localtime function,

140–141sleep function, 141time function, 139

three-dimensional (3D) graphics, 164

tkinter module, 163animation, 183–186, 198askcolor function, 176Canvas object

coords function, 200–201

winfo_height function, 200

winfo_width function, 202

and colors, 174–177coords function, 201creating

button, 165–166canvas, 167–168

displayingimages, 181–183text, 180–181

drawingarcs, 177–179boxes, 170–174, 206lines, 168–169ovals (circles), 196–197polygons, 179–180

event binding, 186, 208and identifiers, 169,

184, 188itemconfig function of the

canvas, 189keysym variable 187move function, 207pack function, 168, 195PhotoImage, 182tk object

title function, 195update function, 197update_idletasks

function, 197wm_attributes

function, 195transparency in images,

222–223, 230creating with GIMP, 224defined, 311

try keyword, 305tuples, 38, 176, 181turtle module, 44–50,

145–160begin_fill function, 153clear function, 49color function, 152creating canvas, 44drawing

8-point star, 147boxes, 146, 206car, 151filled circle, 153filled square, 157filled stars, 158line, 169spiral star, 148

end_fill function, 153importing, 44moving

backward, 49forward, 46

Pen class, 44reset function, 49

turningleft, 47right, 49

using with for loops, 147two-dimensional (2D)

graphics, 163TypeError, 37, 38, 41

UUbuntu Linux

creating files on, 124–125file locations on, 126installing Python on, 10opening files on, 126

Unity3D, 286user input, 62

VValueError, 64, 118variables

creating, 19defined, 311printing the contents

of, 20resetting, 62scope of, 84using, 21

vertical, defined, 311

Wwhile keyword, 305while loops, 75–78whitespace, 55whole numbers, 116Windows

creating files on, 123file locations on, 126installing Python on, 5opening files on, 125setting up IDLE on, 6

with keyword, 306writing objects to files, 142

Yyield keyword, 306

Python for Kids©2012, Jason R. Briggs