python_book_01.pdf
(
1332 KB
)
Pobierz
A Python Book
A Python Book: Beginning Python, Advanced
Python, and Python Exercises
Author:
Dave Kuhlman
Contact:
dkuhlman@davekuhlman.org
Address:
http://www.davekuhlman.org
Page 1
A Python Book
Revision
1.3a
Date
December 15, 2013
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/mitlicense.php.
Abstract
This document is a selflearning 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
A Python Book
Contents
1 Part 1 Beginning Python...........................................................................................10
1.1 Introductions Etc...................................................................................................10
1.1.1 Resources.......................................................................................................11
1.1.2 A general description of Python....................................................................12
1.1.3 Interactive Python..........................................................................................15
1.2 Lexical matters......................................................................................................15
1.2.1 Lines..............................................................................................................15
1.2.2 Comments......................................................................................................16
1.2.3 Names and tokens..........................................................................................16
1.2.4 Blocks and indentation..................................................................................16
1.2.5 Doc strings.....................................................................................................17
1.2.6 Program structure..........................................................................................17
1.2.7 Operators.......................................................................................................18
1.2.8 Also see.........................................................................................................19
1.2.9 Code evaluation.............................................................................................19
1.3 Statements and inspection preliminaries...........................................................20
1.4 Builtin datatypes.................................................................................................21
1.4.1 Numeric types................................................................................................21
1.4.2 Tuples and lists..............................................................................................21
1.4.3 Strings............................................................................................................24
1.4.3.1 The new string.format method...............................................................26
1.4.3.2 Unicode strings......................................................................................27
1.4.4 Dictionaries....................................................................................................29
1.4.5 Files...............................................................................................................32
1.4.6 Other builtin types........................................................................................35
1.4.6.1 The None value/type..............................................................................35
1.4.6.2 Boolean values.......................................................................................36
1.4.6.3 Sets and frozensets.................................................................................36
1.5 Functions and Classes A Preview......................................................................36
1.6 Statements.............................................................................................................37
1.6.1 Assignment statement....................................................................................37
1.6.2 import statement............................................................................................39
1.6.3 print statement...............................................................................................41
1.6.4 if: elif: else: statement...................................................................................43
1.6.5 for: statement.................................................................................................44
1.6.6 while: statement.............................................................................................48
Page 3
A Python Book
1.6.7 continue and break statements.......................................................................48
1.6.8 try: except: statement.....................................................................................49
1.6.9 raise statement...............................................................................................51
1.6.10 with: statement.............................................................................................52
1.6.10.1 Writing a context manager...................................................................52
1.6.10.2 Using the with: statement....................................................................53
1.6.11 del................................................................................................................54
1.6.12 case statement..............................................................................................55
1.7 Functions, Modules, Packages, and Debugging....................................................55
1.7.1 Functions.......................................................................................................55
1.7.1.1 The def statement...................................................................................55
1.7.1.2 Returning values....................................................................................55
1.7.1.3 Parameters..............................................................................................56
1.7.1.4 Arguments..............................................................................................56
1.7.1.5 Local variables.......................................................................................57
1.7.1.6 Other things to know about functions....................................................57
1.7.1.7 Global variables and the global statement.............................................58
1.7.1.8 Doc strings for functions.......................................................................60
1.7.1.9 Decorators for functions........................................................................60
1.7.2 lambda...........................................................................................................61
1.7.3 Iterators and generators.................................................................................62
1.7.4 Modules.........................................................................................................67
1.7.4.1 Doc strings for modules.........................................................................68
1.7.5 Packages........................................................................................................68
1.8 Classes...................................................................................................................69
1.8.1 A simple class................................................................................................69
1.8.2 Defining methods..........................................................................................70
1.8.3 The constructor..............................................................................................70
1.8.4 Member variables..........................................................................................70
1.8.5 Calling methods.............................................................................................71
1.8.6 Adding inheritance........................................................................................71
1.8.7 Class variables...............................................................................................72
1.8.8 Class methods and static methods.................................................................72
1.8.9 Properties.......................................................................................................74
1.8.10 Interfaces.....................................................................................................75
1.8.11 Newstyle classes.........................................................................................75
1.8.12 Doc strings for classes.................................................................................77
1.8.13 Private members..........................................................................................77
1.9 Special Tasks.........................................................................................................77
1.9.1 Debugging tools.............................................................................................77
Page 4
A Python Book
1.9.2 File input and output......................................................................................78
1.9.3 Unit tests........................................................................................................80
1.9.3.1 A simple example..................................................................................80
1.9.3.2 Unit test suites........................................................................................81
1.9.3.3 Additional unittest features....................................................................83
1.9.3.4 Guidance on Unit Testing......................................................................85
1.9.4 doctest............................................................................................................85
1.9.5 The Python database API..............................................................................87
1.9.6 Installing Python packages............................................................................88
1.10 More Python Features and Exercises..................................................................89
2 Part 2 Advanced Python............................................................................................90
2.1 Introduction Python 201 (Slightly) Advanced Python Topics.......................90
2.2 Regular Expressions..............................................................................................90
2.2.1 Defining regular expressions.........................................................................90
2.2.2 Compiling regular expressions......................................................................91
2.2.3 Using regular expressions..............................................................................91
2.2.4 Using match objects to extract a value..........................................................92
2.2.5 Extracting multiple items..............................................................................93
2.2.6 Replacing multiple items...............................................................................94
2.3 Iterator Objects......................................................................................................96
2.3.1 Example A generator function....................................................................98
2.3.2 Example A class containing a generator method......................................100
2.3.3 Example An iterator class.........................................................................102
2.3.4 Example An iterator class that uses yield.................................................104
2.3.5 Example A list comprehension.................................................................105
2.3.6 Example A generator expression..............................................................105
2.4 Unit Tests............................................................................................................106
2.4.1 Defining unit tests........................................................................................106
2.4.1.1 Create a test class.................................................................................106
2.5 Extending and embedding Python......................................................................109
2.5.1 Introduction and concepts............................................................................109
2.5.2 Extension modules.......................................................................................110
2.5.3 SWIG...........................................................................................................112
2.5.4 Pyrex............................................................................................................115
2.5.5 SWIG vs. Pyrex...........................................................................................120
2.5.6 Cython.........................................................................................................120
2.5.7 Extension types............................................................................................122
2.5.8 Extension classes.........................................................................................122
2.6 Parsing.................................................................................................................122
2.6.1 Special purpose parsers...............................................................................123
Page 5
Plik z chomika:
Expearre4
Inne pliki z tego folderu:
Expert Python Programming (2008).pdf
(10481 KB)
think_python.pdf
(12457 KB)
Python Data Visualization Cookbook - Milovanovic, Igor-signed.pdf
(14987 KB)
Python Cookbook, 3rd Edition.pdf
(10240 KB)
Black Hat Python.pdf
(7056 KB)
Inne foldery tego chomika:
Algo
C++
HTML
JavaScript
Lua
Zgłoś jeśli
naruszono regulamin