没有合适的资源?快使用搜索试试~ 我知道了~
首页Python_Programming_An Introduction_to_Computer_Science(2nd Edition)
Python_Programming_An Introduction_to_Computer_Science(2nd Editi...
5星 · 超过95%的资源 需积分: 9 521 下载量 175 浏览量
更新于2023-03-16
评论 3
收藏 1.94MB PDF 举报
itpub.net]Python_Programming_An Introduction_to_Computer_Science(2nd Edition)麻省理工教科书
资源详情
资源评论
资源推荐
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, mecha nical, photocopying, recording, or
otherwise, without prior written permission.
This document was typeset by the author with L
A
T
E
X 2
ǫ
.
Contents
1 Computers and Programs 1
1.1 The Universal Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Program Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 What is Computer Science? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Hardware Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 Programming Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.6 The Magic of Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.7 Inside a Python Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.8 Chaos and Computers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.9 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2 Writing Simple Programs 21
2.1 The Software Development Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.2 Example Program: Temperature Converter . . . . . . . . . . . . . . . . . . . . . . . . 22
2.3 Elements of Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.3.1 Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.3.2 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.4 Output Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.5 Assignment Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.5.1 Simple Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.5.2 Assigning Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.5.3 Simultaneous Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.6 Definite Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.7 Example Program: Future Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.8 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
3 Computing with Numbers 45
3.1 Numeric Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.2 Using the Math Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.3 Accumulating Results: Factorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
i
ii
Contents
3.4 Limitations of Computer Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
3.5 Type Conversions and Rounding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
3.6 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
3.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
4 Objects and Graphics 65
4.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
4.2 The Object of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
4.3 Simple Graphics Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
4.4 Using Graphical Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
4.5 Graphing Future Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
4.6 Choosing Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
4.7 Interactive Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
4.7.1 Getting Mouse Clicks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
4.7.2 Handling Textual Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
4.8 Graphics Module Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
4.8.1 GraphWin Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
4.8.2 Graphics Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
4.8.3 Entry Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
4.8.4 Displaying Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
4.8.5 Generating Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
4.9 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
4.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
5 Sequences: Strings, Lists, and Files 99
5.1 The String Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
5.2 Simple String Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
5.3 Lists as Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
5.4 String Representation and Message Encoding . . . . . . . . . . . . . . . . . . . . . . 107
5.4.1 String Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
5.4.2 Programming an Encoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
5.5 String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
5.5.1 Programming a Decoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
5.5.2 More String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
5.6 Lists Have Methods Too . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
5.7 From Encoding to Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
5.8 Input/Output as String Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
5.8.1 Example Application: Date Conversion . . . . . . . . . . . . . . . . . . . . . . 117
5.8.2 String Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
5.8.3 Better Change Counter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
5.9 File Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
5.9.1 Multi-Line Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
5.9.2 File Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Contents
iii
5.9.3 Example Program: Batch Usernames . . . . . . . . . . . . . . . . . . . . . . . 128
5.10 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
5.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
6 Defining Functions 137
6.1 The Function of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
6.2 Functions, Informally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
6.3 Future Value with a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
6.4 Functions and Parameters: The Exciting Details . . . . . . . . . . . . . . . . . . . . . 144
6.5 Getting Results from a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
6.5.1 Functions That Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
6.5.2 Functions that Modify Parameters . . . . . . . . . . . . . . . . . . . . . . . . . 151
6.6 Functions and Program Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
6.7 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
6.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 9
7 Decision Structures 165
7.1 Simple Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
7.1.1 Example: Temperature Warnings . . . . . . . . . . . . . . . . . . . . . . . . . 166
7.1.2 Forming Simple Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
7.1.3 Example: Conditional Program Execution . . . . . . . . . . . . . . . . . . . . 169
7.2 Two-Way Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
7.3 Multi-Way Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
7.4 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
7.5 Study in Design: Max of Three . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
7.5.1 Strategy 1: Compare Each to All . . . . . . . . . . . . . . . . . . . . . . . . . 180
7.5.2 Strategy 2: Decision Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
7.5.3 Strategy 3: Sequential Processing . . . . . . . . . . . . . . . . . . . . . . . . . 182
7.5.4 Strategy 4: Use Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
7.5.5 Some Lessons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
7.6 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
7.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 7
8 Loop Structures and Booleans 193
8.1 For Loops: A Quick Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
8.2 Indefinite Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
8.3 Common Loop Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
8.3.1 Interactive Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
8.3.2 Sentinel Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
8.3.3 File Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
8.3.4 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
8.4 Computing with Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
8.4.1 Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
剩余413页未读,继续阅读
评论22