Synopses & Reviews
Get a comprehensive, in-depth introduction to the core Python language with this hands-on book. Based on author Mark Lutzs popular training course, this updated fifth edition will help you quickly write efficient, high-quality code with Python. Its an ideal way to begin, whether youre new to programming or a professional developer versed in other languages.
Complete with quizzes, exercises, and helpful illustrations, this easy-to-follow, self-paced tutorial gets you started with both Python 2.7 and 3.3— the latest releases in the 3.X and 2.X lines—plus all other releases in common use today. Youll also learn some advanced language features that recently have become more common in Python code.
- Explore Pythons major built-in object types such as numbers, lists, and dictionaries
- Create and process objects with Python statements, and learn Pythons general syntax model
- Use functions to avoid code redundancy and package code for reuse
- Organize statements, functions, and other tools into larger components with modules
- Dive into classes: Pythons object-oriented programming tool for structuring code
- Write large programs with Pythons exception-handling model and development tools
- Learn advanced Python tools, including decorators, descriptors, metaclasses, and Unicode processing
If you want to write efficient, high-quality code thats easily integrated with other languages and tools, this hands-on book will help you be productive with Python quickly. Learning Python, Fifth Edition, is an easy-to-follow, self-paced tutorial based on author and Python expert Mark Lutzs popular training course.
Youll find lots of annotated examples and illustrations to help you get started with both Python 3.X and 2.X. Each chapter contains a standalone lesson on a key component of the language.
- Learn about Pythons major built-in object types such as numbers, lists, and dictionaries
- Create and process objects using Python statements, and learn Pythons general syntax model
- Structure and reuse code with functions, Pythons basic procedural tool
- Learn about Python modules: packages of statements, functions, and other tools organized into larger components
- Discover Pythons object-oriented and functional programming tools for structuring and applying code
- Learn about the exception-handling model and development tools for writing larger programs
- Explore advanced Python tools, including decorators, descriptors, metaclasses, and Unicode processing
About the Author
Mark Lutz is a leading Python trainer, the author of Pythons earliest and best-selling texts, and a pioneering figure in the Python world.
Mark is the author of the three OReilly books: Learning Python, Programming Python, and Python Pocket Reference, all currently in fourth or fifth editions. He has been using and promoting Python since 1992, started writing Python books in 1995, and began teaching Python classes in 1997. As of Spring 2013, Mark has instructed 260 Python training sessions, taught roughly 4,000 students in live classes, and written Python books that have sold 400,000 units and been translated to at least a dozen languages.
Together, his two decades of Python efforts have helped to establish it as one of the most widely used programming languages in the world today. In addition, Mark has been in the software field for 30 years. He holds BS and MS degrees in computer science from the University of Wisconsin where he explored implementations of the Prolog language, and over his career has worked as a professional software developer on compilers, programming tools, scripting applications, and assorted client/server systems.
Mark maintains a training website (http://learning-python.com) and an additional book support site on the Web (http://www.rmi.net/~lutz).
Table of Contents
Dedication; Preface; This Book's "Ecosystem"; About This Fifth Edition; The Python 2.X and 3.X Lines; This Book's Prerequisites and Effort; This Book's Structure; What This Book Is Not; This Book's Programs; Font Conventions; Book Updates and Resources; Acknowledgments; Getting Started; Chapter 1: A Python Q&A Session; 1.1 Why Do People Use Python?; 1.2 Is Python a "Scripting Language"?; 1.3 OK, but What's the Downside?; 1.4 Who Uses Python Today?; 1.5 What Can I Do with Python?; 1.6 How Is Python Developed and Supported?; 1.7 What Are Python's Technical Strengths?; 1.8 How Does Python Stack Up to Language X?; 1.9 Chapter Summary; 1.10 Test Your Knowledge: Quiz; 1.11 Test Your Knowledge: Answers; Chapter 2: How Python Runs Programs; 2.1 Introducing the Python Interpreter; 2.2 Program Execution; 2.3 Execution Model Variations; 2.4 Chapter Summary; 2.5 Test Your Knowledge: Quiz; 2.6 Test Your Knowledge: Answers; Chapter 3: How You Run Programs; 3.1 The Interactive Prompt; 3.2 System Command Lines and Files; 3.3 Unix-Style Executable Scripts: #!; 3.4 Clicking File Icons; 3.5 Module Imports and Reloads; 3.6 Using exec to Run Module Files; 3.7 The IDLE User Interface; 3.8 Other IDEs; 3.9 Other Launch Options; 3.10 Which Option Should I Use?; 3.11 Chapter Summary; 3.12 Test Your Knowledge: Quiz; 3.13 Test Your Knowledge: Answers; 3.14 Test Your Knowledge: Part I Exercises; Types and Operations; Chapter 4: Introducing Python Object Types; 4.1 The Python Conceptual Hierarchy; 4.2 Why Use Built-in Types?; 4.3 Python's Core Data Types; 4.4 Numbers; 4.5 Strings; 4.6 Lists; 4.7 Dictionaries; 4.8 Tuples; 4.9 Files; 4.10 Other Core Types; 4.11 Chapter Summary; 4.12 Test Your Knowledge: Quiz; 4.13 Test Your Knowledge: Answers; Chapter 5: Numeric Types; 5.1 Numeric Type Basics; 5.2 Numbers in Action; 5.3 Other Numeric Types; 5.4 Numeric Extensions; 5.5 Chapter Summary; 5.6 Test Your Knowledge: Quiz; 5.7 Test Your Knowledge: Answers; Chapter 6: The Dynamic Typing Interlude; 6.1 The Case of the Missing Declaration Statements; 6.2 Shared References; 6.3 Dynamic Typing Is Everywhere; 6.4 Chapter Summary; 6.5 Test Your Knowledge: Quiz; 6.6 Test Your Knowledge: Answers; Chapter 7: String Fundamentals; 7.1 This Chapter's Scope; 7.2 String Basics; 7.3 String Literals; 7.4 Strings in Action; 7.5 String Methods; 7.6 String Formatting Expressions; 7.7 String Formatting Method Calls; 7.8 General Type Categories; 7.9 Chapter Summary; 7.10 Test Your Knowledge: Quiz; 7.11 Test Your Knowledge: Answers; Chapter 8: Lists and Dictionaries; 8.1 Lists; 8.2 Lists in Action; 8.3 Dictionaries; 8.4 Dictionaries in Action; 8.5 Chapter Summary; 8.6 Test Your Knowledge: Quiz; 8.7 Test Your Knowledge: Answers; Chapter 9: Tuples, Files, and Everything Else; 9.1 Tuples; 9.2 Files; 9.3 Core Types Review and Summary; 9.4 Built-in Type Gotchas; 9.5 Chapter Summary; 9.6 Test Your Knowledge: Quiz; 9.7 Test Your Knowledge: Answers; 9.8 Test Your Knowledge: Part II Exercises; Statements and Syntax; Chapter 10: Introducing Python Statements; 10.1 The Python Conceptual Hierarchy Revisited; 10.2 Python's Statements; 10.3 A Tale of Two ifs; 10.4 A Quick Example: Interactive Loops; 10.5 Chapter Summary; 10.6 Test Your Knowledge: Quiz; 10.7 Test Your Knowledge: Answers; Chapter 11: Assignments, Expressions, and Prints; 11.1 Assignment Statements; 11.2 Expression Statements; 11.3 Print Operations; 11.4 Chapter Summary; 11.5 Test Your Knowledge: Quiz; 11.6 Test Your Knowledge: Answers; Chapter 12: if Tests and Syntax Rules; 12.1 if Statements; 12.2 Python Syntax Revisited; 12.3 Truth Values and Boolean Tests; 12.4 The if/else Ternary Expression; 12.5 Chapter Summary; 12.6 Test Your Knowledge: Quiz; 12.7 Test Your Knowledge: Answers; Chapter 13: while and for Loops; 13.1 while Loops; 13.2 break, continue, pass, and the Loop else; 13.3 for Loops; 13.4 Loop Coding Techniques; 13.5 Chapter Summary; 13.6 Test Your Knowledge: Quiz; 13.7 Test Your Knowledge: Answers; Chapter 14: Iterations and Comprehensions; 14.1 Iterations: A First Look; 14.2 List Comprehensions: A First Detailed Look; 14.3 Other Iteration Contexts; 14.4 New Iterables in Python 3.X; 14.5 Other Iteration Topics; 14.6 Chapter Summary; 14.7 Test Your Knowledge: Quiz; 14.8 Test Your Knowledge: Answers; Chapter 15: The Documentation Interlude; 15.1 Python Documentation Sources; 15.2 Common Coding Gotchas; 15.3 Chapter Summary; 15.4 Test Your Knowledge: Quiz; 15.5 Test Your Knowledge: Answers; 15.6 Test Your Knowledge: Part III Exercises; Functions and Generators; Chapter 16: Function Basics; 16.1 Why Use Functions?; 16.2 Coding Functions; 16.3 A First Example: Definitions and Calls; 16.4 A Second Example: Intersecting Sequences; 16.5 Chapter Summary; 16.6 Test Your Knowledge: Quiz; 16.7 Test Your Knowledge: Answers; Chapter 17: Scopes; 17.1 Python Scope Basics; 17.2 The global Statement; 17.3 Scopes and Nested Functions; 17.4 The nonlocal Statement in 3.X; 17.5 Why nonlocal? State Retention Options; 17.6 Chapter Summary; 17.7 Test Your Knowledge: Quiz; 17.8 Test Your Knowledge: Answers; Chapter 18: Arguments; 18.1 Argument-Passing Basics; 18.2 Special Argument-Matching Modes; 18.3 The min Wakeup Call!; 18.4 Generalized Set Functions; 18.5 Emulating the Python 3.X print Function; 18.6 Chapter Summary; 18.7 Test Your Knowledge: Quiz; 18.8 Test Your Knowledge: Answers; Chapter 19: Advanced Function Topics; 19.1 Function Design Concepts; 19.2 Recursive Functions; 19.3 Function Objects: Attributes and Annotations; 19.4 Anonymous Functions: lambda; 19.5 Functional Programming Tools; 19.6 Chapter Summary; 19.7 Test Your Knowledge: Quiz; 19.8 Test Your Knowledge: Answers; Chapter 20: Comprehensions and Generations; 20.1 List Comprehensions and Functional Tools; 20.2 Generator Functions and Expressions; 20.3 Comprehension Syntax Summary; 20.4 Chapter Summary; 20.5 Test Your Knowledge: Quiz; 20.6 Test Your Knowledge: Answers; Chapter 21: The Benchmarking Interlude; 21.1 Timing Iteration Alternatives; 21.2 Timing Iterations and Pythons with timeit; 21.3 Other Benchmarking Topics: pystones; 21.4 Function Gotchas; 21.5 Chapter Summary; 21.6 Test Your Knowledge: Quiz; 21.7 Test Your Knowledge: Answers; 21.8 Test Your Knowledge: Part IV Exercises; Modules and Packages; Chapter 22: Modules: The Big Picture; 22.1 Why Use Modules?; 22.2 Python Program Architecture; 22.3 How Imports Work; 22.4 Byte Code Files: __pycache__ in Python 3.2+; 22.5 The Module Search Path; 22.6 Chapter Summary; 22.7 Test Your Knowledge: Quiz; 22.8 Test Your Knowledge: Answers; Chapter 23: Module Coding Basics; 23.1 Module Creation; 23.2 Module Usage; 23.3 Module Namespaces; 23.4 Reloading Modules; 23.5 Chapter Summary; 23.6 Test Your Knowledge: Quiz; 23.7 Test Your Knowledge: Answers; Chapter 24: Module Packages; 24.1 Package Import Basics; 24.2 Package Import Example; 24.3 Why Use Package Imports?; 24.4 Package Relative Imports; 24.5 Python 3.3 Namespace Packages; 24.6 Chapter Summary; 24.7 Test Your Knowledge: Quiz; 24.8 Test Your Knowledge: Answers; Chapter 25: Advanced Module Topics; 25.1 Module Design Concepts; 25.2 Data Hiding in Modules; 25.3 Enabling Future Language Features: __future__; 25.4 Mixed Usage Modes: __name__ and __main__; 25.5 Example: Dual Mode Code; 25.6 Changing the Module Search Path; 25.7 The as Extension for import and from; 25.8 Example: Modules Are Objects; 25.9 Importing Modules by Name String; 25.10 Example: Transitive Module Reloads; 25.11 Module Gotchas; 25.12 Chapter Summary; 25.13 Test Your Knowledge: Quiz; 25.14 Test Your Knowledge: Answers; 25.15 Test Your Knowledge: Part V Exercises; Classes and OOP; Chapter 26: OOP: The Big Picture; 26.1 Why Use Classes?; 26.2 OOP from 30,000 Feet; 26.3 Chapter Summary; 26.4 Test Your Knowledge: Quiz; 26.5 Test Your Knowledge: Answers; Chapter 27: Class Coding Basics; 27.1 Classes Generate Multiple Instance Objects; 27.2 Classes Are Customized by Inheritance; 27.3 Classes Can Intercept Python Operators; 27.4 The World's Simplest Python Class; 27.5 Chapter Summary; 27.6 Test Your Knowledge: Quiz; 27.7 Test Your Knowledge: Answers; Chapter 28: A More Realistic Example; 28.1 Step 1: Making Instances; 28.2 Step 2: Adding Behavior Methods; 28.3 Step 3: Operator Overloading; 28.4 Step 4: Customizing Behavior by Subclassing; 28.5 Step 5: Customizing Constructors, Too; 28.6 Step 6: Using Introspection Tools; 28.7 Step 7 (Final): Storing Objects in a Database; 28.8 Future Directions; 28.9 Chapter Summary; 28.10 Test Your Knowledge: Quiz; 28.11 Test Your Knowledge: Answers; Chapter 29: Class Coding Details; 29.1 The class Statement; 29.2 Methods; 29.3 Inheritance; 29.4 Namespaces: The Conclusion; 29.5 Documentation Strings Revisited; 29.6 Classes Versus Modules; 29.7 Chapter Summary; 29.8 Test Your Knowledge: Quiz; 29.9 Test Your Knowledge: Answers; Chapter 30: Operator Overloading; 30.1 The Basics; 30.2 Indexing and Slicing: __getitem__ and __setitem__; 30.3 Index Iteration: __getitem__; 30.4 Iterable Objects: __iter__ and __next__; 30.5 Membership: __contains__, __iter__, and __getitem__; 30.6 Attribute Access: __getattr__ and __setattr__; 30.7 String Representation: __repr__ and __str__; 30.8 Right-Side and In-Place Uses: __radd__ and __iadd__; 30.9 Call Expressions: __call__; 30.10 Comparisons: __lt__, __gt__, and Others; 30.11 Boolean Tests: __bool__ and __len__; 30.12 Object Destruction: __del__; 30.13 Chapter Summary; 30.14 Test Your Knowledge: Quiz; 30.15 Test Your Knowledge: Answers; Chapter 31: Designing with Classes; 31.1 Python and OOP; 31.2 OOP and Inheritance: "Is-a" Relationships; 31.3 OOP and Composition: "Has-a" Relationships; 31.4 OOP and Delegation: "Wrapper" Proxy Objects; 31.5 Pseudoprivate Class Attributes; 31.6 Methods Are Objects: Bound or Unbound; 31.7 Classes Are Objects: Generic Object Factories; 31.8 Multiple Inheritance: "Mix-in" Classes; 31.9 Other Design-Related Topics; 31.10 Chapter Summary; 31.11 Test Your Knowledge: Quiz; 31.12 Test Your Knowledge: Answers; Chapter 32: Advanced Class Topics; 32.1 Extending Built-in Types; 32.2 The "New Style" Class Model; 32.3 New-Style Class Changes; 32.4 New-Style Class Extensions; 32.5 Static and Class Methods; 32.6 Decorators and Metaclasses: Part 1; 32.7 The super Built-in Function: For Better or Worse?; 32.8 Class Gotchas; 32.9 Chapter Summary; 32.10 Test Your Knowledge: Quiz; 32.11 Test Your Knowledge: Answers; 32.12 Test Your Knowledge: Part VI Exercises; Exceptions and Tools; Chapter 33: Exception Basics; 33.1 Why Use Exceptions?; 33.2 Exceptions: The Short Story; 33.3 Chapter Summary; 33.4 Test Your Knowledge: Quiz; 33.5 Test Your Knowledge: Answers; Chapter 34: Exception Coding Details; 34.1 The try/except/else Statement; 34.2 The try/finally Statement; 34.3 Unified try/except/finally; 34.4 The raise Statement; 34.5 The assert Statement; 34.6 with/as Context Managers; 34.7 Chapter Summary; 34.8 Test Your Knowledge: Quiz; 34.9 Test Your Knowledge: Answers; Chapter 35: Exception Objects; 35.1 Exceptions: Back to the Future; 35.2 Why Exception Hierarchies?; 35.3 Built-in Exception Classes; 35.4 Custom Print Displays; 35.5 Custom Data and Behavior; 35.6 Chapter Summary; 35.7 Test Your Knowledge: Quiz; 35.8 Test Your Knowledge: Answers; Chapter 36: Designing with Exceptions; 36.1 Nesting Exception Handlers; 36.2 Exception Idioms; 36.3 Exception Design Tips and Gotchas; 36.4 Core Language Summary; 36.5 Chapter Summary; 36.6 Test Your Knowledge: Quiz; 36.7 Test Your Knowledge: Answers; 36.8 Test Your Knowledge: Part VII Exercises; Advanced Topics; Chapter 37: Unicode and Byte Strings; 37.1 String Changes in 3.X; 37.2 String Basics; 37.3 Coding Basic Strings; 37.4 Coding Unicode Strings; 37.5 Using 3.X bytes Objects; 37.6 Using 3.X/2.6+ bytearray Objects; 37.7 Using Text and Binary Files; 37.8 Using Unicode Files; 37.9 Other String Tool Changes in 3.X; 37.10 Chapter Summary; 37.11 Test Your Knowledge: Quiz; 37.12 Test Your Knowledge: Answers; Chapter 38: Managed Attributes; 38.1 Why Manage Attributes?; 38.2 Properties; 38.3 Descriptors; 38.4 __getattr__ and __getattribute__; 38.5 Example: Attribute Validations; 38.6 Chapter Summary; 38.7 Test Your Knowledge: Quiz; Chapter 39: Decorators; 39.1 What's a Decorator?; 39.2 The Basics; 39.3 Coding Function Decorators; 39.4 Coding Class Decorators; 39.5 Managing Functions and Classes Directly; 39.6 Example: "Private" and "Public" Attributes; 39.7 Example: Validating Function Arguments; 39.8 Chapter Summary; 39.9 Test Your Knowledge: Quiz; 39.10 Test Your Knowledge: Answers; Chapter 40: Metaclasses; 40.1 To Metaclass or Not to Metaclass; 40.2 The Metaclass Model; 40.3 Declaring Metaclasses; 40.4 Coding Metaclasses; 40.5 Inheritance and Instance; 40.6 Metaclass Methods; 40.7 Example: Adding Methods to Classes; 40.8 Example: Applying Decorators to Methods; 40.9 Chapter Summary; 40.10 Test Your Knowledge: Quiz; 40.11 Test Your Knowledge: Answers; Chapter 41: All Good Things; 41.1 The Python Paradox; 41.2 Where to Go From Here; 41.3 Encore: Print Your Own Completion Certificate!; Appendixes; Installation and Configuration; Installing the Python Interpreter; Configuring Python; For More Help; The Python 3.3 Windows Launcher; The Unix Legacy; The Windows Legacy; Introducing the New Windows Launcher; A Windows Launcher Tutorial; Pitfalls of the New Windows Launcher; Conclusions: A Net Win for Windows; Python Changes and This Book; Major 2.X/3.X Differences; General Remarks: 3.X Changes; Fifth Edition Python Changes: 2.7, 3.2, 3.3; Fourth Edition Python Changes: 2.6, 3.0, 3.1; Third Edition Python Changes: 2.3, 2.4, 2.5; Earlier and Later Python Changes; Solutions to End-of-Part Exercises; Part I, Getting Started; Part II, Types and Operations; Part III, Statements and Syntax; Part IV, Functions and Generators; Part V, Modules and Packages; Part VI, Classes and OOP; Part VII, Exceptions and Tools; Colophon;