[ Team LiB ] • Table of Contents • Index • Reviews • Reader Reviews • Errata • Academic Learning Python, 2nd Edition By David Ascher, Mark Lutz Publisher: O'Reilly Pub Date: December 2003 ISBN: 0-596-00281-5 Pages: 620 Learning Python, Second Edition offers programmers a comprehensive learning tool for Python and object-oriented programming. Thoroughly updated, this guide introduces the basic elements of the latest release of Python 2.3 and covers new features, such as list comprehensions, nested scopes, and iterators/generators. [ Team LiB ] [ Team LiB ] • Table of Contents • Index • Reviews • Reader Reviews • Errata • Academic Learning Python, 2nd Edition By David Ascher, Mark Lutz Publisher: O'Reilly Pub Date: December 2003 ISBN: 0-596-00281-5 Pages: 620 Copyright Dedication Preface About This Second Edition Prerequisites This Book's Scope This Book's Style and Structure Book Updates Font Conventions About the Programs in This Book Using Code Examples How to Contact Us Acknowledgments Part I: Getting Started Chapter 1. A Python Q&A Session Section 1.1. Why Do People Use Python? Section 1.2. Is Python a Scripting Language? Section 1.3. Okay, But What's the Downside? Section 1.4. Who Uses Python Today? Section 1.5. What Can I Do with Python? Section 1.6. What Are Python's Technical Strengths? Section 1.7. How Does Python Stack Up to Language X? Chapter 2. How Python Runs Programs Section 2.1. Introducing the Python Interpreter Section 2.2. Program Execution Section 2.3. Execution Model Variations Chapter 3. How You Run Programs Section 3.1. Interactive Coding Section 3.2. System Command Lines and Files Section 3.3. Clicking Windows File Icons Section 3.4. Module Imports and Reloads Section 3.5. The IDLE User Interface Section 3.6. Other IDEs Section 3.7. Embedding Calls Section 3.8. Frozen Binary Executables Section 3.9. Text Editor Launch Options Section 3.10. Other Launch Options Section 3.11. Future Possibilities? Section 3.12. Which Option Should I Use? Section 3.13. Part I Exercises Part II: Types and Operations Chapter 4. Numbers Section 4.1. Python Program Structure Section 4.2. Why Use Built-in Types? Section 4.3. Numbers Section 4.4. Python Expression Operators Section 4.5. Numbers in Action Section 4.6. The Dynamic Typing Interlude Chapter 5. Strings Section 5.1. String Literals Section 5.2. Strings in Action Section 5.3. String Formatting Section 5.4. String Methods Section 5.5. General Type Categories Chapter 6. Lists and Dictionaries Section 6.1. Lists Section 6.2. Lists in Action Section 6.3. Dictionaries Section 6.4. Dictionaries in Action Chapter 7. Tuples, Files, and Everything Else Section 7.1. Tuples Section 7.2. Files Section 7.3. Type Categories Revisited Section 7.4. Object Generality Section 7.5. References Versus Copies Section 7.6. Comparisons, Equality, and Truth Section 7.7. Python's Type Hierarchies Section 7.8. Other Types in Python Section 7.9. Built-in Type Gotchas Section 7.10. Part II Exercises Part III: Statements and Syntax Chapter 8. Assignment, Expressions, and Print Section 8.1. Assignment Statements Section 8.2. Expression Statements Section 8.3. Print Statements Chapter 9. if Tests Section 9.1. if Statements Section 9.2. Python Syntax Rules Section 9.3. Truth Tests Chapter 10. while and for Loops Section 10.1. while Loops Section 10.2. break, continue, pass, and the Loop else Section 10.3. for Loops Section 10.4. Loop Variations Chapter 11. Documenting Python Code Section 11.1. The Python Documentation Interlude Section 11.2. Common Coding Gotchas Section 11.3. Part III Exercises Part IV: Functions Chapter 12. Function Basics Section 12.1. Why Use Functions? Section 12.2. Coding Functions Section 12.3. A First Example: Definitions and Calls Section 12.4. A Second Example: Intersecting Sequences Chapter 13. Scopes and Arguments Section 13.1. Scope Rules Section 13.2. The global Statement Section 13.3. Scopes and Nested Functions Section 13.4. Passing Arguments Section 13.5. Special Argument Matching Modes Chapter 14. Advanced Function Topics Section 14.1. Anonymous Functions: lambda Section 14.2. Applying Functions to Arguments Section 14.3. Mapping Functions Over Sequences Section 14.4. Functional Programming Tools Section 14.5. List Comprehensions Section 14.6. Generators and Iterators Section 14.7. Function Design Concepts Section 14.8. Function Gotchas Section 14.9. Part IV Exercises Part V: Modules Chapter 15. Modules: The Big Picture Section 15.1. Why Use Modules? Section 15.2. Python Program Architecture Section 15.3. How Imports Work Chapter 16. Module Coding Basics Section 16.1. Module Creation Section 16.2. Module Usage Section 16.3. Module Namespaces Section 16.4. Reloading Modules Chapter 17. Module Packages Section 17.1. Package Import Basics Section 17.2. Package Import Example Section 17.3. Why Use Package Imports? Section 17.4. A Tale of Three Systems Chapter 18. Advanced Module Topics Section 18.1. Data Hiding in Modules Section 18.2. Enabling Future Language Features Section 18.3. Mixed Usage Modes: __name__ and __main__ Section 18.4. Changing the Module Search Path Section 18.5. The import as Extension Section 18.6. Module Design Concepts Section 18.7. Module Gotchas Section 18.8. Part V Exercises Part VI: Classes and OOP Chapter 19. OOP: The Big Picture Section 19.1. Why Use Classes? Section 19.2. OOP from 30,000 Feet Chapter 20. Class Coding Basics Section 20.1. Classes Generate Multiple Instance Objects Section 20.2. Classes Are Customized by Inheritance Section 20.3. Classes Can Intercept Python Operators Chapter 21. Class Coding Details Section 21.1. The Class Statement Section 21.2. Methods Section 21.3. Inheritance Section 21.4. Operator Overloading Section 21.5. Namespaces: The Whole Story Chapter 22. Designing with Classes Section 22.1. Python and OOP Section 22.2. Classes as Records Section 22.3. OOP and Inheritance: "is-a" Relationships Section 22.4. OOP and Composition: "has-a" Relationships Section 22.5. OOP and Delegation Section 22.6. Multiple Inheritance Section 22.7. Classes Are Objects: Generic Object Factories Section 22.8. Methods Are Objects: Bound or Unbound Section 22.9. Documentation Strings Revisited Section 22.10. Classes Versus Modules Chapter 23. Advanced Class Topics Section 23.1. Extending Built-in Types Section 23.2. Pseudo-Private Class Attributes Section 23.3. "New Style" Classes in Python 2.2 Section 23.4. Class Gotchas Section 23.5. Part VI Exercises Part VII: Exceptions and Tools Chapter 24. Exception Basics Section 24.1. Why Use Exceptions? Section 24.2. Exception Handling: The Short Story Section 24.3. The try/except/else Statement Section 24.4. The try/finally Statement Section 24.5. The raise Statement Section 24.6. The assert Statement Chapter 25. Exception Objects Section 25.1. String-Based Exceptions Section 25.2. Class-Based Exceptions Section 25.3. General raise Statement Forms Chapter 26. Designing with Exceptions Section 26.1. Nesting Exception Handlers Section 26.2. Exception Idioms Section 26.3. Exception Design Tips Section 26.4. Exception Gotchas Section 26.5. Core Language Summary Section 26.6. Part VII Exercises Part VIII: The Outer Layers Chapter 27. Common Tasks in Python Section 27.1. Exploring on Your Own Section 27.2. Conversions, Numbers, and Comparisons Section 27.3. Manipulating Strings Section 27.4. Data Structure Manipulations Section 27.5. Manipulating Files and Directories Section 27.6. Internet-Related Modules Section 27.7. Executing Programs Section 27.8. Debugging, Testing, Timing, Profiling Section 27.9. Exercises Chapter 28. Frameworks Section 28.1. An Automated Complaint System Section 28.2. Interfacing with COM: Cheap Public Relations Section 28.3. A Tkinter-Based GUI Editor for Managing Form Data Section 28.4. Jython: The Felicitous Union of Python and Java Section 28.5. Exercises Chapter 29. Python Resources Section 29.1. Layers of Community Section 29.2. The Process Section 29.3. Services and Products Section 29.4. The Legal Framework: The Python Software Foundation Section 29.5. Software Section 29.6. Popular Third-Party Software Section 29.7. Web Application Frameworks Section 29.8. Tools for Python Developers Part IX: Appendixes Appendix A. Installation and Configuration Section A.1. Installing the Python Interpreter Appendix B. Solutions to Exercises Section B.1. Part I, Getting Started Section B.2. Part II, Types and Operations Section B.3. Part III, Statements and Syntax Section B.4. Part IV, Functions Section B.5. Part V, Modules Section B.6. Part VI, Classes and OOP Section B.7. Part VII, Exceptions and Tools Section B.8. Part VIII, The Outer Layers Colophon Index [ Team LiB ] [ Team LiB ] Copyright Copyright © 2004, 1999 O'Reilly & Associates, Inc. Printed in the United States of America. Published by O'Reilly & Associates, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O'Reilly & Associates books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com. Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of O'Reilly & Associates, Inc. Learning Python, the image of a wood rat, and related trade dress are trademarks of O'Reilly & Associates, Inc. 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 O'Reilly & Associates, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. [ Team LiB ] [ Team LiB ] Dedication To the late Frank Willison, our mentor, friend, and first editor. [ Team LiB ] [ Team LiB ] Preface This book provides an introduction to the Python programming language. Python is a popular object-oriented language used for both standalone programs and scripting applications in a variety of domains. It is free, portable, powerful, and remarkably easy to use. Whether you are new to programming or a professional developer, this book's goal is to bring you up to speed on the core Python language in a hurry. [ Team LiB ] [...]... application-level programming from other texts Because of its beginners' focus, Learning Python is designed to be naturally complimented by O'Reilly' s other Python books For instance, Programming Python, Second Edition provides larger and more advanced application-level examples, and was explicitly designed to be a follow-up text to the one you are reading now Roughly, the second editions of Learning Python. .. the Python language, by studying Python' s major built-in object types in depth: numbers, lists, dictionaries, and so on You can get a lot done in Python with these tools alone Part III The next part moves on to introduce Python' s statements—the code you type to create and process objects in Python It also presents Python' s general syntax model Part IV This part begins our look at Python' s higher-level... first met Python in 1992, I had no idea what an impact it would have on the next decade of my life After writing the first edition of Programming Python in 1995, I began traveling around the country and world teaching Python to both beginners and experts Since finishing the first edition of this book in 1999, I've been a full-time, independent Python trainer and writer, thanks largely to Python' s exponentially... system for use in Python scripts We won't talk about extensions much in this text, but this is really just an instance of the Python- as-control-language role that we discussed earlier A prime example of this dual language strategy is the NumPy numeric programming extension for Python; by combining compiled and optimized numeric extension libraries with the Python language, NumPy turns Python into a numeric... references We recommend O'Reilly' s Python in a Nutshell and New Riders' Python Essential Reference as references, and O'Reilly' s Python Cookbook as an example library Regardless of which books you choose, you should keep in mind that the rest of the Python story requires studying examples that are more realistic than there is space for here There are roughly 40 English language Python books available... write Python has been optimized numerous times, and Python code runs fast enough by itself in most application domains Furthermore, whenever you do something "real" in a Python script, like process a file or construct a GUI, your program is actually running at C speed since such tasks are immediately dispatched to compiled C code inside the Python interpreter More fundamentally, Python' s speed-of-development... About This Second Edition In the four years after the first edition of this book was published in late 1998, there have been substantial changes in both the Python language and in the topics presented by the authors in Python training sessions Although we have attempted to retain as much of the original version as possible, this new edition reflects recent changes in both Python and Python training On... users, Python is also being applied in real revenue-generating products, by real companies For instance, Google and Yahoo! currently use Python in Internet services; Hewlett-Packard, Seagate, and IBM use Python for hardware testing; Industrial Light and Magic and other companies use Python in the production of movie animation; and so on Probably the only common thread behind companies using Python. .. using Python today is that Python is used all over the map, in terms of application domains Its general purpose nature makes it applicable to almost all fields, not just one For more details on companies using Python today, see Python' s web site at http://www .python. org [ Team LiB ] [ Team LiB ] 1.5 What Can I Do with Python? Besides being a well-designed programming language, Python is also useful for... toolkit for constructing portable GUIs in Python Higher-level toolkits such as PythonCard and PMW are built on top of base APIs such as wxPython and Tkinter With the proper library, you can also use other GUI toolkits in Python such as Qt, GTK, MFC, and Swing For applications that run in web browsers or have simple interface requirements, both Jython and Python server-side CGI scripts provide additional . Errata • Academic Learning Python, 2nd Edition By David Ascher, Mark Lutz Publisher: O'Reilly Pub Date: December 2003 ISBN: 0-5 9 6-0 028 1-5 Pages: 620 Learning Python, Second Edition offers. Reader Reviews • Errata • Academic Learning Python, 2nd Edition By David Ascher, Mark Lutz Publisher: O'Reilly Pub Date: December 2003 ISBN: 0-5 9 6-0 028 1-5 Pages: 620 Copyright Dedication . with Python? Section 1.6. What Are Python& apos;s Technical Strengths? Section 1.7. How Does Python Stack Up to Language X? Chapter 2. How Python Runs Programs Section 2.1. Introducing the Python