251373638-Python-Programming-for-Starters

415 2 0
251373638-Python-Programming-for-Starters

Đ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

Python Programming: An Introduction to Computer Science John M Zelle, Ph.D Preliminary Second Edition Fall 2009 Copyright © 2009 John M Zelle All rights reserved No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission This document was typeset by the author with LATEX 2ǫ Contents Computers and Programs 1.1 The Universal Machine 1.2 Program Power 1.3 What is Computer Science? 1.4 Hardware Basics 1.5 Programming Languages 1.6 The Magic of Python 1.7 Inside a Python Program 1.8 Chaos and Computers 1.9 Chapter Summary 1.10 Exercises 1 12 14 16 17 21 21 22 24 24 25 27 28 28 30 32 34 37 39 40 Computing with Numbers 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial 45 45 49 51 Writing Simple Programs 2.1 The Software Development Process 2.2 Example Program: Temperature Converter 2.3 Elements of Programs 2.3.1 Names 2.3.2 Expressions 2.4 Output Statements 2.5 Assignment Statements 2.5.1 Simple Assignment 2.5.2 Assigning Input 2.5.3 Simultaneous Assignment 2.6 Definite Loops 2.7 Example Program: Future Value 2.8 Chapter Summary 2.9 Exercises i Contents ii 3.4 3.5 3.6 3.7 Limitations of Computer Arithmetic Type Conversions and Rounding Chapter Summary Exercises Objects and Graphics 4.1 Overview 4.2 The Object of Objects 4.3 Simple Graphics Programming 4.4 Using Graphical Objects 4.5 Graphing Future Value 4.6 Choosing Coordinates 4.7 Interactive Graphics 4.7.1 Getting Mouse Clicks 4.7.2 Handling Textual Input 4.8 Graphics Module Reference 4.8.1 GraphWin Objects 4.8.2 Graphics Objects 4.8.3 Entry Objects 4.8.4 Displaying Images 4.8.5 Generating Colors 4.9 Chapter Summary 4.10 Exercises Sequences: Strings, Lists, and Files 5.1 The String Data Type 5.2 Simple String Processing 5.3 Lists as Sequences 5.4 String Representation and Message Encoding 5.4.1 String Representation 5.4.2 Programming an Encoder 5.5 String Methods 5.5.1 Programming a Decoder 5.5.2 More String Methods 5.6 Lists Have Methods Too 5.7 From Encoding to Encryption 5.8 Input/Output as String Manipulation 5.8.1 Example Application: Date Conversion 5.8.2 String Formatting 5.8.3 Better Change Counter 5.9 File Processing 5.9.1 Multi-Line Strings 5.9.2 File Processing 54 57 58 59 65 65 66 67 71 75 81 84 84 86 88 89 89 91 92 92 92 93 99 99 102 105 107 107 109 110 110 113 115 116 117 117 121 123 124 125 126 Contents iii 5.9.3 Example Program: Batch Usernames 128 5.10 Chapter Summary 129 5.11 Exercises 130 Defining Functions 6.1 The Function of Functions 6.2 Functions, Informally 6.3 Future Value with a Function 6.4 Functions and Parameters: The Exciting Details 6.5 Getting Results from a Function 6.5.1 Functions That Return Values 6.5.2 Functions that Modify Parameters 6.6 Functions and Program Structure 6.7 Chapter Summary 6.8 Exercises 137 137 139 142 144 147 148 151 155 158 159 Decision Structures 7.1 Simple Decisions 7.1.1 Example: Temperature Warnings 7.1.2 Forming Simple Conditions 7.1.3 Example: Conditional Program Execution 7.2 Two-Way Decisions 7.3 Multi-Way Decisions 7.4 Exception Handling 7.5 Study in Design: Max of Three 7.5.1 Strategy 1: Compare Each to All 7.5.2 Strategy 2: Decision Tree 7.5.3 Strategy 3: Sequential Processing 7.5.4 Strategy 4: Use Python 7.5.5 Some Lessons 7.6 Chapter Summary 7.7 Exercises 165 165 166 168 169 171 174 177 180 180 182 182 185 185 186 187 Loop Structures and Booleans 8.1 For Loops: A Quick Review 8.2 Indefinite Loops 8.3 Common Loop Patterns 8.3.1 Interactive Loops 8.3.2 Sentinel Loops 8.3.3 File Loops 8.3.4 Nested Loops 8.4 Computing with Booleans 8.4.1 Boolean Operators 193 193 195 197 197 198 201 202 204 204 Contents iv 8.4.2 Boolean Algebra 8.5 Other Common Structures 8.5.1 Post-Test Loop 8.5.2 Loop and a Half 8.5.3 Boolean Expressions as Decisions 8.6 Chapter Summary 8.7 Exercises 207 208 208 210 211 214 215 Simulation and Design 9.1 Simulating Racquetball 9.1.1 A Simulation Problem 9.1.2 Analysis and Specification 9.2 Pseudo Random Numbers 9.3 Top-Down Design 9.3.1 Top-Level Design 9.3.2 Separation of Concerns 9.3.3 Second-Level Design 9.3.4 Designing simNGames 9.3.5 Third-Level Design 9.3.6 Finishing Up 9.3.7 Summary of the Design Process 9.4 Bottom-Up Implementation 9.4.1 Unit Testing 9.4.2 Simulation Results 9.5 Other Design Techniques 9.5.1 Prototyping and Spiral Development 9.5.2 The Art of Design 9.6 Chapter Summary 9.7 Exercises 221 221 222 222 223 225 226 227 228 229 230 233 235 236 236 237 238 238 240 240 241 247 247 248 248 249 252 253 253 257 259 263 263 264 10 Defining Classes 10.1 Quick Review of Objects 10.2 Example Program: Cannonball 10.2.1 Program Specification 10.2.2 Designing the Program 10.2.3 Modularizing the Program 10.3 Defining New Classes 10.3.1 Example: Multi-Sided Dice 10.3.2 Example: The Projectile Class 10.4 Data Processing with Class 10.5 Objects and Encapsulation 10.5.1 Encapsulating Useful Abstractions 10.5.2 Putting Classes in Modules Contents 10.5.3 Module Documentation 10.5.4 Working with Multiple Modules 10.6 Widgets 10.6.1 Example Program: Dice Roller 10.6.2 Building Buttons 10.6.3 Building Dice 10.6.4 The Main Program 10.7 Chapter Summary 10.8 Exercises v 264 266 267 267 267 271 274 275 276 11 Data Collections 11.1 Example Problem: Simple Statistics 11.2 Applying Lists 11.2.1 Lists and Arrays 11.2.2 List Operations 11.2.3 Statistics with Lists 11.3 Lists of Records 11.4 Designing with Lists and Classes 11.5 Case Study: Python Calculator 11.5.1 A Calculator as an Object 11.5.2 Constructing the Interface 11.5.3 Processing Buttons 11.6 Non-Sequential Collections 11.6.1 Dictionary Basics 11.6.2 Dictionary Operations 11.6.3 Example Program: Word Frequency 11.7 Chapter Summary 11.8 Exercises 283 283 285 285 286 289 293 296 301 301 302 304 307 308 309 310 315 315 12 Object-Oriented Design 12.1 The Process of OOD 12.2 Case Study: Racquetball Simulation 12.2.1 Candidate Objects and Methods 12.2.2 Implementing SimStats 12.2.3 Implementing RBallGame 12.2.4 Implementing Player 12.2.5 The Complete Program 12.3 Case Study: Dice Poker 12.3.1 Program Specification 12.3.2 Identifying Candidate Objects 12.3.3 Implementing the Model 12.3.4 A Text-Based UI 12.3.5 Developing a GUI 323 323 325 325 327 328 331 331 335 335 336 337 341 343 Contents vi 12.4 OO Concepts 12.4.1 Encapsulation 12.4.2 Polymorphism 12.4.3 Inheritance 12.5 Chapter Summary 12.6 Exercises 13 Algorithm Design and Recursion 13.1 Searching 13.1.1 A Simple Searching Problem 13.1.2 Strategy 1: Linear Search 13.1.3 Strategy 2: Binary Search 13.1.4 Comparing Algorithms 13.2 Recursive Problem-Solving 13.2.1 Recursive Definitions 13.2.2 Recursive Functions 13.2.3 Example: String Reversal 13.2.4 Example: Anagrams 13.2.5 Example: Fast Exponentiation 13.2.6 Example: Binary Search 13.2.7 Recursion vs Iteration 13.3 Sorting Algorithms 13.3.1 Naive Sorting: Selection Sort 13.3.2 Divide and Conquer: Merge Sort 13.3.3 Comparing Sorts 13.4 Hard Problems 13.4.1 Towers of Hanoi 13.4.2 The Halting Problem 13.4.3 Conclusion 13.5 Chapter Summary 13.6 Exercises 350 350 351 351 353 353 357 357 358 359 359 360 362 363 364 365 366 367 368 369 371 371 373 375 377 378 382 384 384 385 Chapter Computers and Programs Objectives • To understand the respective roles of hardware and software in a computing system • To learn what computer scientists study and the techniques that they use • To understand the basic design of a modern computer • To understand the form and function of computer programming languages • To begin using the Python programming language • To learn about chaotic models and their implications for computing 1.1 The Universal Machine Almost everyone has used a computer at one time or another Perhaps you have played computer games or used a computer to write a paper or balance your checkbook Computers are used to predict the weather, design airplanes, make movies, run businesses, perform financial transactions, and control factories Have you ever stopped to wonder what exactly a computer is? How can one device perform so many different tasks? These basic questions are the starting point for learning about computers and computer programming A modern computer can be defined as “a machine that stores and manipulates information under the control of a changeable program.” There are two key elements to this definition The first is that computers are devices for manipulating information This means we can put information into a computer, and it can transform the information into new, useful forms, and then output or display the information for our interpretation Computers are not the only machines that manipulate information When you use a simple calculator to add up a column of numbers, you are entering information (the numbers) and the

Ngày đăng: 13/04/2019, 01:35

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan