You are previewing Python Power!: The Comprehensive Guide.
O'Reilly logo
Python Power!: The Comprehensive Guide

Book Description

Python, a powerful and flexible general purpose programming language is growing in popularity as more people become interested in programming using open source languages. "Python Power!: The Comprehensive Guide" shows readers how to use Python to run Web sites, write GUI interfaces, control number-crunching code, process large XML data sets, build test suites for C or Java code, and much more. Technical topics are explained clearly and concisely and end-of-chapter projects encourage the reader to program on their own. A detailed table of contents and a complete, well-organized index make this book a great reference tool. All code used in the book will be available for download on the companion website.

Table of Contents

  1. Copyright
    1. Dedication
  2. Acknowledgments
  3. About the Author
  4. Introduction
    1. What You’ll Find in This Book
    2. Who This Book Is For
    3. How This Book Is Organized
  5. 1. About Python
    1. What is Python?
    2. A Brief History of Python
    3. Interpreters → Versus Compilers
      1. When to Use (or Not Use) an Interpreted Language
        1. Disadvantages
        2. Advantages
      2. Understanding Bytecodes
    4. Why Use Python?
      1. Object-Oriented
      2. Cross Platform
      3. Broad User Base
      4. Well Supported in Third-Party Tools
      5. Good Selection of Tools Available
      6. Good Selection of Pre-built Libraries
    5. Where Is Python Used?
    6. How Is Python Licensed?
    7. Where Do I Get Python?
    8. Installing Python
    9. Getting Information on Python
    10. Python Communities
    11. Other Software
    12. And Now for Something Completely Different...
  6. 2. Python Language Overview
    1. Python Syntax
      1. Comments
      2. Indentation
    2. Python Reserved Words
      1. Decision Making and Iteration Keywords
      2. Debugging Keywords
      3. Package and Module Handling Keywords
      4. Exception Handling Keywords
      5. General Language Keywords
      6. Other Keywords
    3. Variable Usage
      1. The Continuation Variable
      2. Watching Out for Spelling Mistakes!
    4. Predicates
    5. Identifier Scope
    6. Operators
      1. Modulo Operator
      2. Exponential Operator
      3. Logical Operators
      4. Comparative Operators
      5. Bitwise Operators
      6. Membership Operators and String Operators
      7. Identity Operators
    7. In Conclusion
  7. 3. Tools
    1. IDLE
      1. File Menu
        1. New Window
        2. Open
        3. Recent Files
        4. Open Module
        5. Class Browser
        6. Path Browser
      2. The Path Browser Dialog
        1. Save
        2. Save As
        3. Save Copy As
        4. Print Window
        5. Close
        6. Exit
      3. Edit Menu
        1. Undo and Redo
        2. Cut, Copy, and Paste
        3. Select All
        4. Find, Find Next, Find Selection, Find in Files, Replace
        5. Go to Line
        6. Show Call Tips
        7. Show Completions
        8. Show Surrounding Parens
        9. Expand Word
      4. Shell Menu
        1. View Last Restart
        2. Restart Shell
      5. Debug Menu
        1. Go to File/Line
        2. Debugger
        3. Stack Viewer
        4. Auto-Open Stack Viewer
        5. Configure IDLE
      6. The Edit Window
      7. Format Menu
        1. Indent Region
        2. Dedent Region
        3. Comment Out Region
        4. Uncomment Region
        5. Tabify Region
        6. Untabify Region
        7. Toggle Tabs
        8. New Indent Width
        9. Format Paragraph
        10. Python Shell
        11. Check Module
        12. Run Module
      8. Command Line Compiler
      9. Creating Python Files
      10. Documentation
    2. In Conclusion
  8. 4. Data Types
    1. Numeric Types
      1. Integers
      2. Demonstrating Long Integers
      3. Octal and Hexadecimal
    2. Floating Point Numbers
    3. Strings
      1. String Variables
        1. Enclosed in Quotes
        2. Spanning Multiple Lines
        3. Triple Quotes
      2. Concatenating Strings
      3. Repeating Strings
      4. Substrings
      5. Slicing
      6. String Functions
      7. String Constants
      8. Conversion Functions
      9. Search Functions
      10. Formatting Functions
      11. Escape Sequences
    4. Sequences
      1. Lists
        1. Adding, Removing and Inserting with Lists
      2. Shared References
      3. Tuples
      4. Dictionaries
        1. Creating a Dictionary
        2. Adding to a Dictionary
        3. Retrieving Values for Keys
        4. Iterating Over Keys
        5. Removing Keys from a Dictionary
    5. Advanced Type
      1. Classes and Objects
      2. Complex Type
      3. Generator Type
      4. None Type
      5. Unicode Type
    6. In Conclusion
  9. 5. 5 Control Flow
    1. Conditionals
      1. The if Statement
      2. The elif Statement
      3. The else Statement
      4. Wrapping Up the Conditionals: A Cool Example
    2. Loops
      1. The for Loop
      2. The while Loop
    3. In Conclusion
  10. 6. Input and Output
    1. User Input
      1. The input Function
      2. The raw_input Function
    2. User Output
      1. Formatting
    3. File Input
    4. File Output
    5. Closing Files
    6. Positioning in Files
    7. Directories and Files
    8. The stat Module: File Statistics
    9. Command Line Arguments
    10. Pickle
    11. In Conclusion
  11. 7. Functions and Modules
    1. What Is a Function?
    2. Defining Functions in Python
    3. What Are Arguments?
    4. How Do You Pass an Argument to a Function?
    5. Default Arguments
    6. Variable Default Arguments
    7. Keyword Arguments
    8. Returning Values from Functions
    9. Returning Multiple Values from Functions
    10. Recursive Functions
    11. Passing Functions as Arguments
    12. Lambda Functions
    13. Variable Numbers of Arguments to a Function
    14. Variable Scope in Functions
    15. Using Modules
    16. In Conclusion
  12. 8. Exception Handling
    1. Looking at Exceptions in Python
    2. Traceback Example
    3. Understanding Tracebacks
    4. Exceptions
      1. Catching Exceptions with try..except
      2. Multiple except Clauses
      3. Blank except Clauses
    5. The else Clauses
    6. The finally Clause
    7. Raising Your Own Exceptions
    8. Exception Arguments
    9. User-Defined Exceptions
    10. Working with the Exception Information
      1. exc_type
      2. exc_value
    11. Using the with Clause for Files
    12. Re-throwing Exceptions
    13. In Conclusion
  13. 9. Object-Oriented Programming
    1. A Brief History of OOP
    2. What Is an Object?
    3. Why Do We Use Objects?
      1. Reuse
      2. Ease in Debugging
      3. Maintainability
    4. The Attributes of Object-Oriented Development
      1. Abstraction
      2. Data Hiding
      3. Inheritance
      4. Polymorphism
    5. Terminology
      1. Class
      2. Object
      3. Attribute
      4. Method
      5. Message Passing
      6. Event Handling
      7. Derivation
      8. Coupling
      9. Cohesion
      10. Constants
    6. Other Concepts
    7. In Conclusion
  14. 10. Classes and Objects in Python
    1. Python Classes
    2. Properties
    3. Attribute Modifying Functions
    4. Private Attributes
    5. Doc Strings
    6. Properties
    7. The self Object
    8. Methods
    9. Special Methods
      1. Initialization
      2. Termination
      3. String Conversion
    10. Inheritance
    11. Multiple Inheritance
    12. Using super
    13. Polymorphism
    14. Exception Classes
    15. Iterators
    16. Operator Overloading
    17. In Conclusion
  15. 11. The Python Library
    1. Containers
      1. Working with the deque Class
    2. Math
    3. Complex Math
    4. Types
    5. Strings
    6. Regular Expressions
      1. Patterns
      2. Special Sequence Characters
      3. Compiling Regular Expressions
      4. Matching Strings
      5. Meta Characters
      6. Grouping
    7. System
    8. Random Number Generation
    9. Dates and Times
      1. Creating a New Time
      2. Time Operations
      3. Creating a New Date
      4. Date Operations
      5. Time Zone Information
    10. Operating System Interface
      1. System Information
      2. Process Management
    11. In Conclusion
  16. 12. The GUI–TkInter
    1. What Is TkInter?
    2. Terms and Conditions
      1. Event Handling
      2. Callbacks
      3. Widgets
      4. Layout Managers
    3. Working with TkInter
    4. Creating a Label
    5. Frame Widgets and Centering
    6. An Application with a Button
    7. Working with Entry Fields and Grid Layouts
    8. Creating a Class to Handle User Interfaces
    9. Working with List Boxes
      1. Scrolling a List Box
    10. Menus
    11. Context Menus
    12. Scale Widgets
    13. RadioButtons and CheckButton
    14. Text Widgets
    15. In Conclusion
  17. 13. The Web Server—Apache
    1. Setting Up Apache
    2. Testing Apache
    3. Your First Python CGI Script: Hello Apache
      1. Examining the Hello Python Script
      2. The cgi-bin Directory
    4. A Script for Displaying the Environment
    5. Receiving Data from an HTML File
    6. Sending Data to an HTML File
      1. How It All Works
    7. Dynamic HTML Displays Based on User Input
    8. HTML Elements
    9. Cookies
    10. Uploading Files
    11. Redirection
    12. Error Handling
    13. In Conclusion
  18. 14. Working with Databases
    1. What Is a Database?
      1. Simple Database Terminology
    2. What Is MySQL?
    3. Downloading and Installing
    4. Creating a New Database
    5. Creating a New User
    6. Opening an Existing Database
    7. Writing to a Database
    8. Reading from a Database
    9. Updating a Database
    10. Deleting from a Database
    11. Searching a Database
    12. In Conclusion
  19. 15. Putting It All Together
    1. Designing the Application
      1. Program Flow
      2. User Interface Design
      3. Database Design
    2. Implementing the Database Tables
    3. Implementing the Forms
    4. Adding Reviews
    5. Adding the Review to the Database
    6. Listing the Reviews
    7. Deleting Books
    8. In Conclusion
  20. 16. Python and Graphics
    1. The PIL Library
      1. Downloading
      2. Installing
      3. Verifying Your Installation
    2. Creating a New Image
      1. Function Parameters
    3. Drawing on the Image
      1. Drawing the Image
      2. Displaying the Image
      3. Saving the Image
      4. Loading an Existing Image
    4. Displaying Text
    5. Identifying an Image
    6. Rotating an Image
    7. Postscript Printing
    8. Creating Thumbnails
    9. In Conclusion