python_book_01.pdf

(1045 KB) Pobierz
A Python Book: Beginning Python, Advanced 
Python, and Python Exercises
Author: 
Dave Kuhlman
Address: 
http://www.rexx.com/~dkuhlman
Page 1
revision
1.0a
date
March 31, 2009
Copyright
Copyright (c) 2009 Dave Kuhlman. All Rights Reserved. This document is 
subject to the provisions of the Open Source MIT License 
http://www.opensource.org/licenses/mit­license.php.
Abstract
This document is a self­learning document for a course in Python programming. 
This course contains (1) a part for beginners, (2) a discussion of several 
advanced topics that are of interest to Python programmers, and (3) a Python 
workbook with lots of exercises.
Page 2
1064095683.005.png 1064095683.006.png 1064095683.007.png 1064095683.008.png
 
Table of Contents
1   Part 1 ­­ Beginning Python........................................................................................8
1.1   Introduction ­­ Python 101 ­­ Beginning Python...............................................8
1.1.1   Important Features of Python....................................................................8
1.1.2   Where to Go For Additional help..............................................................9
1.2   Interactive Python..............................................................................................9
1.3   Lexical matters................................................................................................10
1.3.1   Lines.........................................................................................................10
1.3.2   Names and Tokens...................................................................................10
1.3.3   Blocks and Indentation............................................................................11
1.3.4   Doc Strings..............................................................................................11
1.3.5   Operators..................................................................................................11
1.3.6   Also See...................................................................................................12
1.3.7   Code Evaluation.......................................................................................13
1.4   Built­in Data Types.........................................................................................13
1.4.1   Strings......................................................................................................13
1.4.1.1   What strings are................................................................................13
1.4.1.2   When to use strings..........................................................................13
1.4.1.3   How to use strings............................................................................14
1.4.2   Sequences ­­ Lists and Tuples..................................................................16
1.4.2.1   What sequences are..........................................................................16
1.4.2.2   When to use sequences.....................................................................17
1.4.2.3   How to use sequences......................................................................17
1.4.3   Dictionaries..............................................................................................19
1.4.3.1   What dictionaries are........................................................................19
1.4.3.2   When to use dictionaries..................................................................20
1.4.3.3   How to use dictionaries....................................................................20
1.4.4   Files..........................................................................................................23
1.4.4.1   What files are...................................................................................23
1.4.4.2   When to use files..............................................................................23
1.4.4.3   How to use files................................................................................23
1.4.4.4   Reading Text Files...........................................................................23
1.5   Simple Statements...........................................................................................25
1.5.1   print statement..........................................................................................25
1.5.2   Assignment statement..............................................................................26
1.5.3   import statement......................................................................................27
Page 3
1064095683.001.png 1064095683.002.png 1064095683.003.png 1064095683.004.png
 
1.5.4   assert statement........................................................................................28
1.5.5   global statement.......................................................................................29
1.6   Compound statments ­­ Control Structures.....................................................30
1.6.1   if: statement..............................................................................................30
1.6.2   for: statement...........................................................................................31
1.6.2.1   The for: statement and unpacking....................................................33
1.6.3   while: statement.......................................................................................34
1.6.4   try:except: and raise ­­ Exceptions...........................................................35
1.7   Organization....................................................................................................37
1.7.1   Functions..................................................................................................37
1.7.1.1   A basic function...............................................................................37
1.7.1.2   A function with default arguments...................................................37
1.7.1.3   Argument lists and keyword argument lists.....................................37
1.7.1.4   Calling a function with keyword arguments....................................38
1.7.2   Classes and instances...............................................................................39
1.7.2.1   A basic class.....................................................................................39
1.7.2.2   Inheritance........................................................................................40
1.7.2.3   Class data.........................................................................................41
1.7.2.4   Static methods and class methods....................................................41
1.7.2.5   Properties..........................................................................................43
1.7.3   Modules...................................................................................................44
1.7.4   Packages...................................................................................................46
1.8   Acknowledgements and Thanks......................................................................47
1.9   See Also...........................................................................................................48
2   Part 2 ­­ Advanced Python......................................................................................49
2.1   Introduction ­­ Python 201 ­­ (Slightly) Advanced Python Topics.................49
2.2   Regular Expressions........................................................................................49
2.2.1   Defining regular expressions...................................................................49
2.2.2   Compiling regular expressions................................................................50
2.2.3   Using regular expressions........................................................................50
2.2.4   Using match objects to extract a value....................................................51
2.2.5   Extracting multiple items.........................................................................52
2.2.6   Replacing multiple items.........................................................................53
2.3   Iterator Objects................................................................................................55
2.3.1   Example ­ A generator function...............................................................57
2.3.2   Example ­ A class containing a generator method...................................58
2.3.3   Example ­ An iterator class......................................................................59
2.3.4   Example ­ An iterator class that uses yield..............................................61
2.3.5   Example ­ A list comprehension..............................................................62
2.3.6   Example ­ A generator expression...........................................................62
2.4   Unit Tests.........................................................................................................63
2.4.1   Defining unit tests....................................................................................63
2.4.1.1   Create a test class.............................................................................63
2.5   Extending and embedding Python...................................................................65
2.5.1   Introduction and concepts........................................................................65
2.5.2   Extension modules...................................................................................66
Page 4
2.5.3   SWIG.......................................................................................................68
2.5.4   Pyrex........................................................................................................71
2.5.5   SWIG vs. Pyrex.......................................................................................75
2.5.6   Cython......................................................................................................76
2.5.7   Extension types........................................................................................77
2.5.8   Extension classes.....................................................................................77
2.6   Parsing.............................................................................................................78
2.6.1   Special purpose parsers............................................................................78
2.6.2   Writing a recursive descent parser by hand.............................................78
2.6.3   Creating a lexer/tokenizer with Plex........................................................85
2.6.4   A survey of existing tools........................................................................94
2.6.5   Creating a parser with PLY......................................................................94
2.6.6   Creating a parser with pyparsing...........................................................100
2.6.6.1   Parsing comma­delimited lines......................................................100
2.6.6.2   Parsing functors..............................................................................101
2.6.6.3   Parsing names, phone numbers, etc...............................................102
2.6.6.4   A more complex example..............................................................103
2.7   GUI Applications...........................................................................................105
2.7.1   Introduction............................................................................................105
2.7.2   PyGtk.....................................................................................................105
2.7.2.1   A simple message dialog box.........................................................105
2.7.2.2   A simple text input dialog box.......................................................107
2.7.2.3   A file selection dialog box.............................................................109
2.7.3   EasyGUI.................................................................................................111
2.7.3.1   A simple EasyGUI example...........................................................112
2.7.3.2   An EasyGUI file open dialog example..........................................112
2.8   Guidance on Packages and Modules.............................................................112
2.8.1   Introduction............................................................................................112
2.8.2   Implementing Packages.........................................................................112
2.8.3   Using Packages......................................................................................113
2.8.4   Distributing and Installing Packages.....................................................113
2.9   End Matter.....................................................................................................114
2.9.1   Acknowledgements and Thanks............................................................115
2.9.2   See Also.................................................................................................115
3   Part 3 ­­ Python Workbook...................................................................................116
3.1   Introduction...................................................................................................116
3.2   Lexical Structures..........................................................................................116
3.2.1   Variables and names..............................................................................116
3.2.2   Line structure.........................................................................................118
3.2.3   Indentation and program structure.........................................................119
3.3   Execution Model............................................................................................120
3.4   Built­in Data Types.......................................................................................121
3.4.1   Numbers.................................................................................................121
3.4.1.1   Literal representations of numbers.................................................121
3.4.1.2   Operators for numbers....................................................................123
3.4.1.3   Methods on numbers......................................................................125
Page 5
Zgłoś jeśli naruszono regulamin