|
|
|
Copyright
|
|
|
|
Praise for Core Python Programming
|
|
|
|
Prentice Hall Core Series
|
|
|
|
Preface
|
|
|
|
Acknowledgments
|
|
|
|
Part I: Core Python
|
|
|
|
|
Chapter 1. Welcome to Python!
|
|
|
|
|
Section 1.1. What Is Python?
|
|
|
|
|
Section 1.2. Origins
|
|
|
|
|
Section 1.3. Features
|
|
|
|
|
Section 1.4. Downloading and Installing Python
|
|
|
|
|
Section 1.5. Running Python
|
|
|
|
|
Section 1.6. Python Documentation
|
|
|
|
|
Section 1.7. Comparing Python
|
|
|
|
|
Section 1.8. Other Implementations
|
|
|
|
|
Section 1.9. Exercises
|
|
|
|
|
Chapter 2. Getting Started
|
|
|
|
|
Section 2.1. Program Output, the print Statement, and "Hello World!"
|
|
|
|
|
Section 2.2. Program Input and the raw_input()Built-in Function
|
|
|
|
|
Section 2.3. Comments
|
|
|
|
|
Section 2.4. Operators
|
|
|
|
|
Section 2.5. Variables and Assignment
|
|
|
|
|
Section 2.6. Numbers
|
|
|
|
|
Section 2.7. Strings
|
|
|
|
|
Section 2.8. Lists and Tuples
|
|
|
|
|
Section 2.9. Dictionaries
|
|
|
|
|
Section 2.10. Code Blocks Use Indentation
|
|
|
|
|
Section 2.11. if Statement
|
|
|
|
|
Section 2.12. while Loop
|
|
|
|
|
Section 2.13. for Loop and the range() Built-in Function
|
|
|
|
|
Section 2.14. List Comprehensions
|
|
|
|
|
Section 2.15. Files and the
open
() and file() Built-in Functions
|
|
|
|
|
Section 2.16. Errors and Exceptions
|
|
|
|
|
Section 2.17. Functions
|
|
|
|
|
Section 2.18. Classes
|
|
|
|
|
Section 2.19. Modules
|
|
|
|
|
Section 2.20. Useful Functions
|
|
|
|
|
Section 2.21. Exercises
|
|
|
|
|
Chapter 3. Python Basics
|
|
|
|
|
Section 3.1. Statements and Syntax
|
|
|
|
|
Section 3.2. Variable Assignment
|
|
|
|
|
Section 3.3. Identifiers
|
|
|
|
|
Section 3.4. Basic Style Guidelines
|
|
|
|
|
Section 3.5. Memory Management
|
|
|
|
|
Section 3.6. First Python Programs
|
|
|
|
|
Section 3.7. Related Modules/Developer Tools
|
|
|
|
|
Section 3.8. Exercises
|
|
|
|
|
Chapter 4. Python Objects
|
|
|
|
|
Section 4.1. Python Objects
|
|
|
|
|
Section 4.2. Standard Types
|
|
|
|
|
Section 4.3. Other Built-in Types
|
|
|
|
|
Section 4.4. Internal Types
|
|
|
|
|
Section 4.5. Standard Type Operators
|
|
|
|
|
Section 4.6. Standard Type Built-in Functions
|
|
|
|
|
Section 4.7. Type Factory Functions
|
|
|
|
|
Section 4.8. Categorizing the Standard Types
|
|
|
|
|
Section 4.9. Unsupported Types
|
|
|
|
|
Section 4.10. Exercises
|
|
|
|
|
Chapter 5. Numbers
|
|
|
|
|
Section 5.1. Introduction to Numbers
|
|
|
|
|
Section 5.2. Integers
|
|
|
|
|
Section 5.3. Double Precision Floating Point Numbers
|
|
|
|
|
Section 5.4. Complex Numbers
|
|
|
|
|
Section 5.5. Operators
|
|
|
|
|
Section 5.6. Built-in and Factory Functions
|
|
|
|
|
Section 5.7. Other Numeric Types
|
|
|
|
|
Section 5.8. Related Modules
|
|
|
|
|
Section 5.9. Exercises
|
|
|
|
|
Chapter 6. Sequences: Strings, Lists, and Tuples
|
|
|
|
|
Section 6.1. Sequences
|
|
|
|
|
Section 6.2. Strings
|
|
|
|
|
Section 6.3. Strings and Operators
|
|
|
|
|
Section 6.4. String-Only Operators
|
|
|
|
|
Section 6.5. Built-in Functions
|
|
|
|
|
Section 6.6. String Built-in Methods
|
|
|
|
|
Section 6.7. Special Features of Strings
|
|
|
|
|
Section 6.8. Unicode
|
|
|
|
|
Section 6.9. Related Modules
|
|
|
|
|
Section 6.10. Summary of String Highlights
|
|
|
|
|
Section 6.11. Lists
|
|
|
|
|
Section 6.12. Operators
|
|
|
|
|
Section 6.13. Built-in Functions
|
|
|
|
|
Section 6.14. List Type Built-in Methods
|
|
|
|
|
Section 6.15. Special Features of Lists
|
|
|
|
|
Section 6.16. Tuples
|
|
|
|
|
Section 6.17. Tuple Operators and Built-in Functions
|
|
|
|
|
Section 6.18. Special Features of Tuples
|
|
|
|
|
Section 6.19. Related Modules
|
|
|
|
|
Section 6.20. *Copying Python Objects and Shallow and Deep Copies
|
|
|
|
|
Section 6.21. Summary of Sequences
|
|
|
|
|
Section 6.22. Exercises
|
|
|
|
|
Chapter 7. Mapping and Set Types
|
|
|
|
|
Section 7.1. Mapping Type: Dictionaries
|
|
|
|
|
Section 7.2. Mapping Type Operators
|
|
|
|
|
Section 7.3. Mapping Type Built-in and Factory Functions
|
|
|
|
|
Section 7.4. Mapping Type Built-in Methods
|
|
|
|
|
Section 7.5. Dictionary Keys
|
|
|
|
|
Section 7.6. Set Types
|
|
|
|
|
Section 7.7. Set Type Operators
|
|
|
|
|
Section 7.8. Built-in Functions
|
|
|
|
|
Section 7.9. Set Type Built-in Methods
|
|
|
|
|
Section 7.10. Operator, Function/Method Summary Table for Set Types
|
|
|
|
|
Section 7.11. Related Modules
|
|
|
|
|
Section 7.12. Exercises
|
|
|
|
|
Chapter 8. Conditionals and Loops
|
|
|
|
|
Section 8.1. if Statement
|
|
|
|
|
Section 8.2. else Statement
|
|
|
|
|
Section 8.3. elif (aka else-if) Statement
|
|
|
|
|
Section 8.4. Conditional Expressions (aka "the Ternary Operator")
|
|
|
|
|
Section 8.5. while Statement
|
|
|
|
|
Section 8.6. for Statement
|
|
|
|
|
Section 8.7. break Statement
|
|
|
|
|
Section 8.8. continue Statement
|
|
|
|
|
Section 8.9. pass Statement
|
|
|
|
|
Section 8.10. else Statement ... Take Two
|
|
|
|
|
Section 8.11. Iterators and the iter() Function
|
|
|
|
|
Section 8.12. List Comprehensions
|
|
|
|
|
Section 8.13. Generator Expressions
|
|
|
|
|
Section 8.14. Related Modules
|
|
|
|
|
Section 8.15. Exercises
|
|
|
|
|
Chapter 9. Files and Input/Output
|
|
|
|
|
Section 9.1. File Objects
|
|
|
|
|
Section 9.2. File Built-in Functions [open() and file()]
|
|
|
|
|
Section 9.3. File Built-in Methods
|
|
|
|
|
Section 9.4. File Built-in Attributes
|
|
|
|
|
Section 9.5. Standard Files
|
|
|
|
|
Section 9.6. Command-Line Arguments
|
|
|
|
|
Section 9.7. File System
|
|
|
|
|
Section 9.8. File Execution
|
|
|
|
|
Section 9.9. Persistent Storage Modules
|
|
|
|
|
Section 9.10. Related Modules
|
|
|
|
|
Section 9.11. Exercises
|
|
|
|
|
Chapter 10. Errors and Exceptions
|
|
|
|
|
Section 10.1. What Are Exceptions?
|
|
|
|
|
Section 10.2. Exceptions in Python
|
|
|
|
|
Section 10.3. Detecting and Handling Exceptions
|
|
|
|
|
Section 10.4. Context Management
|
|
|
|
|
Section 10.5. *Exceptions as Strings
|
|
|
|
|
Section 10.6. Raising Exceptions
|
|
|
|
|
Section 10.7. Assertions
|
|
|
|
|
Section 10.8. Standard Exceptions
|
|
|
|
|
Section 10.9. *Creating Exceptions
|
|
|
|
|
Section 10.10. Why Exceptions (Now)?
|
|
|
|
|
Section 10.11. Why Exceptions at All?
|
|
|
|
|
Section 10.12. Exceptions and the sys Module
|
|
|
|
|
Section 10.13. Related Modules
|
|
|
|
|
Section 10.14. Exercises
|
|
|
|
|
Chapter 11. Functions and Functional Programming
|
|
|
|
|
Section 11.1. What Are Functions?
|
|
|
|
|
Section 11.2. Calling Functions
|
|
|
|
|
Section 11.3. Creating Functions
|
|
|
|
|
Section 11.4. Passing Functions
|
|
|
|
|
Section 11.5. Formal Arguments
|
|
|
|
|
Section 11.6. Variable-Length Arguments
|
|
|
|
|
Section 11.7. Functional Programming
|
|
|
|
|
Section 11.8. Variable Scope
|
|
|
|
|
Section 11.9. *Recursion
|
|
|
|
|
Section 11.10. Generators
|
|
|
|
|
Section 11.11. Exercises
|
|
|
|
|
Chapter 12. Modules
|
|
|
|
|
Section 12.1. What Are Modules?
|
|
|
|
|
Section 12.2. Modules and Files
|
|
|
|
|
Section 12.3. Namespaces
|
|
|
|
|
Section 12.4. Importing Modules
|
|
|
|
|
Section 12.5. Features of Module Import
|
|
|
|
|
Section 12.6. Module Built-in Functions
|
|
|
|
|
Section 12.7. Packages
|
|
|
|
|
Section 12.8. Other Features of Modules
|
|
|
|
|
Section 12.9. Related Modules
|
|
|
|
|
Section 12.10. Exercises
|
|
|
|
|
Chapter 13. Object-Oriented Programming
|
|
|
|
|
Section 13.1. Introduction
|
|
|
|
|
Section 13.2. Object-Oriented Programming
|
|
|
|
|
Section 13.3. Classes
|
|
|
|
|
Section 13.4. Class Attributes
|
|
|
|
|
Section 13.5. Instances
|
|
|
|
|
Section 13.6. Instance Attributes
|
|
|
|
|
Section 13.7. Binding and Method Invocation
|
|
|
|
|
Section 13.8. Static Methods and Class Methods
|
|
|
|
|
Section 13.9. Composition
|
|
|
|
|
Section 13.10. Subclassing and Derivation
|
|
|
|
|
Section 13.11. Inheritance
|
|
|
|
|
Section 13.12. Built-in Functions for Classes, Instances, and Other Objects
|
|
|
|
|
Section 13.13. Customizing Classes with Special Methods
|
|
|
|
|
Section 13.14. Privacy
|
|
|
|
|
Section 13.15. *Delegation
|
|
|
|
|
Section 13.16. Advanced Features of New-Style Classes (Python 2.2+)
|
|
|
|
|
Section 13.17. Related Modules and Documentation
|
|
|
|
|
Section 13.18. Exercises
|
|
|
|
|
Chapter 14. Execution Environment
|
|
|
|
|
Section 14.1. Callable Objects
|
|
|
|
|
Section 14.2. Code Objects
|
|
|
|
|
Section 14.3. Executable Object Statements and Built-in Functions
|
|
|
|
|
Section 14.4. Executing Other (Python) Programs
|
|
|
|
|
Section 14.5. Executing Other (Non-Python) Programs
|
|
|
|
|
Section 14.6. Restricted Execution
|
|
|
|
|
Section 14.7. Terminating Execution
|
|
|
|
|
Section 14.8. Miscellaneous Operating System Interface
|
|
|
|
|
Section 14.9. Related Modules
|
|
|
|
|
Section 14.10. Exercises
|
|
|
|
Part II: Advanced Topics
|
|
|
|
|
Chapter 15. Regular Expressions
|
|
|
|
|
Section 15.1. Introduction/Motivation
|
|
|
|
|
Section 15.2. Special Symbols and Characters
|
|
|
|
|
Section 15.3. REs and Python
|
|
|
|
|
Section 15.4. Regular Expressions Example
|
|
|
|
|
Section 15.5. Exercises
|
|
|
|
|
Chapter 16. Network Programming
|
|
|
|
|
Section 16.1. Introduction
|
|
|
|
|
Section 16.2. Sockets: Communication Endpoints
|
|
|
|
|
Section 16.3. Network Programming in Python
|
|
|
|
|
Section 16.4. *SocketServer Module
|
|
|
|
|
Section 16.5. *Introduction to the Twisted Framework
|
|
|
|
|
Section 16.6. Related Modules
|
|
|
|
|
Section 16.7. Exercises
|
|
|
|
|
Chapter 17. Internet Client Programming
|
|
|
|
|
Section 17.1. What Are Internet Clients?
|
|
|
|
|
Section 17.2. Transferring Files
|
|
|
|
|
Section 17.3. Network News
|
|
|
|
|
Section 17.4. Electronic Mail
|
|
|
|
|
Section 17.5. Related Modules
|
|
|
|
|
Section 17.6. Exercises
|
|
|
|
|
Chapter 18. Multithreaded Programming
|
|
|
|
|
Section 18.1. Introduction/Motivation
|
|
|
|
|
Section 18.2. Threads and Processes
|
|
|
|
|
Section 18.3. Python, Threads, and the Global Interpreter Lock
|
|
|
|
|
Section 18.4. thread Module
|
|
|
|
|
Section 18.5. threading Module
|
|
|
|
|
Section 18.6. Related Modules
|
|
|
|
|
Section 18.7. Exercises
|
|
|
|
|
Chapter 19. GUI Programming
|
|
|
|
|
Section 19.1. Introduction
|
|
|
|
|
Section 19.2. Tkinter and Python Programming
|
|
|
|
|
Section 19.3. Tkinter Examples
|
|
|
|
|
Section 19.4. Brief Tour of Other GUIs
|
|
|
|
|
Section 19.5. Related Modules and Other GUIs
|
|
|
|
|
Section 19.6. Exercises
|
|
|
|
|
Chapter 20. Web Programming
|
|
|
|
|
Section 20.1. Introduction
|
|
|
|
|
Section 20.2. Web Surfing with Python: Creating Simple Web Clients
|
|
|
|
|
Section 20.3. Advanced Web Clients
|
|
|
|
|
Section 20.4. CGI: Helping Web Servers Process Client Data
|
|
|
|
|
Section 20.5. Building CGI Applications
|
|
|
|
|
Section 20.6. Using Unicode with CGI
|
|
|
|
|
Section 20.7. Advanced CGI
|
|
|
|
|
Section 20.8. Web (HTTP) Servers
|
|
|
|
|
Section 20.9. Related Modules
|
|
|
|
|
Section 20.10. Exercises
|
|
|
|
|
Chapter 21. Database Programming
|
|
|
|
|
Section 21.1. Introduction
|
|
|
|
|
Section 21.2. Python Database Application Programmer's Interface (DB-API)
|
|
|
|
|
Section 21.3. Object-Relational Managers (ORMs)
|
|
|
|
|
Section 21.4. Related Modules
|
|
|
|
|
Section 21.5. Exercises
|
|
|
|
|
Chapter 22. Extending Python
|
|
|
|
|
Section 22.1. Introduction/Motivation
|
|
|
|
|
Section 22.2. Extending Python by Writing Extensions
|
|
|
|
|
Section 22.3. Related Topics
|
|
|
|
|
Section 22.4. Exercises
|
|
|
|
|
Chapter 23. Miscellaneous
|
|
|
|
|
Section 23.1. Web Services
|
|
|
|
|
Section 23.2. Programming Microsoft Office with Win32 COM
|
|
|
|
|
Section 23.3. Python and Java Programming with Jython
|
|
|
|
|
Section 23.4. Exercises
|
|
|
|
|
Appendix A. Answers to Selected Exercises
|
|
|
|
|
Chapter 2
|
|
|
|
|
Chapter 3
|
|
|
|
|
Chapter 4
|
|
|
|
|
Chapter 5
|
|
|
|
|
Chapter 6
|
|
|
|
|
Chapter 7
|
|
|
|
|
Chapter 8
|
|
|
|
|
Chapter 9
|
|
|
|
|
Chapter 10
|
|
|
|
|
Chapter 11
|
|
|
|
|
Chapter 12
|
|
|
|
|
Chapter 13
|
|
|
|
|
Chapter 14
|
|
|
|
|
Chapter 15
|
|
|
|
|
Chapter 16
|
|
|
|
|
Chapter 17
|
|
|
|
|
Chapter 18
|
|
|
|
|
Chapter 19
|
|
|
|
|
Chapter 20
|
|
|
|
|
Chapter 21
|
|
|
|
|
Chapter 22
|
|
|
|
|
Chapter 23
|
|
|
|
|
Appendix B. Reference Tables
|
|
|
|
|
Python Keywords
|
|
|
|
|
Python Standard Operators and Functions
|
|
|
|
|
Numeric Type Operators and Functions
|
|
|
|
|
Sequence Type Operators and Functions
|
|
|
|
|
String Format Operator Conversion Symbols
|
|
|
|
|
String Format Operator Directives
|
|
|
|
|
String Type Built-in Methods
|
|
|
|
|
List Type Built-in Methods
|
|
|
|
|
Dictionary Type Built-in Methods
|
|
|
|
|
Set Types Operators and Functions
|
|
|
|
|
File Object Methods and Data Attriobutes
|
|
|
|
|
Python Exceptions
|
|
|
|
|
Special Methods for Classes
|
|
|
|
|
Python Operator Summary
|
|
|
|
|
Appendix 3. About the Author
|
|
|
|
Index
|