Transcription of Learning Python - UPV/EHU
1 Learning PythonFOURTH EDITIONL earning PythonMark LutzBeijing Cambridge Farnham K ln Sebastopol Taipei TokyoLearning Python , Fourth Editionby Mark LutzCopyright 2009 Mark Lutz. All rights in the United States of by O Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA Reilly books may be purchased for educational, business, or sales promotional use. Online editionsare also available for most titles ( ). For more information, contact ourcorporate/institutional sales department: (800) 998-9938 or SteeleProduction Editor:Sumita MukherjiCopyeditor:Rachel HeadProduction Services:Newgen North AmericaIndexer:John BickelhauptCover Designer:Karen MontgomeryInterior Designer:David FutatoIllustrator:Robert RomanoPrinting History:March 1999:First Edition.
2 December 2003:Second Edition. October 2007:Third Edition. September 2009: Fourth Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O Reilly logo are registered trademarks ofO Reilly Media, Inc. Learning Python , the image of a wood rat, and related trade dress are trademarksof O Reilly Media, of the designations used by manufacturers and sellers to distinguish their products are claimed astrademarks. Where those designations appear in this book, and O Reilly Media, Inc., was aware of atrademark claim, the designations have been printed in caps or initial every precaution has been taken in the preparation of this book, the publisher and author assumeno responsibility for errors or omissions, or for damages resulting from the use of the information con-tained : 978-0-596-15806-4[M]1252944666To are my of ContentsPreface.
3 XxxiPart I. Getting Started Python Q&A Session .. 3 Why Do People Use Python ?3 Software Quality4 Developer Productivity5Is Python a Scripting Language ?5OK, but What s the Downside?7 Who Uses Python Today?7 What Can I Do with Python ?9 Systems Programming9 GUIs9 Internet Scripting10 Component Integration10 Database Programming11 Rapid Prototyping11 Numeric and Scientific Programming11 Gaming, Images, Serial Ports, XML, Robots, and More12 How Is Python Supported?12 What Are Python s Technical Strengths?13It s Object-Oriented13It s Free13It s Portable14It s Powerful15It s Mixable16It s Easy to Use16It s Easy to Learn17It s Named After Monty Python17 How Does Python Stack Up to Language X?
4 17viiChapter Summary18 Test Your Knowledge: Quiz19 Test Your Knowledge: Python Runs Programs .. 23 Introducing the Python Interpreter23 Program Execution24 The Programmer s View24 Python s View26 Execution Model Variations29 Python Implementation Alternatives29 Execution Optimization Tools30 Frozen Binaries32 Other Execution Options33 Future Possibilities?33 Chapter Summary34 Test Your Knowledge: Quiz34 Test Your Knowledge: You Run Programs .. 35 The Interactive Prompt35 Running Code Interactively37 Why the Interactive Prompt?38 Using the Interactive Prompt39 System Command Lines and Files41A First Script42 Running Files with Command Lines43 Using Command Lines and Files44 Unix Executable Scripts (#!)
5 46 Clicking File Icons47 Clicking Icons on Windows47 The input Trick49 Other Icon-Click Limitations50 Module Imports and Reloads51 The Grander Module Story: Attributes53import and reload Usage Notes56 Using exec to Run Module Files57 The IDLE User Interface58 IDLE Basics58 Using IDLE60 Advanced IDLE Tools62 Other IDEs63 Other Launch Options64viii|Table of ContentsEmbedding Calls64 Frozen Binary Executables65 Text Editor Launch Options65 Still Other Launch Options66 Future Possibilities?66 Which Option Should I Use?66 Chapter Summary68 Test Your Knowledge: Quiz68 Test Your Knowledge: Answers69 Test Your Knowledge: Part I Exercises70 Part II.
6 Types and Operations Python Object Types .. 75 Why Use Built-in Types?76 Python s Core Data Types77 Numbers78 Strings80 Sequence Operations80 Immutability82 Type-Specific Methods82 Getting Help84 Other Ways to Code Strings85 Pattern Matching85 Lists86 Sequence Operations86 Type-Specific Operations87 Bounds Checking87 Nesting88 Comprehensions88 Dictionaries90 Mapping Operations90 Nesting Revisited91 Sorting Keys: for Loops93 Iteration and Optimization94 Missing Keys: if Tests95 Tuples96 Why Tuples?97 Files97 Other File-Like Tools99 Other Core Types99 How to Break Your Code s Flexibility100 Table of Contents|ixUser-Defined Classes101 And Everything Else102 Chapter Summary103 Test Your Knowledge: Quiz103 Test Your Knowledge: Types.
7 105 Numeric Type Basics105 Numeric Literals106 Built-in Numeric Tools108 Python Expression Operators108 Numbers in Action113 Variables and Basic Expressions113 Numeric Display Formats115 Comparisons: Normal and Chained116 Division: Classic, Floor, and True117 Integer Precision121 Complex Numbers122 Hexadecimal, Octal, and Binary Notation122 Bitwise Operations124 Other Built-in Numeric Tools125 Other Numeric Types127 Decimal Type127 Fraction Type129 Sets133 Booleans139 Numeric Extensions140 Chapter Summary141 Test Your Knowledge: Quiz141 Test Your Knowledge: Dynamic Typing Interlude.
8 143 The Case of the Missing Declaration Statements143 Variables, Objects, and References144 Types Live with Objects, Not Variables145 Objects Are Garbage-Collected146 Shared References148 Shared References and In-Place Changes149 Shared References and Equality151 Dynamic Typing Is Everywhere152 Chapter Summary153 Test Your Knowledge: Quiz153 Test Your Knowledge: Answers154x|Table of .. 155 String Literals157 Single- and Double-Quoted Strings Are the Same158 Escape Sequences Represent Special Bytes158 Raw Strings Suppress Escapes161 Triple Quotes Code Multiline Block Strings162 Strings in Action163 Basic Operations164 Indexing and Slicing165 String Conversion Tools169 Changing Strings171 String Methods172 String Method Examples: Changing Strings174 String Method Examples.
9 Parsing Text176 Other Common String Methods in Action177 The Original string Module (Gone in )178 String Formatting Expressions179 Advanced String Formatting Expressions181 Dictionary-Based String Formatting Expressions182 String Formatting Method Calls183 The Basics184 Adding Keys, Attributes, and Offsets184 Adding Specific Formatting185 Comparison to the % Formatting Expression187 Why the New Format Method?190 General Type Categories193 Types Share Operation Sets by Categories194 Mutable Types Can Be Changed In-Place194 Chapter Summary195 Test Your Knowledge: Quiz195 Test Your Knowledge: and Dictionaries.
10 197 Lists197 Lists in Action200 Basic List Operations200 List Iteration and Comprehensions200 Indexing, Slicing, and Matrixes201 Changing Lists In-Place202 Dictionaries207 Dictionaries in Action209 Basic Dictionary Operations209 Changing Dictionaries In-Place210 Table of Contents|xiMore Dictionary Methods211A Languages Table212 Dictionary Usage Notes213 Other Ways to Make Dictionaries216 Dictionary Changes in Python Summary223 Test Your Knowledge: Quiz224 Test Your Knowledge: , Files, and Everything Else .. 225 Tuples225 Tuples in Action227 Why Lists and Tuples?229 Files229 Opening Files230 Using Files231 Files in Action232 Other File Tools238 Type Categories Revisited239 Object Flexibility241 References Versus Copies241 Comparisons, Equality, and Truth244 Python Dictionary Comparisons246 The Meaning of True and False in Python246 Python s Type Hierarchies248 Type Objects249 Other Types in Python250 Built-in Type Gotchas251 Assignment Creates References, Not Copies251 Repetition Adds One Level Deep252 Beware of Cyclic Data Structures252 Immutable Types Can t Be Changed In-Place253 Chapter Summary253 Test Your Knowledge.