Dive Into Python
Total Page:16
File Type:pdf, Size:1020Kb
Table of Contents Dive Into Python...............................................................................................................................................................1 Chapter 1. Installing Python...........................................................................................................................................2 1.1. Which Python is right for you?......................................................................................................................2 1.2. Python on Windows.......................................................................................................................................2 1.3. Python on Mac OS X.....................................................................................................................................3 1.4. Python on Mac OS 9......................................................................................................................................5 1.5. Python on RedHat Linux...............................................................................................................................5 1.6. Python on Debian GNU/Linux......................................................................................................................6 1.7. Python Installation from Source....................................................................................................................6 1.8. The Interactive Shell......................................................................................................................................7 1.9. Summary........................................................................................................................................................8 Chapter 2. Your First Python Program.........................................................................................................................9 2.1. Diving in........................................................................................................................................................9 2.2. Declaring Functions.......................................................................................................................................9 2.3. Documenting Functions...............................................................................................................................10 2.4. Everything Is an Object...............................................................................................................................11 2.5. Indenting Code.............................................................................................................................................13 2.6. Testing Modules...........................................................................................................................................14 Chapter 3. Native Datatypes.........................................................................................................................................15 3.1. Introducing Dictionaries..............................................................................................................................15 3.2. Introducing Lists..........................................................................................................................................17 3.3. Introducing Tuples.......................................................................................................................................22 3.4. Declaring variables......................................................................................................................................23 3.5. Formatting Strings.......................................................................................................................................25 3.6. Mapping Lists..............................................................................................................................................26 3.7. Joining Lists and Splitting Strings...............................................................................................................28 3.8. Summary......................................................................................................................................................29 Chapter 4. The Power Of Introspection.......................................................................................................................31 4.1. Diving In......................................................................................................................................................31 4.2. Using Optional and Named Arguments.......................................................................................................32 4.3. Using type, str, dir, and Other Built−In Functions......................................................................................33 4.4. Getting Object References With getattr.......................................................................................................36 4.5. Filtering Lists...............................................................................................................................................38 4.6. The Peculiar Nature of and and or...............................................................................................................39 4.7. Using lambda Functions..............................................................................................................................41 4.8. Putting It All Together.................................................................................................................................43 4.9. Summary......................................................................................................................................................45 Chapter 5. Objects and Object−Orientation...............................................................................................................47 5.1. Diving In......................................................................................................................................................47 5.2. Importing Modules Using from module import...........................................................................................49 5.3. Defining Classes..........................................................................................................................................50 5.4. Instantiating Classes.....................................................................................................................................53 5.5. Exploring UserDict: A Wrapper Class........................................................................................................54 5.6. Special Class Methods.................................................................................................................................56 5.7. Advanced Special Class Methods................................................................................................................59 Dive Into Python i Table of Contents Chapter 5. Objects and Object−Orientation 5.8. Introducing Class Attributes........................................................................................................................60 5.9. Private Functions.........................................................................................................................................62 5.10. Summary....................................................................................................................................................63 Chapter 6. Exceptions and File Handling....................................................................................................................64 6.1. Handling Exceptions....................................................................................................................................64 6.2. Working with File Objects...........................................................................................................................66 6.3. Iterating with for Loops...............................................................................................................................70 6.4. Using sys.modules.......................................................................................................................................72 6.5. Working with Directories............................................................................................................................74 6.6. Putting It All Together.................................................................................................................................77 6.7. Summary......................................................................................................................................................78 Chapter 7. Regular Expressions...................................................................................................................................81 7.1. Diving In......................................................................................................................................................81 7.2. Case Study: Street Addresses.......................................................................................................................81 7.3. Case Study: Roman Numerals.....................................................................................................................83