2009.Python for Software Design - How to Think Like a Computer Scientist.pdf

(783 KB) Pobierz
519231092 UNPDF
PYTHON FOR
SOFTWARE
DESIGN
How to Think Like a
Computer Scientist
Allen B. Downey
Olin College of Engineering
519231092.001.png
CAMBRIDGE UNIVERSITY PRESS
Cambridge, New York, Melbourne, Madrid, Cape Town, Singapore, São Paulo
Cambridge University Press
The Edinburgh Building, Cambridge CB2 8RU, UK
Published in the United States of America by Cambridge University Press, New York
www.cambridge.org
Information on this title: www.cambridge.org/9780521898119
© Allen B. Downey 2009
This publication is in copyright. Subject to statutory exception and to the
provision of relevant collective licensing agreements, no reproduction of any part
may take place without the written permission of Cambridge University Press.
First published in print format
2009
ISBN-13 978-0-511-50731-1
eBook (EBL)
ISBN-13 978-0-521-89811-9
hardback
ISBN-13 978-0-521-72596-5
paperback
Cambridge University Press has no responsibility for the persistence or accuracy
of urls for external or third-party internet websites referred to in this publication,
and does not guarantee that any content on such websites is, or will remain,
accurate or appropriate.
519231092.002.png
Contents
Preface
page xi
1 The Way of the Program
1
1.1 The Python Programming Language
1
1.2 What Is a Program?
3
1.3 What Is Debugging?
3
1.3.1 Syntax Errors
3
1.3.2 Runtime Errors
4
1.3.3 Semantic Errors
4
1.3.4 Experimental Debugging
4
1.4 Formal and Natural Languages
5
1.5 The First Program
6
1.6 Debugging
7
1.7 Glossary
8
1.8 Exercises
9
2 Variables, Expressions, and Statements
10
2.1 Values and Types
10
2.2 Variables
11
2.3 Variable Names and Keywords
13
2.4 Statements
13
2.5 Operators and Operands
14
2.6 Expressions
15
2.7 Order of Operations
15
2.8 String Operations
16
2.9 Comments
17
2.10 Debugging
17
2.11 Glossary
18
2.12 Exercises
19
v
vi
Contents
3 Functions
21
3.1 Function Calls
21
3.2 Type Conversion Functions
21
3.3 Math Functions
22
3.4 Composition
23
3.5 Adding New Functions
24
3.6 Definitions and Uses
26
3.7 Flow of Execution
26
3.8 Parameters and Arguments
27
3.9 Variables and Parameters Are Local
28
3.10 Stack Diagrams
29
3.11 Fruitful Functions and Void Functions
30
3.12 Why Functions?
31
3.13 Debugging
31
3.14 Glossary
32
3.15 Exercises
33
4 Case Study: Interface Design
35
4.1 TurtleWorld
35
4.2 Simple Repetition
36
4.3 Exercises
37
4.4 Encapsulation
38
4.5 Generalization
39
4.6 Interface Design
40
4.7 Refactoring
41
4.8 A Development Plan
42
4.9 Docstring
43
4.10 Debugging
43
4.11 Glossary
44
4.12 Exercises
44
5 Conditionals and Recursion
46
5.1 Modulus Operator
46
5.2 Boolean Expressions
46
5.3 Logical Operators
47
5.4 Conditional Execution
48
5.5 Alternative Execution
48
5.6 Chained Conditionals
49
5.7 Nested Conditionals
49
5.8 Recursion
50
5.9 Stack Diagrams for Recursive Functions
52
5.10 Infinite Recursion
52
5.11 Keyboard Input
53
5.12 Debugging
54
5.13 Glossary
55
5.14 Exercises
56
Contents
vii
6 Fruitful Functions
59
6.1 Return Values
59
6.2 Incremental Development
60
6.3 Composition
63
6.4 Boolean Functions
64
6.5 More Recursion
65
6.6 Leap of Faith
67
6.7 One More Example
67
6.8 Checking Types
68
6.9 Debugging
69
6.10 Glossary
70
6.11 Exercises
71
7 Iteration
73
7.1 Multiple Assignment
73
7.2 Updating Variables
74
7.3 The while Statement
75
7.4 break
76
7.5 Square Roots
77
7.6 Algorithms
79
7.7 Debugging
79
7.8 Glossary
80
7.9 Exercises
80
8 Strings
82
8.1 A String Is a Sequence
82
8.2 len
83
8.3 Traversal with a for Loop
83
8.4 String Slices
85
8.5 Strings Are Immutable
86
8.6 Searching
86
8.7 Looping and Counting
87
8.8 string Methods
87
8.9 The in Operator
89
8.10 String Comparison
89
8.11 Debugging
90
8.12 Glossary
92
8.13 Exercises
92
9 Case Study: Word Play
95
9.1 Reading Word Lists
95
9.2 Exercises
96
9.3 Search
97
9.4 Looping with Indices
99
9.5 Debugging
100
9.6 Glossary
101
9.7 Exercises
101
Zgłoś jeśli naruszono regulamin