1. Trang chủ
  2. » Tất cả

Python for Software Design - How to Think Like a Computer Scientist [2009]

271 3 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 271
Dung lượng 1,12 MB

Nội dung

This page intentionally left blank Python for Software Design Python for Software Design is a concise introduction to software design using the Python programming language Intended for people with no programming experience, this book starts with the most basic concepts and gradually adds new material Some of the ideas students find most challenging, like recursion and object-oriented programming, are divided into a sequence of smaller steps and introduced over the course of several chapters The focus is on the programming process, with special emphasis on debugging The book includes a wide range of exercises, from short examples to substantial projects, so that students have ample opportunity to practice each new concept Exercise solutions and code examples along with Swampy, a suite of Python programs that is used in some of the exercises, are available from thinkpython.com Allen B Downey, Ph.D., is an Associate Professor of Computer Science at the Olin College of Engineering in Needham, Massachusetts He has taught at Wellesley College, Colby College, and UC Berkeley He has a doctorate in computer science from UC Berkeley and a master’s degree from MIT Professor Downey is the author of a previous version of this book, titled How to Think Like a Computer Scientist: Learning with Python, which he self-published in 2001 PYTHON FOR SOFTWARE DESIGN How to Think Like a Computer Scientist Allen B Downey Olin College of Engineering 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-50155-5 eBook (Adobe Reader) 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 Contents Preface page xi The Way of the Program 1.1 The Python Programming Language 1.2 What Is a Program? 1.3 What Is Debugging? 1.3.1 Syntax Errors 1.3.2 Runtime Errors 1.3.3 Semantic Errors 1.3.4 Experimental Debugging 1.4 Formal and Natural Languages 1.5 The First Program 1.6 Debugging 1.7 Glossary 1.8 Exercises Variables, Expressions, and Statements 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 2.11 2.12 Values and Types Variables Variable Names and Keywords Statements Operators and Operands Expressions Order of Operations String Operations Comments Debugging Glossary Exercises 1 3 4 10 10 11 13 13 14 15 15 16 17 17 18 19 v vi Contents Functions 21 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 21 21 22 23 24 26 26 27 28 29 30 31 31 32 33 Case Study: Interface Design 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10 4.11 4.12 Function Calls Type Conversion Functions Math Functions Composition Adding New Functions Definitions and Uses Flow of Execution Parameters and Arguments Variables and Parameters Are Local Stack Diagrams Fruitful Functions and Void Functions Why Functions? Debugging Glossary Exercises 35 TurtleWorld Simple Repetition Exercises Encapsulation Generalization Interface Design Refactoring A Development Plan Docstring Debugging Glossary Exercises 35 36 37 38 39 40 41 42 43 43 44 44 Conditionals and Recursion 46 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 5.10 5.11 5.12 5.13 5.14 Modulus Operator Boolean Expressions Logical Operators Conditional Execution Alternative Execution Chained Conditionals Nested Conditionals Recursion Stack Diagrams for Recursive Functions Infinite Recursion Keyboard Input Debugging Glossary Exercises 46 46 47 48 48 49 49 50 52 52 53 54 55 56 Contents Fruitful Functions 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 6.10 6.11 59 60 63 64 65 67 67 68 69 70 71 Iteration 73 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 73 74 75 76 77 79 79 80 80 Multiple Assignment Updating Variables The while Statement break Square Roots Algorithms Debugging Glossary Exercises Strings 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 8.10 8.11 8.12 8.13 Return Values Incremental Development Composition Boolean Functions More Recursion Leap of Faith One More Example Checking Types Debugging Glossary Exercises 59 82 A String Is a Sequence len Traversal with a for Loop String Slices Strings Are Immutable Searching Looping and Counting string Methods The in Operator String Comparison Debugging Glossary Exercises Case Study: Word Play 9.1 9.2 9.3 9.4 9.5 9.6 9.7 Reading Word Lists Exercises Search Looping with Indices Debugging Glossary Exercises 82 83 83 85 86 86 87 87 89 89 90 92 92 95 95 96 97 99 100 101 101 vii viii Contents 10 Lists 10.1 10.2 10.3 10.4 10.5 10.6 10.7 10.8 10.9 10.10 10.11 10.12 10.13 10.14 10.15 11 Dictionary as a Set of Counters Looping and Dictionaries Reverse Lookup Dictionaries and Lists Memos Global Variables Long Integers Debugging Glossary Exercises Tuples 12.1 12.2 12.3 12.4 12.5 12.6 12.7 12.8 12.9 12.10 12.11 13 A List Is a Sequence Lists Are Mutable Traversing a List List Operations List Slices List Methods Map, Filter, and Reduce Deleting Elements Lists and Strings Objects and Values Aliasing List Arguments Debugging Glossary Exercises Dictionaries 11.1 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 11.10 12 103 119 121 123 123 124 126 128 129 130 131 131 133 Tuples Are Immutable Tuple Assignment Tuples as Return Values Variable-Length Argument Tuples Lists and Tuples Dictionaries and Tuples Comparing Tuples Sequences of Sequences Debugging Glossary Exercises Case Study: Data Structure Selection 13.1 13.2 13.3 13.4 103 104 105 106 106 107 108 109 110 111 113 113 115 116 117 Word Frequency Analysis Random Numbers Word Histogram Most Common Words 133 135 136 136 138 139 141 142 143 144 145 147 147 148 149 151 ... naturally Formal languages are languages that are designed by people for specific applications For example, the notation that mathematicians use is a formal language that is particularly good at... of Cambridge University Press First published in print format 2009 ISBN-13 97 8-0 -5 1 1-5 015 5-5 eBook (Adobe Reader) ISBN-13 97 8-0 -5 2 1-8 981 1-9 hardback ISBN-13 97 8-0 -5 2 1-7 259 6-5 paperback Cambridge... something and make 1.4 Formal and Natural Languages small modifications, debugging them as you go, so that you always have a working program For example, Linux is an operating system that contains

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