PYTHON PROGRAMMING: AN INTRODUCTION TO COMPUTER SCIENCE JOHN ZELLE FRANKLIN,BEEDLE ASSOCIATES
PYTHON programming: AN INTRODUCTION TO COMPUTER SCIENCE JOHN ZELLE FRANKLIN, BEEDLE &, ASSOCIATES
PYTHON PROGRAMMING AN INTRODUCTION TO COMPUTER SCIENCE 出 John M.Zelle Wartburg College Franklin,Beedle Associates,Inc.+8536 SW St.Helens Drive,Suite D+Wilsonville,Oregon 97070+503/682-7668 www.fbeedle.com
PYTHON PROGRAMMING AN INTRODUCTION TO COMPUTER SCIENCE ^ JohnM.Zelle Wartburg College Franklin, Beedle &i Associates, Inc. + 8536 SW St. Helens Drive, Suite D + Wilsonville, Oregon 97070 + 503/682-7668 www.fbeedle.com
President and Publisher Jim Leisy (jimleisy@fbeedle.com) Production TomSumner Dean Lake Cover Ian Shadburne Marketing Christine Collier Order Processing Krista Brown Printed in the U.S.A. Names of all products herein are used for identification purposes only and are trade- marks and/or registered trademarks of their respective owners.Franklin,Beedle Asso- ciates,Inc.,makes no claim of ownership or corporate association with the products or companies that own them. 2004 Franklin,Beedle Associates Incorporated.No part of this book may be repro- duced,stored in a retrieval system,transmitted,or transcribed,in any form or by any means-electronic,mechanical,telepathic,photocopying,recording,or otherwise-with- out prior written permission of the publisher.Requests for permission should be ad- dressed as follows: Rights and Permissions Franklin,Beedle Associates,Incorporated 8536 SW St.Helens Drive,Suite D Wilsonville,Oregon 97070 Library of Congress Cataloging-in-Publication data may be obtained from the publisher
President and Publisher Jim Leisy (jimleisy@fbeedle.com) Production TomSumner Dean Lake Cover Ian Shadburne Marketing Christine Collier Order Processing Krista Brown Printed in the U.S.A. Names of all products herein are used for identification purposes only and are trade- marks and/or registered trademarks of their respective owners. Franklin, Beedle & Associates, Inc., makes no claim of ownership or corporate association with the products or companies that own them. ©2004 Franklin, Beedle & Associates Incorporated. No part of this book may be repro- duced, stored in a retrieval system, transmitted, or transcribed, in any form or by any means—electronic, mechanical, telepathic, photocopying, recording, or otherwise—without prior written permission of the publisher. Requests for permission should be addressed as follows: Rights and Permissions Franklin, Beedle & Associates, Incorporated 8536 SW St. Helens Drive, Suite D Wilsonville, Oregon 97070 Library of Congress Cataloging-in-Publication data may be obtained from the publisher
Contents Foreword .X preface X Chapter 1 Computers and Programs 1 1.1 The Universal Machine................. 1.2 Program Power.… 1 3 1.3 What is Computer Science?............. 3 1.4 Hardware Basics… 5 1.5 Programming Languages 6 1.6 The Magic of Python............. 9 1.7 Inside a Python Program.......... 14 1.8 Chaos and Computers 17 1.9 Chapter Summary.… 18 1.10 Exercises.… 20 Chapter 2 Writing Simple Programs 25 2.1 The Software Development Process.......... 25 2.2 Example Program:Temperature Converter 26 2.3 Elements of Programs.......... .29 2.3.1 Names… 29 2.3.2 Expressions 30 2.4 Output Statements… 31 2.5 Assignment Statements. 33 2.5.1 Simple Assignment.. 33 2.5.2 Assigning Input... 5 2.5.3 Simultaneous Assignment 3 2.6 Definite Loops.......... 9 2.7 Example Program:Future Value 42
Contents Foreword ix Preface x Chapter 1 Computers and Programs 1 1.1 The Universal Machine 1 1.2 Program Power 3 1.3 What is Computer Science? 3 1.4 Hardware Basics 5 1.5 Programming Languages 6 1.6 The Magic of Python 9 1.7 Inside a Python Program 14 1.8 Chaos and Computers 17 1.9 Chapter Summary 18 1.10 Exercises 20 Chapter 2 Writing Simple Programs 25 2.1 The Software Development Process 25 2.2 Example Program: Temperature Converter 26 2.3 Elements of Programs 29 2.3.1 Names 29 2.3.2 Expressions 30 2.4 Output Statements 31 2.5 Assignment Statements 33 2.5.1 Simple Assignment 33 2.5.2 Assigning Input 35 2.5.3 Simultaneous Assignment 36 2.6 Definite Loops 39 2.7 Example Program: Future Value 42
Contents 2.8 Chapter Summary .45 2.9 Exercises .46 Chapter 3 Computing with Numbers 51 3.1 Numeric Data Types.... 51 3.2 Using the Math Library… .55 3.3 Accumulating Results:Factorial....... 58 3.4 The Limits of Int......... 61 3.5 Handling Large Numbers:Long Ints... 64 3.6 Type Conversions... 66 3.7 Chapter Summary 68 3.8 Exercises.… 69 Chapter 4 Computing with Strings 77 4.1 The String Data Type......... 77 4.2 Simple String Processing...... 82 4.3 Strings,Lists,and Sequences. 85 4.4 Strings and Secret Codes.. 87 4.4.1 String Representation.. 87 4.4.2 Programming an Encoder 89 4.4.3 Programming a Decoder........ 90 4.4.4 Other String Operations..... 94 4.4.5 From Encoding to Encryption............ 95 4.5 Input/Output as String Manipulation. 4.5.1 Example Application:Date Conversion 97 4.5.2 String F0 matting… 102 4.5.3 Better Change Counter. 104 4.6 File Processing.. 106 4.6.1 Multi-Line Strings.............. 106 4.6.2 File Processing........ 107 4.6.3 Example Program:Batch Usernames........ 111 4.6.4 Coming Attraction:Objects............. 112 4.7 Chapter Summary............. .113 4.8 Exercises 115 Chapter 5 Objects and Graphics 123 5.1 Overview 123 5.2 The Object of Objects................ 124 5.3 Simple Graphics Programming............ 125 5.4 Using Graphical Objects............ 129 5.5 Graphing Future Value......... 135 5.6 Choosing Coordinates.......... 143 5.7 Interactive Graphics.................. 146 5.7.1 Getting Mouse Clicks....... 146 5.7.2 Handling Textual Input..... 148
iv Contents 2.8 Chapter Summary 45 2.9 Exercises 46 Chapter 3 Computing with Numbers 51 3.1 Numeric Data Types 51 3.2 Using the Math Library 55 3.3 Accumulating Results: Factorial 58 3.4 The Limits of Int 61 3.5 Handling Large Numbers: Long Ints 64 3.6 Type Conversions 66 3.7 Chapter Summary 68 3.8 Exercises 69 Chapter 4 Computing with Strings 77 4.1 The String Data Type 77 4.2 Simple String Processing 82 4.3 Strings, Lists, and Sequences 85 4.4 Strings and Secret Codes 87 4.4.1 String Representation 87 4.4.2 Programming an Encoder 89 4.4.3 Programming a Decoder 90 4.4.4 Other String Operations 94 4.4.5 From Encoding to Encryption 95 4.5 Input/Output as String Manipulation 97 4.5.1 Example Application: Date Conversion 97 4.5.2 String Formatting 102 4.5.3 Better Change Counter 104 4.6 File Processing 106 4.6.1 Multi-Line Strings 106 4.6.2 File Processing 107 4.6.3 Example Program: Batch Usernames Ill 4.6.4 Coming Attraction: Objects 112 4.7 Chapter Summary 113 4.8 Exercises 115 Chapter 5 Objects and Graphics 123 5.1 Overview 123 5.2 The Object of Objects 124 5.3 Simple Graphics Programming 125 5.4 Using Graphical Objects 129 5.5 Graphing Future Value 135 5.6 Choosing Coordinates 143 5.7 I nteractive Gra phics 146 5.7.1 Getting Mouse Clicks 146 5.7.2 Handling Textual Input 148