Learning Python : Includes index
معرفی کتاب «Learning Python : Includes index» نوشتهٔ Mark Lutz; Safari, an O’Reilly Media Company، منتشرشده توسط نشر O'Reilly Media در سال 2008. این کتاب در فرمت pdf، زبان انگلیسی ارائه شده است. «Learning Python : Includes index» در دستهٔ بدون دستهبندی قرار دارد.
Portable, powerful, and a breeze to use, Python is ideal for both standalone programs and scripting applications. With this hands-on book, you can master the fundamentals of the core Python language quickly and efficiently, whether you're new to programming or just new to Python. Once you finish, you will know enough about the language to use it in any application domain you choose. Learning Python is based on material from author Mark Lutz's popular training courses, which he's taught over the past decade. Each chapter is a self-contained lesson that helps you thoroughly understand a key component of Python before you continue. Along with plenty of annotated examples, illustrations, and chapter summaries, every chapter also contains Brain Builder , a unique section with practical exercises and review quizzes that let you practice new skills and test your understanding as you go. This book covers: Types and Operations -- Python's major built-in object types in depth: numbers, lists, dictionaries, and more Statements and Syntax -- the code you type to create and process objects in Python, along with Python's general syntax model Functions -- Python's basic procedural tool for structuring and reusing code Modules -- packages of statements, functions, and other tools organized into larger components Classes and OOP -- Python's optional object-oriented programming tool for structuring code for customization and reuse Exceptions and Tools -- exception handling model and statements, plus a look at development tools for writing larger programs Learning Python gives you a deep and complete understanding of the language that will help you comprehend any application-level examples of Python that you later encounter. If you're ready to discover what Google and YouTube see in Python, this book is the best way to get started. Read more... Abstract: Portable, powerful, and a breeze to use, Python is ideal for both standalone programs and scripting applications. With this hands-on book, you can master the fundamentals of the core Python language quickly and efficiently, whether you're new to programming or just new to Python. Once you finish, you will know enough about the language to use it in any application domain you choose. Learning Python is based on material from author Mark Lutz's popular training courses, which he's taught over the past decade. Each chapter is a self-contained lesson that helps you thoroughly understand a key component of Python before you continue. Along with plenty of annotated examples, illustrations, and chapter summaries, every chapter also contains Brain Builder , a unique section with practical exercises and review quizzes that let you practice new skills and test your understanding as you go. This book covers: Types and Operations -- Python's major built-in object types in depth: numbers, lists, dictionaries, and more Statements and Syntax -- the code you type to create and process objects in Python, along with Python's general syntax model Functions -- Python's basic procedural tool for structuring and reusing code Modules -- packages of statements, functions, and other tools organized into larger components Classes and OOP -- Python's optional object-oriented programming tool for structuring code for customization and reuse Exceptions and Tools -- exception handling model and statements, plus a look at development tools for writing larger programs Learning Python gives you a deep and complete understanding of the language that will help you comprehend any application-level examples of Python that you later encounter. If you're ready to discover what Google and YouTube see in Python, this book is the best way to get started Learning Python, Third Edition -1 Table of Contents 5 Preface 27 About This Third Edition 27 This Edition’s Python Language Changes 27 This Edition’s Python Training Changes 28 This Edition’s Structural Changes 29 This Edition’s Scope Changes 30 About This Book 30 This Book’s Prerequisites 30 This Book’s Scope and Other Books 31 This Book’s Style and Structure 32 Book Updates 34 About the Programs in This Book 34 Preparing for Python 3.0 35 About This Series 38 Using Code Examples 38 Font Conventions 39 Safari® Books Online 40 How to Contact Us 40 Acknowledgments 40 Part I 43 A Python Q&A Session 45 Why Do People Use Python? 45 Software Quality 47 Developer Productivity 47 Is Python a “Scripting Language”? 48 OK, but What’s the Downside? 49 Who Uses Python Today? 50 What Can I Do with Python? 51 Systems Programming 51 GUIs 51 Internet Scripting 52 Component Integration 52 Database Programming 53 Rapid Prototyping 53 Numeric and Scientific Programming 53 Gaming, Images, AI, XML, Robots, and More 54 What Are Python’s Technical Strengths? 54 It’s Object Oriented 54 It’s Free 55 It’s Portable 55 It’s Powerful 56 It’s Mixable 57 It’s Easy to Use 57 It’s Easy to Learn 59 It’s Named After Monty Python 59 How Does Python Stack Up to Language X? 60 Chapter Summary 61 Quiz Answers 62 How Python Runs Programs 64 Introducing the Python Interpreter 64 Program Execution 66 The Programmer’s View 66 Python’s View 67 Byte code compilation 67 The Python Virtual Machine (PVM) 68 Performance implications 68 Development implications 69 Execution Model Variations 69 Python Implementation Alternatives 70 CPython 70 Jython 70 IronPython 71 Execution Optimization Tools 71 The Psyco just-in-time compiler 71 The Shedskin C++ translator 72 Frozen Binaries 73 Future Possibilities? 74 Chapter Summary 74 Quiz Answers 75 How You Run Programs 76 Interactive Coding 76 Using the Interactive Prompt 79 System Command Lines and Files 79 Using Command Lines and Files 82 Unix Executable Scripts (#!) 83 Clicking File Icons 84 Clicking Icons on Windows 84 The raw_input Trick 86 Other Icon-Click Limitations 87 Module Imports and Reloads 87 The Grander Module Story: Attributes 89 Modules and namespaces 91 import and reload Usage Notes 91 The IDLE User Interface 92 IDLE Basics 93 Using IDLE 94 Advanced IDLE Tools 96 Other IDEs 96 Embedding Calls 98 Frozen Binary Executables 98 Text Editor Launch Options 99 Other Launch Options 99 Future Possibilities? 99 Which Option Should I Use? 100 Chapter Summary 100 Quiz Answers 101 Part II 105 Introducing Python Object Types 107 Why Use Built-in Types? 108 Python’s Core Data Types 109 Numbers 110 Strings 111 Sequence Operations 112 Immutability 113 Type-Specific Methods 114 Getting Help 115 Other Ways to Code Strings 116 Pattern Matching 117 Lists 117 Sequence Operations 118 Type-Specific Operations 118 Bounds Checking 119 Nesting 119 List Comprehensions 120 Dictionaries 121 Mapping Operations 121 Nesting Revisited 122 Sorting Keys: for Loops 123 Iteration and Optimization 125 Missing Keys: if Tests 126 Tuples 127 Why Tuples? 127 Files 127 Other File-Like Tools 128 Other Core Types 129 How to Break Your Code’s Flexibility 130 User-Defined Classes 130 And Everything Else 131 Chapter Summary 132 Quiz Answers 133 Numbers 135 Python Numeric Types 135 Numeric Literals 136 Built-in Numeric Tools and Extensions 137 Python Expression Operators 138 Mixed Operators Follow Operator Precedence 139 Parentheses Group Subexpressions 139 Mixed Types Are Converted Up 139 Preview: Operator Overloading 140 Numbers in Action 141 Variables and Basic Expressions 141 Numeric Display Formats 142 Division: Classic, Floor, and True 144 Bitwise Operations 145 Long Integers 145 Complex Numbers 146 Hexadecimal and Octal Notation 147 Other Built-in Numeric Tools 148 Other Numeric Types 149 Decimal Numbers 149 Sets 150 Booleans 151 Third-Party Extensions 152 Chapter Summary 152 Quiz Answers 153 The Dynamic Typing Interlude 154 The Case of the Missing Declaration Statements 154 Variables, Objects, and References 154 Types Live with Objects, Not Variables 156 Objects Are Garbage-Collected 157 Shared References 158 Shared References and In-Place Changes 160 Shared References and Equality 161 Dynamic Typing Is Everywhere 163 Chapter Summary 163 Quiz Answers 164 Strings 165 String Literals 166 Single- and Double-Quoted Strings Are the Same 167 Escape Sequences Represent Special Bytes 167 Raw Strings Suppress Escapes 169 Triple Quotes Code Multiline Block Strings 171 Unicode Strings Encode Larger Character Sets 172 Strings in Action 174 Basic Operations 174 Indexing and Slicing 175 Extended slicing: the third limit 177 String Conversion Tools 178 Character code conversions 180 Changing Strings 181 String Formatting 182 Advanced String Formatting 183 Dictionary-Based String Formatting 184 String Methods 185 String Method Examples: Changing Strings 186 String Method Examples: Parsing Text 188 Other Common String Methods in Action 189 The Original string Module 190 General Type Categories 191 Types Share Operation Sets by Categories 191 Mutable Types Can Be Changed In-Place 192 Chapter Summary 192 Quiz Answers 193 Lists and Dictionaries 194 Lists 194 Lists in Action 196 Basic List Operations 196 Indexing, Slicing, and Matrixes 197 Changing Lists In-Place 198 Index and slice assignments 198 List method calls 199 Other common list operations 201 Dictionaries 202 Dictionaries in Action 203 Basic Dictionary Operations 204 Changing Dictionaries In-Place 205 More Dictionary Methods 205 A Languages Table 207 Dictionary Usage Notes 208 Using dictionaries to simulate flexible lists 208 Using dictionaries for sparse data structures 209 Avoiding missing-key errors 209 Using dictionaries as “records” 210 Other ways to make dictionaries 211 Chapter Summary 212 Quiz Answers 213 Tuples, Files, and Everything Else 214 Tuples 214 Tuples in Action 215 Tuple syntax peculiarities: commas and parentheses 216 Conversions and immutability 216 Why Lists and Tuples? 217 Files 218 Opening Files 218 Using Files 219 Files in Action 220 Storing and parsing Python objects in files 220 Storing native Python objects with pickle 222 Storing and parsing packed binary data in files 223 Other File Tools 224 Type Categories Revisited 224 Object Flexibility 225 References Versus Copies 226 Comparisons, Equality, and Truth 228 The Meaning of True and False in Python 230 Python’s Type Hierarchies 231 Other Types in Python 233 Built-in Type Gotchas 233 Assignment Creates References, Not Copies 233 Repetition Adds One Level Deep 234 Beware of Cyclic Data Structures 235 Immutable Types Can’t Be Changed In-Place 235 Chapter Summary 235 Quiz Answers 237 Part III 241 Introducing Python Statements 243 Python Program Structure Revisited 243 Python’s Statements 244 A Tale of Two ifs 245 What Python Adds 246 What Python Removes 246 Parentheses are optional 246 End of line is end of statement 246 End of indentation is end of block 247 Why Indentation Syntax? 248 A Few Special Cases 250 Statement rule special cases 250 Block rule special case 251 A Quick Example: Interactive Loops 252 A Simple Interactive Loop 252 Doing Math on User Inputs 253 Handling Errors by Testing Inputs 254 Handling Errors with try Statements 255 Nesting Code Three Levels Deep 256 Chapter Summary 257 Quiz Answers 258 Assignment, Expressions, and print 259 Assignment Statements 259 Assignment Statement Forms 260 Sequence Assignments 261 Advanced sequence assignment patterns 262 Multiple-Target Assignments 264 Multiple-target assignment and shared references 264 Augmented Assignments 265 Augmented assignment and shared references 267 Variable Name Rules 267 Naming conventions 269 Names have no type, but objects do 269 Expression Statements 270 Expression Statements and In-Place Changes 271 print Statements 271 The Python “Hello World” Program 272 Redirecting the Output Stream 273 The print >> file Extension 274 Chapter Summary 276 Quiz Answers 277 if Tests 278 if Statements 278 General Format 278 Basic Examples 279 Multiway Branching 279 Python Syntax Rules 281 Block Delimiters 282 Statement Delimiters 283 A Few Special Cases 284 Truth Tests 285 The if/else Ternary Expression 286 Chapter Summary 288 Quiz Answers 289 while and for Loops 290 while Loops 290 General Format 291 Examples 291 break, continue, pass, and the Loop else 292 General Loop Format 292 Examples 293 pass 293 continue 293 break 294 else 294 More on the loop else clause 295 for Loops 296 General Format 296 Examples 298 Basic usage 298 Other data types 298 Tuple assignment in for 299 Nested for loops 299 Iterators: A First Look 300 File Iterators 302 Other Built-in Type Iterators 304 Other Iteration Contexts 305 User-Defined Iterators 306 Loop Coding Techniques 307 Counter Loops: while and range 307 Nonexhaustive Traversals: range 308 Changing Lists: range 309 Parallel Traversals: zip and map 310 Dictionary construction with zip 312 Generating Both Offsets and Items: enumerate 313 List Comprehensions: A First Look 314 List Comprehension Basics 314 Using List Comprehensions on Files 315 Extended List Comprehension Syntax 316 Chapter Summary 317 Quiz Answers 318 The Documentation Interlude 320 Python Documentation Sources 320 # Comments 321 The dir Function 321 Docstrings: __doc__ 322 User-defined docstrings 323 Docstring standards 324 Built-in docstrings 324 PyDoc: The help Function 325 PyDoc: HTML Reports 327 Standard Manual Set 331 Web Resources 331 Published Books 332 Common Coding Gotchas 333 Chapter Summary 335 Quiz Answers 336 Part IV 339 Function Basics 341 Why Use Functions? 342 Coding Functions 342 def Statements 344 def Executes at Runtime 345 A First Example: Definitions and Calls 345 Definition 346 Calls 346 Polymorphism in Python 347 A Second Example: Intersecting Sequences 348 Definition 348 Calls 348 Polymorphism Revisited 349 Local Variables 350 Chapter Summary 350 Quiz Answers 351 Scopes and Arguments 352 Scope Rules 352 Python Scope Basics 353 Name Resolution: The LEGB Rule 354 Scope Example 356 The Built-in Scope 356 The global Statement 358 Minimize Global Variables 359 Minimize Cross-File Changes 360 Other Ways to Access Globals 361 Scopes and Nested Functions 362 Nested Scope Details 362 Nested Scope Examples 363 Factory functions 363 Retaining enclosing scopes’ state with defaults 365 Nested scopes and lambdas 366 Scopes versus defaults with loop variables 366 Arbitrary scope nesting 368 Passing Arguments 368 Arguments and Shared References 369 Avoiding Mutable Argument Changes 371 Simulating Output Parameters 371 Special Argument-Matching Modes 372 Keyword and Default Examples 374 Keywords 374 Defaults 375 Arbitrary Arguments Examples 375 Collecting arguments 376 Unpacking arguments 376 Combining Keywords and Defaults 377 The min Wakeup Call 378 Full credit 378 Bonus points 379 The punch line 380 A More Useful Example: General Set Functions 380 Argument Matching: The Gritty Details 381 Chapter Summary 382 Quiz Answers 385 Advanced Function Topics 386 Anonymous Functions: lambda 386 lambda Expressions 386 Why Use lambda? 388 How (Not) to Obfuscate Your Python Code 389 Nested lambdas and Scopes 390 Applying Functions to Arguments 392 The apply Built-in 392 Passing keyword arguments 393 apply-Like Call Syntax 393 Mapping Functions over Sequences: map 394 Functional Programming Tools: filter and reduce 395 List Comprehensions Revisited: Mappings 397 List Comprehension Basics 397 Adding Tests and Nested Loops 398 List Comprehensions and Matrixes 400 Comprehending List Comprehensions 402 Iterators Revisited: Generators 402 Generator Function Example 404 Extended Generator Function Protocol: send Versus next 406 Iterators and Built-in Types 406 Generator Expressions: Iterators Meet List Comprehensions 407 Timing Iteration Alternatives 408 Function Design Concepts 411 Functions Are Objects: Indirect Calls 412 Function Gotchas 413 Local Names Are Detected Statically 414 Defaults and Mutable Objects 415 Functions Without returns 417 Enclosing Scope Loop Variables 417 Chapter Summary 417 Quiz Answers 419 Part V 425 Modules: The Big Picture 427 Why Use Modules? 427 Python Program Architecture 428 How to Structure a Program 429 Imports and Attributes 429 Standard Library Modules 431 How Imports Work 431 1. Find It 432 The module search path 432 The sys.path list 434 Module file selection 435 Advanced module selection concepts 435 2. Compile It (Maybe) 436 3. Run It 436 Chapter Summary 437 Quiz Answers 439 Module Coding Basics 440 Module Creation 440 Module Usage 441 The import Statement 441 The from statement 442 The from * Statement 442 Imports Happen Only Once 442 import and from Are Assignments 443 Cross-File Name Changes 444 import and from Equivalence 444 Potential Pitfalls of the from Statement 445 When import is required 446 Module Namespaces 446 Files Generate Namespaces 447 Attribute Name Qualification 448 Imports Versus Scopes 449 Namespace Nesting 450 Reloading Modules 451 reload Basics 452 reload Example 453 Chapter Summary 454 Quiz Answers 456 Module Packages 457 Package Import Basics 457 Packages and Search Path Settings 458 Package __init__.py Files 458 Package Import Example 460 from Versus import with Packages 461 Why Use Package Imports? 462 A Tale of Three Systems 463 Chapter Summary 466 Quiz Answers 467 Advanced Module Topics 468 Data Hiding in Modules 468 Minimizing from * Damage: _X and __all__ 468 Enabling Future Language Features 469 Mixed Usage Modes: __name__ and __main__ 470 Unit Tests with __name__ 471 Changing the Module Search Path 472 The import as Extension 473 Relative Import Syntax 473 Why Relative Imports? 474 Module Design Concepts 476 Modules Are Objects: Metaprograms 477 Module Gotchas 479 Statement Order Matters in Top-Level Code 479 Importing Modules by Name String 480 from Copies Names but Doesn’t Link 481 from * Can Obscure the Meaning of Variables 482 reload May Not Impact from Imports 482 reload, from, and Interactive Testing 483 reload Isn’t Applied Transitively 484 Recursive from Imports May Not Work 485 Chapter Summary 486 Quiz Answers 487 Part VI 491 OOP: The Big Picture 493 Why Use Classes? 494 OOP from 30,000 Feet 495 Attribute Inheritance Search 495 Classes and Instances 497 Class Method Calls 498 Coding Class Trees 498 OOP Is About Code Reuse 501 Chapter Summary 504 Quiz Answers 505 Class Coding Basics 507 Classes Generate Multiple Instance Objects 507 Class Objects Provide Default Behavior 508 Instance Objects Are Concrete Items 508 A First Example 509 Classes Are Customized by Inheritance 511 A Second Example 512 Classes Are Attributes in Modules 513 Classes Can Intercept Python Operators 514 A Third Example 516 Why Use Operator Overloading? 517 The World’s Simplest Python Class 518 Chapter Summary 520 Quiz Answers 521 Class Coding Details 523 The class Statement 523 General Form 523 Example 524 Methods 526 Example 527 Calling Superclass Constructors 528 Other Method Call Possibilities 528 Inheritance 528 Attribute Tree Construction 529 Specializing Inherited Methods 530 Class Interface Techniques 531 Abstract Superclasses 532 Operator Overloading 533 Common Operator Overloading Methods 534 __getitem__ Intercepts Index References 535 __getitem__ and __iter__ Implement Iteration 535 User-Defined Iterators 536 Multiple iterators on one object 538 __getattr__ and __setattr__ Catch Attribute References 540 Emulating Privacy for Instance Attributes 541 __repr__ and __str__ Return String Representations 542 __radd__ Handles Right-Side Addition 544 __call__ Intercepts Calls 544 Function Interfaces and Callback-Based Code 545 __del__ Is a Destructor 547 Namespaces: The Whole Story 548 Simple Names: Global Unless Assigned 548 Attribute Names: Object Namespaces 548 The “Zen” of Python Namespaces: Assignments Classify Names 548 Namespace Dictionaries 550 Namespace Links 553 A More Realistic Example 554 Chapter Summary 557 Quiz Answers 558 Designing with Classes 560 Python and OOP 560 Overloading by Call Signatures (or Not) 561 Classes As Records 561 OOP and Inheritance: “Is-a” Relationships 563 OOP and Composition: “Has-a” Relationships 565 Stream Processors Revisited 566 OOP and Delegation 569 Multiple Inheritance 571 Classes Are Objects: Generic Object Factories 574 Why Factories? 575 Methods Are Objects: Bound or Unbound 576 Documentation Strings Revisited 577 Classes Versus Modules 579 Chapter Summary 579 Quiz Answers 580 Advanced Class Topics 581 Extending Built-in Types 581 Extending Types by Embedding 582 Extending Types by Subclassing 582 Pseudoprivate Class Attributes 585 Name Mangling Overview 585 Why Use Pseudoprivate Attributes? 586 New-Style Classes 587 Diamond Inheritance Change 588 Diamond inheritance example 589 Explicit conflict resolution 589 Other New-Style Class Extensions 591 Static and class methods 591 Instance slots 591 Class properties 592 New __getattribute__ overloading method 594 Static and Class Methods 594 Using Static and Class Methods 596 Function Decorators 598 Decorator Example 600 Class Gotchas 601 Changing Class Attributes Can Have Side Effects 601 Multiple Inheritance: Order Matters 602 Methods, Classes, and Nested Scopes 603 “Overwrapping-itis” 605 Chapter Summary 606 Quiz Answers 607 Part VII 615 Exception Basics 617 Why Use Exceptions? 618 Exception Roles 618 Exception Handling: The Short Story 619 The try/except/else Statement 623 try Statement Clauses 624 The try/else Clause 627 Example: Default Behavior 627 Example: Catching Built-in Exceptions 628 The try/finally Statement 629 Example: Coding Termination Actions with try/finally 630 Unified try/except/finally 631 Combining finally and except by Nesting 632 Unified try Example 633 The raise Statement 634 Example: Raising and Catching User-Defined Exceptions 635 Example: Passing Extra Data with raise 635 Example: Propagating Exceptions with raise 636 The assert Statement 637 Example: Trapping Constraints (but Not Errors) 637 with/as Context Managers 638 Basic Usage 638 The Context Management Protocol 640 Chapter Summary 642 Quiz Answers 643 Exception Objects 644 String-Based Exceptions 645 String Exceptions Are Right Out! 645 Class-Based Exceptions 646 Class Exception Example 646 Why Class Exceptions? 648 Built-in Exception Classes 651 Specifying Exception Text 652 Sending Extra Data and Behavior in Instances 653 Example: Extra data with classes and strings 653 General raise Statement Forms 655 Chapter Summary 657 Quiz Answers 658 Designing with Exceptions 659 Nesting Exception Handlers 659 Example: Control-Flow Nesting 661 Example: Syntactic Nesting 661 Exception Idioms 663 Exceptions Aren’t Always Errors 663 Functions Signal Conditions with raise 664 Debugging with Outer try Statements 664 Running In-Process Tests 665 More on sys.exc_info 666 Exception Design Tips 666 What Should Be Wrapped 666 Catching Too Much: Avoid Empty excepts 667 Catching Too Little: Use Class-Based Categories 669 Exception Gotchas 669 String Exceptions Match by Identity, Not by Value 670 Catching the Wrong Thing 671 Core Language Summary 671 The Python Toolset 672 Development Tools for Larger Projects 673 Chapter Summary 676 Quiz Answers 677 Part VIII 679 Installation and Configuration 681 Installing the Python Interpreter 681 Is Python Already Present? 681 Where to Fetch Python 681 Installation Steps 682 Configuring Python 683 Python Environment Variables 684 How to Set Configuration Options 686 Unix/Linux shell variables 686 DOS variables (Windows) 686 Other Windows options 687 Path files 687 Solutions to End-of-Part Exercises 688 Part I, Getting Started 688 Part II, Types and Operations 690 Part III, Statements and Syntax 696 Part IV, Functions 698 Part V, Modules 703 Part VI, Classes and OOP 706 Part VII, Exceptions and Tools 714 Index 723
دانلود کتاب Learning Python : Includes index