Transcription of Programming in Python 3 - SMU
1 Programming in Python 3. A Complete Introduction to the Python Language Second Edition Mark Summer eld Upper Saddle River, NJ Boston Indianapolis San Francisco New York Toronto Montreal London Munich Paris Madrid Capetown Sydney Tokyo Singapore Mexico City Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions.
2 No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: Corporate and Government Sales (800) 382-3419. For sales outside the United States, please contact: International Sales Visit us on the Web: Library of Congress Cataloging-in-Publication Data Summer eld, Mark.
3 Programming in Python 3 : a complete introduction to the Python language / Mark Summer eld. 2nd ed. p. cm. Includes bibliographical references and index. ISBN 978-0-321-68056-3 (pbk. : alk. paper). 1. Python (Computer program language) 2. Object-oriented Programming (Computer science). I. Title. 2010. '3 dc22. 2009035430. Copyright 2010 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
4 For information regarding permissions, write to: Pearson Education, Inc. Rights and Contracts Department 501 Boylston Street, Suite 900. Boston, MA 02116. Fax: (617) 671-3447. ISBN-13: 978-0-321-68056-3. ISBN-10: 0-321-68056-1. Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana. First printing, November 2009. Contents at a Glance List of Tables .. xv Introduction .. 1. Chapter 1. Rapid Introduction to Procedural Programming .. 9. Chapter 2. Data Types .. 51. Chapter 3. Collection Data Types .. 107. Chapter 4. Control Structures and Functions .. 159.
5 Chapter 5. Modules .. 195. Chapter 6. Object-Oriented Programming .. 233. Chapter 7. File Handling .. 287. Chapter 8. Advanced Programming Techniques .. 339. Chapter 9. Debugging, Testing, and Pro ling .. 413. Chapter 10. Processes and Threading .. 439. Chapter 11. Networking .. 457. Chapter 12. Database Programming .. 475. Chapter 13. Regular Expressions .. 489. Chapter 14. Introduction to Parsing .. 513. Chapter 15. Introduction to GUI Programming .. 569. Epilogue .. 595. Selected Bibliography .. 597. Index .. 599. Contents List of Tables .. xv Introduction .. 1. Chapter 1. Rapid Introduction to Procedural Programming .
6 9. Creating and Running Python Programs .. 9. Python 's Beautiful Heart .. 14. Piece #1: Data Types .. 14. Piece #2: Object References .. 16. Piece #3: Collection Data Types .. 18. Piece #4: Logical Operations .. 21. Piece #5: Control Flow Statements .. 26. Piece #6: Arithmetic Operators .. 30. Piece #7: Input/Output .. 33. Piece #8: Creating and Calling Functions .. 36. Examples .. 39.. 39.. 42. Summary .. 44. Exercises .. 47. Chapter 2. Data Types .. 51. Identi ers and Keywords .. 51. Integral Types .. 54. Integers .. 54. Booleans .. 58. Floating-Point Types .. 58. Floating-Point Numbers.
7 59. Complex Numbers .. 62. Decimal Numbers .. 63. Strings .. 65. Comparing Strings .. 68. Slicing and Striding Strings .. 69. String Operators and Methods .. 71. ix String Formatting with the () Method .. 78. Character Encodings .. 91. Examples .. 94.. 94.. 97. Summary .. 102. Exercises .. 104. Chapter 3. Collection Data Types .. 107. Sequence Types .. 107. Tuples .. 108. Named Tuples .. 111. Lists .. 113. Set Types .. 120. Sets .. 121. Frozen Sets .. 125. Mapping Types .. 126. Dictionaries .. 126. Default Dictionaries .. 135. Ordered Dictionaries .. 136. Iterating and Copying Collections.
8 138. Iterators and Iterable Operations and Functions .. 138. Copying Collections .. 146. Examples .. 148.. 149.. 152. Summary .. 156. Exercises .. 158. Chapter 4. Control Structures and Functions .. 159. Control Structures .. 159. Conditional Branching .. 159. Looping .. 161. Exception Handling .. 163. Catching and Raising Exceptions .. 163. Custom Exceptions .. 168. Custom Functions .. 171. Names and Docstrings .. 176. Argument and Parameter Unpacking .. 177. x Accessing Variables in the Global Scope .. 180. Lambda Functions .. 182. Assertions .. 183. Example: .. 185. Summary .. 191. Exercise.
9 192. Chapter 5. Modules .. 195. Modules and Packages .. 195. Packages .. 199. Custom Modules .. 202. Overview of Python 's Standard Library .. 212. String Handling .. 213. Command-Line Programming .. 214. Mathematics and Numbers .. 216. Times and Dates .. 216. Algorithms and Collection Data Types .. 217. File Formats, Encodings, and Data Persistence .. 219. File, Directory, and Process Handling .. 222. Networking and Internet Programming .. 225. XML .. 226. Other Modules .. 228. Summary .. 230. Exercise .. 231. Chapter 6. Object-Oriented Programming .. 233. The Object-Oriented Approach.
10 234. Object-Oriented Concepts and Terminology .. 235. Custom Classes .. 238. Attributes and Methods .. 238. Inheritance and Polymorphism .. 243. Using Properties to Control Attribute Access .. 246. Creating Complete Fully Integrated Data Types .. 248. Custom Collection Classes .. 261. Creating Classes That Aggregate Collections .. 261. Creating Collection Classes Using Aggregation .. 269. Creating Collection Classes Using Inheritance .. 276. Summary .. 283. Exercises .. 285. xi Chapter 7. File Handling .. 287. Writing and Reading Binary Data .. 292. Pickles with Optional Compression .. 292.