www.it-ebooks.info Hello World! www.it-ebooks.info Licensed to Deborah Christiansen <[email protected]> www.it-ebooks.info Licensed to Deborah Christiansen <[email protected]> Hello World! Computer Programming for Kids and Other Beginners WARREN SANDE CARTER SANDE MANNING Greenwich (74° w. long.) www.it-ebooks.info Licensed to Deborah Christiansen <[email protected]> For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. Sound View Court 3B fax: (609) 877-8256 Greenwich, CT 06830 email: [email protected] ©2009 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15% recycled and processed without elemental chlorine. Manning Publications Co. Development editor: Cynthia Kane Sound View Court 3B Copyeditors: Andy Carroll, Anna Welles Greenwich, CT 06830 Technical proofreader: Ignacio Beltran-Torres Typesetter: Marija Tudor Illustrator: Martin Murtonen Cover designer: Leslie Haimes Fourth, corrected printing August 2009 ISBN 978-1-933988-49-8 Printed in the United States of America 1 2 34 5 6 7 8 9 10 – MAL – 15 14 13 12 11 10 09 www.it-ebooks.info Licensed to Deborah Christiansen <[email protected]> To our family, who inspire, encourage, and support us in school, work, and life www.it-ebooks.info Licensed to Deborah Christiansen <[email protected]> www.it-ebooks.info Licensed to Deborah Christiansen <[email protected]> Contents Preface xiii Acknowledgments xix About this book xxi 1 Getting Started 1 Installing Python 1 Starting Python with IDLE 2 Instructions, ■ ■ please 3 Interacting with Python 5 Time to program 7 ■ ■ Running your first program 8 If something goes wrong 9 ■ Your second program 11 2 Remember This—Memory and Variables 14 Input, processing, output 14 Names 16 What’s in a name? 20 ■ ■ Numbers and strings 21 How “variable” are they? 22 The new ■ ■ me 23 3 Basic Math 26 The four basic operations 27 Operators 28 Order of ■ ■ operations 29 Two more operators 30 Really big and really ■ ■ small 33 vii www.it-ebooks.info Licensed to Deborah Christiansen <[email protected]> viii CONTENTS 4 Types of Data 38 Changing types 38 ■ Getting more information: type() 41 Type-conversion errors 42 Using type conversions 42 ■ 5 Input 44 raw_input() 45 ■ The print command and the comma 45 Inputting numbers 47 Input from the Web 49 ■ 6 GUIs—Graphical User Interfaces 52 What’s a GUI? 52 Our first GUI 53 GUI input 54 ■ ■ Pick your flavor 55 The number-guessing game . . . again 59 ■ Other GUI pieces 60 7 Decisions, Decisions 62 Testing, testing 62 Indenting 65 Am I seeing double? 65 ■ ■ Other kinds of tests 66 What happens if the test is false? 67 ■ Testing for more than one condition 69 Using “and” 69 ■ Using “or” 70 Using “not” 70 ■ 8 Loop the Loop 74 Counting loops 75 Using a counting loop 77 A shortcut— ■ ■ range() 78 ■ A matter of style—loop variable names 80 Counting by steps 82 Counting without numbers 84 ■ While we’re on the subject . . . 84 ■ Bailing out of a loop—break and continue 85 9 Just for You—Comments 89 Adding comments 89 Single-line comments 90 End-of-line ■ ■ comments 90 Multiline comments 90 Commenting style 91 ■ ■ Commenting out 92 10 Game Time 94 Skier 94 11 Nested and Variable Loops 99 Nested loops 99 Variable loops 101 Variable nested loops 102 ■ ■ Even more variable nested loops 103 Using nested loops 105 ■ www.it-ebooks.info Licensed to Deborah Christiansen <[email protected]> CONTENTS ix 12 Collecting Things Together—Lists 112 What’s a list? 112 Creating a list 113 Adding things to a ■ ■ list 113 What’s the dot? 114 Lists can hold anything 114 ■ ■ Getting items from a list 115 “Slicing” a list 116 Modifying ■ ■ items 118 Other ways of adding to a list 118 Deleting from a ■ ■ list 120 Searching a list 121 Looping through a list 122 ■ ■ Sorting lists 123 Mutable and immutable 126 Lists of lists: tables ■ ■ of data 126 13 Functions 131 Functions—the building blocks 131 Calling a function 133 ■ Passing arguments to a function 134 Functions with more than ■ one argument 137 Functions that return a value 139 Variable ■ ■ scope 140 Forcing a global 143 A bit of advice on naming ■ ■ variables 144 14 Objects 146 Objects in the real world 147 Objects in Python 147 ■ Object = attributes + methods 148 What’s the dot? 149 ■ Creating objects 149 An example class—HotDog 154 ■ Hiding the data 159 Polymorphism and inheritance 159 ■ Thinking ahead 162 15 Modules 164 What’s a module? 164 Why use modules? 164 Buckets of ■ ■ blocks 165 How do we create modules? 165 How do we use ■ ■ modules? 166 Namespaces 167 Standard modules 170 ■ ■ 16 Graphics 174 Getting some help—Pygame 174 A Pygame window 175 ■ Drawing in the window 178 Individual pixels 186 ■ Images 190 Let’s get moving! 192 Animation 193 ■ ■ Smoother animation 194 Bouncing the ball 196 ■ Wrapping the ball 198 17 Sprites and Collision Detection 202 Sprites 202 Bump! Collision detection 208 ■ Counting time 212 www.it-ebooks.info Licensed to Deborah Christiansen <[email protected]>