Learning Python, 3rd Edition

Book description

Portable, powerful, and a breeze to use, Python is ideal for bothstandalone programs and scripting applications. With this hands-onbook, you can master the fundamentals of the core Python languagequickly and efficiently, whether you're new to programming or justnew to Python. Once you finish, you will know enough about thelanguage to use it in any application domain you choose.Learning Python is based on material from author MarkLutz's popular training courses, which he's taught over the pastdecade. Each chapter is a self-contained lesson that helps youthoroughly understand a key component of Python before youcontinue. Along with plenty of annotated examples, illustrations,and chapter summaries, every chapter also contains BrainBuilder, a unique section with practical exercises and reviewquizzes that let you practice new skills and test yourunderstanding as you go.This book covers:

  • Types and Operations -- Python's major built-in object types indepth: numbers, lists, dictionaries, and more

  • Statements and Syntax -- the code you type to create andprocess objects in Python, along with Python's general syntaxmodel

  • Functions -- Python's basic procedural tool for structuring andreusing code

  • Modules -- packages of statements, functions, and other toolsorganized into larger components

  • Classes and OOP -- Python's optional object-orientedprogramming tool for structuring code for customization andreuse

  • Exceptions and Tools -- exception handling model andstatements, plus a look at development tools for writing largerprograms

  • Learning Python gives you a deep and completeunderstanding of the language that will help you comprehend anyapplication-level examples of Python that you later encounter. Ifyou're ready to discover what Google and YouTube see in Python,this book is the best way to get started.

    Publisher resources

    View/Submit Errata

    Table of contents

    1. Dedication
    2. A Note Regarding Supplemental Files
    3. Preface
      1. About This Third Edition
        1. This Edition’s Python Language Changes
        2. This Edition’s Python Training Changes
        3. This Edition’s Structural Changes
        4. This Edition’s Scope Changes
      2. About This Book
        1. This Book’s Prerequisites
        2. This Book’s Scope and Other Books
        3. This Book’s Style and Structure
      3. Book Updates
      4. About the Programs in This Book
      5. Preparing for Python 3.0
      6. About This Series
      7. Using Code Examples
      8. Font Conventions
      9. Safari® Books Online
      10. How to Contact Us
      11. Acknowledgments
    4. I. Getting Started
      1. 1. A Python Q&A Session
        1. Why Do People Use Python?
          1. Software Quality
          2. Developer Productivity
        2. Is Python a “Scripting Language”?
        3. OK, but What’s the Downside?
        4. Who Uses Python Today?
        5. What Can I Do with Python?
          1. Systems Programming
          2. GUIs
          3. Internet Scripting
          4. Component Integration
          5. Database Programming
          6. Rapid Prototyping
          7. Numeric and Scientific Programming
          8. Gaming, Images, AI, XML, Robots, and More
        6. What Are Python’s Technical Strengths?
          1. It’s Object Oriented
          2. It’s Free
          3. It’s Portable
          4. It’s Powerful
          5. It’s Mixable
          6. It’s Easy to Use
          7. It’s Easy to Learn
          8. It’s Named After Monty Python
        7. How Does Python Stack Up to Language X?
        8. Chapter Summary
      2. 2. How Python Runs Programs
        1. Introducing the Python Interpreter
        2. Program Execution
          1. The Programmer’s View
          2. Python’s View
            1. Byte code compilation
            2. The Python Virtual Machine (PVM)
            3. Performance implications
            4. Development implications
        3. Execution Model Variations
          1. Python Implementation Alternatives
            1. CPython
            2. Jython
            3. IronPython
          2. Execution Optimization Tools
            1. The Psyco just-in-time compiler
            2. The Shedskin C++ translator
          3. Frozen Binaries
          4. Future Possibilities?
        4. Chapter Summary
      3. 3. How You Run Programs
        1. Interactive Coding
          1. Using the Interactive Prompt
        2. System Command Lines and Files
          1. Using Command Lines and Files
          2. Unix Executable Scripts (#!)
        3. Clicking File Icons
          1. Clicking Icons on Windows
          2. The raw_input Trick
          3. Other Icon-Click Limitations
        4. Module Imports and Reloads
          1. The Grander Module Story: Attributes
            1. Modules and namespaces
          2. import and reload Usage Notes
        5. The IDLE User Interface
          1. IDLE Basics
          2. Using IDLE
          3. Advanced IDLE Tools
        6. Other IDEs
        7. Embedding Calls
        8. Frozen Binary Executables
        9. Text Editor Launch Options
        10. Other Launch Options
        11. Future Possibilities?
        12. Which Option Should I Use?
        13. Chapter Summary
        14. BRAIN BUILDER
          1. Part I Exercises
    5. II. Types and Operations
      1. 4. Introducing Python Object Types
        1. Why Use Built-in Types?
          1. Python’s Core Data Types
        2. Numbers
        3. Strings
          1. Sequence Operations
          2. Immutability
          3. Type-Specific Methods
          4. Getting Help
          5. Other Ways to Code Strings
          6. Pattern Matching
        4. Lists
          1. Sequence Operations
          2. Type-Specific Operations
          3. Bounds Checking
          4. Nesting
          5. List Comprehensions
        5. Dictionaries
          1. Mapping Operations
          2. Nesting Revisited
          3. Sorting Keys: for Loops
          4. Iteration and Optimization
          5. Missing Keys: if Tests
        6. Tuples
          1. Why Tuples?
        7. Files
          1. Other File-Like Tools
        8. Other Core Types
          1. How to Break Your Code’s Flexibility
          2. User-Defined Classes
          3. And Everything Else
        9. Chapter Summary
      2. 5. Numbers
        1. Python Numeric Types
          1. Numeric Literals
          2. Built-in Numeric Tools and Extensions
        2. Python Expression Operators
          1. Mixed Operators Follow Operator Precedence
          2. Parentheses Group Subexpressions
          3. Mixed Types Are Converted Up
          4. Preview: Operator Overloading
        3. Numbers in Action
          1. Variables and Basic Expressions
          2. Numeric Display Formats
          3. Division: Classic, Floor, and True
          4. Bitwise Operations
          5. Long Integers
          6. Complex Numbers
          7. Hexadecimal and Octal Notation
          8. Other Built-in Numeric Tools
        4. Other Numeric Types
          1. Decimal Numbers
          2. Sets
          3. Booleans
          4. Third-Party Extensions
        5. Chapter Summary
      3. 6. The Dynamic Typing Interlude
        1. The Case of the Missing Declaration Statements
          1. Variables, Objects, and References
          2. Types Live with Objects, Not Variables
          3. Objects Are Garbage-Collected
        2. Shared References
          1. Shared References and In-Place Changes
          2. Shared References and Equality
        3. Dynamic Typing Is Everywhere
        4. Chapter Summary
      4. 7. ofsmallStrings
        1. String Literals
          1. Single- and Double-Quoted Strings Are the Same
          2. Escape Sequences Represent Special Bytes
          3. Raw Strings Suppress Escapes
          4. Triple Quotes Code Multiline Block Strings
          5. Unicode Strings Encode Larger Character Sets
        2. Strings in Action
          1. Basic Operations
          2. Indexing and Slicing
            1. Extended slicing: the third limit
          3. String Conversion Tools
            1. Character code conversions
          4. Changing Strings
        3. String Formatting
          1. Advanced String Formatting
          2. Dictionary-Based String Formatting
        4. String Methods
          1. String Method Examples: Changing Strings
          2. String Method Examples: Parsing Text
          3. Other Common String Methods in Action
          4. The Original string Module
        5. General Type Categories
          1. Types Share Operation Sets by Categories
          2. Mutable Types Can Be Changed In-Place
        6. Chapter Summary
      5. 8. Lists and Dictionaries
        1. Lists
        2. Lists in Action
          1. Basic List Operations
          2. Indexing, Slicing, and Matrixes
          3. Changing Lists In-Place
            1. Index and slice assignments
            2. List method calls
            3. Other common list operations
        3. Dictionaries
        4. Dictionaries in Action
          1. Basic Dictionary Operations
          2. Changing Dictionaries In-Place
          3. More Dictionary Methods
          4. A Languages Table
          5. Dictionary Usage Notes
            1. Using dictionaries to simulate flexible lists
            2. Using dictionaries for sparse data structures
            3. Avoiding missing-key errors
            4. Using dictionaries as “records”
            5. Other ways to make dictionaries
        5. Chapter Summary
      6. 9. Tuples, Files, and Everything Else
        1. Tuples
          1. Tuples in Action
            1. Tuple syntax peculiarities: commas and parentheses
            2. Conversions and immutability
          2. Why Lists and Tuples?
        2. Files
          1. Opening Files
          2. Using Files
          3. Files in Action
            1. Storing and parsing Python objects in files
            2. Storing native Python objects with pickle
            3. Storing and parsing packed binary data in files
          4. Other File Tools
        3. Type Categories Revisited
        4. Object Flexibility
        5. References Versus Copies
        6. Comparisons, Equality, and Truth
          1. The Meaning of True and False in Python
        7. Python’s Type Hierarchies
        8. Other Types in Python
        9. Built-in Type Gotchas
          1. Assignment Creates References, Not Copies
          2. Repetition Adds One Level Deep
          3. Beware of Cyclic Data Structures
          4. Immutable Types Can’t Be Changed In-Place
        10. Chapter Summary
        11. BRAIN BUILDER
          1. Part II Exercises
    6. III. Statements and Syntax
      1. 10. Introducing Python Statements
        1. Python Program Structure Revisited
          1. Python’s Statements
        2. A Tale of Two ifs
          1. What Python Adds
          2. What Python Removes
            1. Parentheses are optional
            2. End of line is end of statement
            3. End of indentation is end of block
          3. Why Indentation Syntax?
          4. A Few Special Cases
            1. Statement rule special cases
            2. Block rule special case
        3. A Quick Example: Interactive Loops
          1. A Simple Interactive Loop
          2. Doing Math on User Inputs
          3. Handling Errors by Testing Inputs
          4. Handling Errors with try Statements
          5. Nesting Code Three Levels Deep
        4. Chapter Summary
      2. 11. rwordsAssignment, Expressions, and print
        1. Assignment Statements
          1. Assignment Statement Forms
          2. Sequence Assignments
            1. Advanced sequence assignment patterns
          3. Multiple-Target Assignments
            1. Multiple-target assignment and shared references
          4. Augmented Assignments
            1. Augmented assignment and shared references
          5. Variable Name Rules
            1. Naming conventions
            2. Names have no type, but objects do
        2. Expression Statements
          1. Expression Statements and In-Place Changes
        3. print Statements
          1. The Python “Hello World” Program
          2. Redirecting the Output Stream
          3. The print >> file Extension
        4. Chapter Summary
      3. 12. if Tests
        1. if Statements
          1. General Format
          2. Basic Examples
          3. Multiway Branching
        2. Python Syntax Rules
          1. Block Delimiters
          2. Statement Delimiters
          3. A Few Special Cases
        3. Truth Tests
          1. The if/else Ternary Expression
        4. Chapter Summary
      4. 13. while and for Loops
        1. while Loops
          1. General Format
          2. Examples
        2. break, continue, pass, and the Loop else
          1. General Loop Format
          2. Examples
            1. pass
            2. continue
            3. break
            4. else
            5. More on the loop else clause
        3. for Loops
          1. General Format
          2. Examples
            1. Basic usage
            2. Other data types
            3. Tuple assignment in for
            4. Nested for loops
        4. Iterators: A First Look
          1. File Iterators
          2. Other Built-in Type Iterators
          3. Other Iteration Contexts
          4. User-Defined Iterators
        5. Loop Coding Techniques
          1. Counter Loops: while and range
          2. Nonexhaustive Traversals: range
          3. Changing Lists: range
          4. Parallel Traversals: zip and map
            1. Dictionary construction with zip
          5. Generating Both Offsets and Items: enumerate
        6. List Comprehensions: A First Look
          1. List Comprehension Basics
          2. Using List Comprehensions on Files
          3. Extended List Comprehension Syntax
        7. Chapter Summary
      5. 14. The Documentation Interlude
        1. Python Documentation Sources
          1. # Comments
          2. The dir Function
          3. Docstrings: _ _doc_ _
            1. User-defined docstrings
            2. Docstring standards
            3. Built-in docstrings
          4. PyDoc: The help Function
          5. PyDoc: HTML Reports
          6. Standard Manual Set
          7. Web Resources
          8. Published Books
        2. Common Coding Gotchas
        3. Chapter Summary
        4. BRAIN BUILDER
          1. Part III Exercises
    7. IV. Functions
      1. 15. Function Basics
        1. Why Use Functions?
          1. Coding Functions
          2. def Statements
          3. def Executes at Runtime
        2. A First Example: Definitions and Calls
          1. Definition
          2. Calls
          3. Polymorphism in Python
        3. A Second Example: Intersecting Sequences
          1. Definition
          2. Calls
          3. Polymorphism Revisited
          4. Local Variables
        4. Chapter Summary
      2. 16. Scopes and Arguments
        1. Scope Rules
          1. Python Scope Basics
          2. Name Resolution: The LEGB Rule
          3. Scope Example
          4. The Built-in Scope
        2. The global Statement
          1. Minimize Global Variables
          2. Minimize Cross-File Changes
          3. Other Ways to Access Globals
        3. Scopes and Nested Functions
          1. Nested Scope Details
          2. Nested Scope Examples
            1. Factory functions
            2. Retaining enclosing scopes’ state with defaults
            3. Nested scopes and lambdas
            4. Scopes versus defaults with loop variables
            5. Arbitrary scope nesting
        4. Passing Arguments
          1. Arguments and Shared References
          2. Avoiding Mutable Argument Changes
          3. Simulating Output Parameters
        5. Special Argument-Matching Modes
          1. Keyword and Default Examples
            1. Keywords
            2. Defaults
          2. Arbitrary Arguments Examples
            1. Collecting arguments
            2. Unpacking arguments
          3. Combining Keywords and Defaults
          4. The min Wakeup Call
            1. Full credit
            2. Bonus points
            3. The punch line
          5. A More Useful Example: General Set Functions
          6. Argument Matching: The Gritty Details
        6. Chapter Summary
      3. 17. Advanced Function Topics
        1. Anonymous Functions: lambda
          1. lambda Expressions
          2. Why Use lambda?
          3. How (Not) to Obfuscate Your Python Code
          4. Nested lambdas and Scopes
        2. Applying Functions to Arguments
          1. The apply Built-in
            1. Passing keyword arguments
          2. apply-Like Call Syntax
        3. Mapping Functions over Sequences: map
        4. Functional Programming Tools: filter and reduce
        5. List Comprehensions Revisited: Mappings
          1. List Comprehension Basics
          2. Adding Tests and Nested Loops
          3. List Comprehensions and Matrixes
          4. Comprehending List Comprehensions
        6. Iterators Revisited: Generators
          1. Generator Function Example
          2. Extended Generator Function Protocol: send Versus next
          3. Iterators and Built-in Types
          4. Generator Expressions: Iterators Meet List Comprehensions
        7. Timing Iteration Alternatives
        8. Function Design Concepts
          1. Functions Are Objects: Indirect Calls
        9. Function Gotchas
          1. Local Names Are Detected Statically
          2. Defaults and Mutable Objects
          3. Functions Without returns
          4. Enclosing Scope Loop Variables
        10. Chapter Summary
        11. BRAIN BUILDER
          1. Part IV Exercises
    8. V. Modules
      1. 18. Modules: The Big Picture
        1. Why Use Modules?
        2. Python Program Architecture
          1. How to Structure a Program
          2. Imports and Attributes
          3. Standard Library Modules
        3. How Imports Work
          1. 1. Find It
            1. The module search path
            2. The sys.path list
            3. Module file selection
            4. Advanced module selection concepts
          2. 2. Compile It (Maybe)
          3. 3. Run It
        4. Chapter Summary
      2. 19. Module Coding Basics
        1. Module Creation
        2. Module Usage
          1. The import Statement
          2. The from statement
          3. The from * Statement
          4. Imports Happen Only Once
          5. import and from Are Assignments
          6. Cross-File Name Changes
          7. import and from Equivalence
          8. Potential Pitfalls of the from Statement
            1. When import is required
        3. Module Namespaces
          1. Files Generate Namespaces
          2. Attribute Name Qualification
          3. Imports Versus Scopes
          4. Namespace Nesting
        4. Reloading Modules
          1. reload Basics
          2. reload Example
        5. Chapter Summary
      3. 20. Module Packages
        1. Package Import Basics
          1. Packages and Search Path Settings
          2. Package _ _init_ _.py Files
        2. Package Import Example
          1. from Versus import with Packages
        3. Why Use Package Imports?
          1. A Tale of Three Systems
        4. Chapter Summary
      4. 21. Advanced Module Topics
        1. Data Hiding in Modules
          1. Minimizing from * Damage: _X and _ _all_ _
        2. Enabling Future Language Features
        3. Mixed Usage Modes: _ _name_ _ and _ _main_ _
          1. Unit Tests with _ _name_ _
        4. Changing the Module Search Path
        5. The import as Extension
        6. Relative Import Syntax
          1. Why Relative Imports?
        7. Module Design Concepts
          1. Modules Are Objects: Metaprograms
        8. Module Gotchas
          1. Statement Order Matters in Top-Level Code
          2. Importing Modules by Name String
          3. from Copies Names but Doesn’t Link
          4. from * Can Obscure the Meaning of Variables
          5. reload May Not Impact from Imports
          6. reload, from, and Interactive Testing
          7. reload Isn’t Applied Transitively
          8. Recursive from Imports May Not Work
        9. Chapter Summary
        10. BRAIN BUILDER
          1. Part V Exercises
    9. VI. Classes and OOP
      1. 22. OOP: The Big Picture
        1. Why Use Classes?
        2. OOP from 30,000 Feet
          1. Attribute Inheritance Search
          2. Classes and Instances
          3. Class Method Calls
          4. Coding Class Trees
          5. OOP Is About Code Reuse
        3. Chapter Summary
      2. 23. Class Coding Basics
        1. Classes Generate Multiple Instance Objects
          1. Class Objects Provide Default Behavior
          2. Instance Objects Are Concrete Items
          3. A First Example
        2. Classes Are Customized by Inheritance
          1. A Second Example
          2. Classes Are Attributes in Modules
        3. Classes Can Intercept Python Operators
          1. A Third Example
          2. Why Use Operator Overloading?
        4. The World’s Simplest Python Class
        5. Chapter Summary
      3. 24. Class Coding Details
        1. The class Statement
          1. General Form
          2. Example
        2. Methods
          1. Example
          2. Calling Superclass Constructors
          3. Other Method Call Possibilities
        3. Inheritance
          1. Attribute Tree Construction
          2. Specializing Inherited Methods
          3. Class Interface Techniques
          4. Abstract Superclasses
        4. Operator Overloading
          1. Common Operator Overloading Methods
          2. _ _getitem_ _ Intercepts Index References
          3. _ _getitem_ _ and _ _iter_ _ Implement Iteration
          4. User-Defined Iterators
            1. Multiple iterators on one object
          5. _ _getattr_ _ and _ _setattr_ _ Catch Attribute References
          6. Emulating Privacy for Instance Attributes
          7. _ _repr_ _ and _ _str_ _ Return String Representations
          8. _ _radd_ _ Handles Right-Side Addition
          9. _ _call_ _ Intercepts Calls
          10. Function Interfaces and Callback-Based Code
          11. _ _del_ _ Is a Destructor
        5. Namespaces: The Whole Story
          1. Simple Names: Global Unless Assigned
          2. Attribute Names: Object Namespaces
          3. The “Zen” of Python Namespaces: Assignments Classify Names
          4. Namespace Dictionaries
          5. Namespace Links
        6. A More Realistic Example
        7. Chapter Summary
      4. 25. Designing with Classes
        1. Python and OOP
          1. Overloading by Call Signatures (or Not)
        2. Classes As Records
        3. OOP and Inheritance: “Is-a” Relationships
        4. OOP and Composition: “Has-a” Relationships
          1. Stream Processors Revisited
        5. OOP and Delegation
        6. Multiple Inheritance
        7. Classes Are Objects: Generic Object Factories
          1. Why Factories?
        8. Methods Are Objects: Bound or Unbound
        9. Documentation Strings Revisited
        10. Classes Versus Modules
        11. Chapter Summary
      5. 26. Advanced Class Topics
        1. Extending Built-in Types
          1. Extending Types by Embedding
          2. Extending Types by Subclassing
        2. Pseudoprivate Class Attributes
          1. Name Mangling Overview
          2. Why Use Pseudoprivate Attributes?
        3. New-Style Classes
          1. Diamond Inheritance Change
            1. Diamond inheritance example
            2. Explicit conflict resolution
          2. Other New-Style Class Extensions
            1. Static and class methods
            2. Instance slots
            3. Class properties
            4. New _ _getattribute_ _ overloading method
        4. Static and Class Methods
          1. Using Static and Class Methods
        5. Function Decorators
          1. Decorator Example
        6. Class Gotchas
          1. Changing Class Attributes Can Have Side Effects
          2. Multiple Inheritance: Order Matters
          3. Methods, Classes, and Nested Scopes
          4. “Overwrapping-itis”
        7. Chapter Summary
        8. BRAIN BUILDER
          1. Part VI Exercises
    10. VII. Exceptions and Tools
      1. 27. Exception Basics
        1. Why Use Exceptions?
          1. Exception Roles
        2. Exception Handling: The Short Story
        3. The try/except/else Statement
          1. try Statement Clauses
          2. The try/else Clause
          3. Example: Default Behavior
          4. Example: Catching Built-in Exceptions
        4. The try/finally Statement
          1. Example: Coding Termination Actions with try/finally
        5. Unified try/except/finally
          1. Combining finally and except by Nesting
          2. Unified try Example
        6. The raise Statement
          1. Example: Raising and Catching User-Defined Exceptions
          2. Example: Passing Extra Data with raise
          3. Example: Propagating Exceptions with raise
        7. The assert Statement
          1. Example: Trapping Constraints (but Not Errors)
        8. with/as Context Managers
          1. Basic Usage
          2. The Context Management Protocol
        9. Chapter Summary
      2. 28. Exception Objects
        1. String-Based Exceptions
          1. String Exceptions Are Right Out!
        2. Class-Based Exceptions
          1. Class Exception Example
          2. Why Class Exceptions?
          3. Built-in Exception Classes
          4. Specifying Exception Text
          5. Sending Extra Data and Behavior in Instances
            1. Example: Extra data with classes and strings
        3. General raise Statement Forms
        4. Chapter Summary
      3. 29. Designing with Exceptions
        1. Nesting Exception Handlers
          1. Example: Control-Flow Nesting
          2. Example: Syntactic Nesting
        2. Exception Idioms
          1. Exceptions Aren’t Always Errors
          2. Functions Signal Conditions with raise
          3. Debugging with Outer try Statements
          4. Running In-Process Tests
          5. More on sys.exc_info
        3. Exception Design Tips
          1. What Should Be Wrapped
          2. Catching Too Much: Avoid Empty excepts
          3. Catching Too Little: Use Class-Based Categories
        4. Exception Gotchas
          1. String Exceptions Match by Identity, Not by Value
          2. Catching the Wrong Thing
        5. Core Language Summary
          1. The Python Toolset
          2. Development Tools for Larger Projects
        6. Chapter Summary
        7. BRAIN BUILDER
          1. Part VII Exercises
    11. VIII. Appendixes
      1. A. Installation and Configuration
        1. Installing the Python Interpreter
          1. Is Python Already Present?
          2. Where to Fetch Python
          3. Installation Steps
        2. Configuring Python
          1. Python Environment Variables
          2. How to Set Configuration Options
            1. Unix/Linux shell variables
            2. DOS variables (Windows)
            3. Other Windows options
            4. Path files
      2. B. Solutions to End-of-Part Exercises
        1. Part I, Getting Started
        2. Part II, Types and Operations
        3. Part III, Statements and Syntax
        4. Part IV, Functions
        5. Part V, Modules
        6. Part VI, Classes and OOP
        7. Part VII, Exceptions and Tools
    12. About the Author
    13. Colophon
    14. Copyright

    Product information

    • Title: Learning Python, 3rd Edition
    • Author(s): Mark Lutz
    • Release date: October 2007
    • Publisher(s): O'Reilly Media, Inc.
    • ISBN: 9780596513986