Unformatted text preview:

Learning Python FOURTH EDITION Learning Python Mark Lutz Beijing Cambridge Farnham K ln Sebastopol Taipei Tokyo Learning Python Fourth Edition by Mark Lutz Copyright 2009 Mark Lutz All rights reserved Printed in the United States of America Published by O Reilly Media Inc 1005 Gravenstein Highway North Sebastopol CA 95472 O Reilly books may be purchased for educational business or sales promotional use Online editions are also available for most titles http my safaribooksonline com For more information contact our corporate institutional sales department 800 998 9938 or corporate oreilly com Julie Steele Editor Production Editor Sumita Mukherji Copyeditor Rachel Head Production Services Newgen North America John Bickelhaupt Indexer Cover Designer Karen Montgomery Interior Designer David Futato Illustrator Robert Romano Printing History March 1999 December 2003 October 2007 September 2009 First Edition Second Edition Third Edition Fourth Edition Nutshell Handbook the Nutshell Handbook logo and the O Reilly logo are registered trademarks of O Reilly Media Inc Learning Python the image of a wood rat and related trade dress are trademarks of O Reilly Media 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 Media 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 author assume no responsibility for errors or omissions or for damages resulting from the use of the information con tained herein ISBN 978 0 596 15806 4 M 1252944666 To Vera You are my life Table of Contents Preface xxxi Part I Getting Started Why Do People Use Python Software Quality Developer Productivity Is Python a Scripting Language OK but What s the Downside Who Uses Python Today What Can I Do with Python Systems Programming GUIs Internet Scripting Component Integration Database Programming Rapid Prototyping Numeric and Scientific Programming Gaming Images Serial Ports XML Robots and More 1 A Python Q A Session 3 3 4 5 5 7 7 9 9 9 10 10 11 11 11 12 12 13 13 13 14 15 16 16 17 17 17 It s Object Oriented It s Free It s Portable It s Powerful It s Mixable It s Easy to Use It s Easy to Learn It s Named After Monty Python How Is Python Supported What Are Python s Technical Strengths How Does Python Stack Up to Language X vii Chapter Summary Test Your Knowledge Quiz Test Your Knowledge Answers 18 19 19 Execution Model Variations The Programmer s View Python s View Introducing the Python Interpreter Program Execution 2 How Python Runs Programs 23 23 24 24 26 29 29 30 32 33 33 34 34 34 Python Implementation Alternatives Execution Optimization Tools Frozen Binaries Other Execution Options Future Possibilities Chapter Summary Test Your Knowledge Quiz Test Your Knowledge Answers Clicking File Icons The Interactive Prompt System Command Lines and Files Running Code Interactively Why the Interactive Prompt Using the Interactive Prompt A First Script Running Files with Command Lines Using Command Lines and Files Unix Executable Scripts 3 How You Run Programs 35 35 37 38 39 41 42 43 44 46 47 47 49 50 51 53 56 57 58 58 60 62 63 64 Clicking Icons on Windows The input Trick Other Icon Click Limitations The Grander Module Story Attributes import and reload Usage Notes IDLE Basics Using IDLE Advanced IDLE Tools Using exec to Run Module Files The IDLE User Interface Other IDEs Other Launch Options Module Imports and Reloads viii Table of Contents Embedding Calls Frozen Binary Executables Text Editor Launch Options Still Other Launch Options Future Possibilities Which Option Should I Use Chapter Summary Test Your Knowledge Quiz Test Your Knowledge Answers Test Your Knowledge Part I Exercises Part II Types and Operations 4 64 65 65 66 66 66 68 68 69 70 Lists Numbers Strings Python s Core Data Types Sequence Operations Immutability Type Specific Methods Getting Help Other Ways to Code Strings Pattern Matching Introducing Python Object Types 75 76 Why Use Built in Types 77 78 80 80 82 82 84 85 85 86 86 87 87 88 88 90 90 91 93 94 95 96 97 97 99 99 100 Mapping Operations Nesting Revisited Sorting Keys for Loops Iteration and Optimization Missing Keys if Tests Sequence Operations Type Specific Operations Bounds Checking Nesting Comprehensions How to Break Your Code s Flexibility Other File Like Tools Other Core Types Why Tuples Dictionaries Tuples Files Table of Contents ix User Defined Classes And Everything Else Chapter Summary Test Your Knowledge Quiz Test Your Knowledge Answers 101 102 103 103 104 Numbers in Action Numeric Type Basics Numeric Literals Built in Numeric Tools Python Expression Operators Variables and Basic Expressions Numeric Display Formats Comparisons Normal and Chained Division Classic Floor and True Integer Precision Complex Numbers Hexadecimal Octal and Binary Notation Bitwise Operations Other Built in Numeric Tools 5 Numeric Types 105 105 106 108 108 113 113 115 116 117 121 122 122 124 125 127 127 129 133 139 140 141 141 141 Numeric Extensions Chapter Summary Test Your Knowledge Quiz Test Your Knowledge Answers Decimal Type Fraction Type Sets Booleans Other Numeric Types Shared References The Case of the Missing Declaration Statements Variables Objects and References Types Live with Objects Not Variables Objects Are Garbage Collected 6 The Dynamic Typing Interlude 143 143 144 145 146 148 149 151 152 153 153 154 Dynamic Typing Is Everywhere Chapter Summary Test Your Knowledge Quiz Test Your Knowledge Answers Shared References and In Place Changes Shared References and Equality x Table of Contents String Literals String Methods Strings in Action Basic Operations Indexing and Slicing String Conversion Tools Changing Strings String Method Examples Changing Strings String Method Examples Parsing Text Other Common String Methods in Action The Original string Module Gone in 3 0 Single and Double Quoted Strings Are the Same Escape Sequences Represent Special Bytes Raw Strings Suppress Escapes Triple Quotes Code Multiline Block Strings 7 Strings 155 157 158 158 161 162 163 164 165 169 171 172 174 176 177 178 179 181 182 183 184 184 185 187 190 193 194 194 195 195 196 The Basics Adding Keys Attributes and Offsets Adding Specific Formatting Comparison to the Formatting Expression Why the New Format Method Advanced String Formatting Expressions Dictionary Based String Formatting


View Full Document

APPALACHIAN BIO 5500 - Learning Python

Download Learning Python
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Learning Python and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Learning Python and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?