1. Trang chủ
  2. » Công Nghệ Thông Tin

hello world

432 2,8K 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 432
Dung lượng 29,96 MB

Nội dung

www.it-ebooks.info Hello World! Licensed to Deborah Christiansen <pedbro@gmail.com> www.it-ebooks.info Licensed to Deborah Christiansen <pedbro@gmail.com> www.it-ebooks.info Hello World! Computer Programming for Kids and Other Beginners WARREN SANDE CARTER SANDE MANNING Greenwich (74° w. long.) Licensed to Deborah Christiansen <pedbro@gmail.com> www.it-ebooks.info 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: orders@manning.com ©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 12345678910–MAL–15141312111009 Licensed to Deborah Christiansen <pedbro@gmail.com> www.it-ebooks.info To our family, who inspire, encourage, and support us in school, work, and life Licensed to Deborah Christiansen <pedbro@gmail.com> www.it-ebooks.info Licensed to Deborah Christiansen <pedbro@gmail.com> www.it-ebooks.info vii 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 3Basic Math26 The four basic operations 27 ■ Operators 28 ■ Order of operations 29 ■ Two more operators 30 ■ Really big and really small 33 Licensed to Deborah Christiansen <pedbro@gmail.com> www.it-ebooks.info viii CONTENTS 4 Types of Data 38 Changing types 38 ■ Getting more information: type() 41 Type-conversion errors 42 ■ Using type conversions 42 5Input44 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 7Decisions, Decisions62 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 Licensed to Deborah Christiansen <pedbro@gmail.com> www.it-ebooks.info ixCONTENTS 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 Licensed to Deborah Christiansen <pedbro@gmail.com> www.it-ebooks.info [...]... >>> Or try this: >>> print "Hello " * 20 Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello www.it-ebooks.info Licensed to Deborah Christiansen CHAPTER 1 Getting Started Besides math, another thing computers are good at is doing things over and over again Here we told Python to print Hello twenty times We’ll do... prompt, type print "Hello World! " and press the Enter key (On some keyboards this is called the Return key.) You need to press the Enter key after every line you type After you press the Enter key, you should get this response: Hello World! >>> The figure below shows how that looks in the IDLE window www.it-ebooks.info Licensed to Deborah Christiansen 3 4 Hello World! You are now... you’ll have to pay the shipping and handling fee based on your address To get the CD, send an email to support@manning.com with subject line Hello World! CD.” If you don’t have email, you can send a fax (609-877-8256), or a good, old-fashioned letter to: Hello World CD Request Manning Publications Co Planetarium Station PO Box 347 New York NY 10024 www.it-ebooks.info Licensed to Deborah Christiansen... graphics and no sound, except the occasional “beep” if you made a mistake! 1 www.it-ebooks.info Licensed to Deborah Christiansen 2 Hello World! Installing Python is pretty easy In the Resources section of this book’s web site (www.helloworldbook.com), find the version of the installer that matches your computer’s operating system There are versions for Windows, Mac OS X, and Linux... computer is under your command! By the way, in learning to program, there is a tradition that the first thing you make the computer do is display Hello World! ” That’s where the title of this book comes from You are following that tradition Welcome to the world of programming! Why are there all those fancy colors in IDLE? Good question! IDLE is trying to help us understand things a bit better It’s showing... for the computer to use www.it-ebooks.info Licensed to Deborah Christiansen PREFACE xv 1110001101 0001110011 0100101000 No, no, no! I only speak human-ese! >>> print Hello Hello Ah! Now I get it! Hello! About time! There are many different programming languages This book will teach you how to use one of those languages—one called Python—to tell the computer what to do Why learn... www.it-ebooks.info Licensed to Deborah Christiansen 10 Hello World! Runtime errors The second kind of error that can happen is one that Python (or IDLE) can’t detect before it runs the program This kind of error only happens when the program runs, so it is called a runtime error Here’s an example of a runtime error in a program: print "Hello, and welcome to Python!" print "I hope you will enjoy... but then we get an error message: >>> ============================ RESTART ============================ >>> Start of the Where the Hello, and welcome to Python! error message error was I hope you will enjoy learning to program Traceback (most recent call last): File "C:/HelloWorld/examples/error1.py", line 3, in print "Bye for now!" + 5 TypeError: cannot concatenate 'str' and 'int' objects >>>... free, open source software, and there is no danger installing and using it on your computers You can get the Python software—and everything else you need to use this book—for free at www.manning.com/helloworld The download files are simple to install and use and are free of viruses and spyware Books like this used to come with CDs with all the software on them, but now most readers (and publishers)... in a language like Python.) I will explain what the different parts are as we go through the rest of this book If it doesn’t work If you made a mistake, you might see something like this: >>> pront "Hello World! " SyntaxError: invalid syntax >>> That error message means you typed something that Python didn’t understand In the example above, print is misspelled pront, and Python doesn’t know what to do . www.it-ebooks.info Hello World! Licensed to Deborah Christiansen <pedbro@gmail.com> www.it-ebooks.info Licensed to Deborah Christiansen <pedbro@gmail.com> www.it-ebooks.info Hello World! Computer. no! I only speak human-ese! 1110001101 0001110011 0100101000 >>> print Hello Ah! Now I get it! Hello! Hello About time! Licensed to Deborah Christiansen <pedbro@gmail.com> www.it-ebooks.info xvi. 314 ■ Creating a deck of cards 319 ■ Crazy Eights 323 24 Computer Simulations 336 Modeling the real world 336 ■ Lunar Lander 337 ■ Keeping time 342 ■ Time objects 343 ■ Saving time to a file 347 Virtual

Ngày đăng: 01/08/2014, 17:12

Xem thêm

TỪ KHÓA LIÊN QUAN

w