A Python Book: Beginning Python, Advanced Python, And .

Transcription

A Python BookA Python Book: Beginning Python, AdvancedPython, and Python ExercisesAuthor:Dave p://www.davekuhlman.orgPage 1

A Python BookRevision1.3aDateDecember 15, 2013CopyrightCopyright (c) 2009 Dave Kuhlman. All Rights Reserved. This document is subjectto the provisions of the Open Source MIT Licensehttp://www.opensource.org/licenses/mit license.php.AbstractThis 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 advancedtopics that are of interest to Python programmers, and (3) a Python workbook withlots of exercises.Page 2

A Python BookContents1 Part 1 Beginning Python.101.1 Introductions Etc.101.1.1 Resources.111.1.2 A general description of Python.121.1.3 Interactive Python.151.2 Lexical matters.151.2.1 Lines.151.2.2 Comments.161.2.3 Names and tokens.161.2.4 Blocks and indentation.161.2.5 Doc strings.171.2.6 Program structure.171.2.7 Operators.181.2.8 Also see.191.2.9 Code evaluation.191.3 Statements and inspection preliminaries.201.4 Built in data types.211.4.1 Numeric types.211.4.2 Tuples and lists.211.4.3 Strings.241.4.3.1 The new string.format method.261.4.3.2 Unicode strings.271.4.4 Dictionaries.291.4.5 Files.321.4.6 Other built in types.351.4.6.1 The None value/type.351.4.6.2 Boolean values.361.4.6.3 Sets and frozensets.361.5 Functions and Classes A Preview.361.6 Statements.371.6.1 Assignment statement.371.6.2 import statement.391.6.3 print statement.411.6.4 if: elif: else: statement.431.6.5 for: statement.441.6.6 while: statement.48Page 3

A Python Book1.6.7 continue and break statements.481.6.8 try: except: statement.491.6.9 raise statement.511.6.10 with: statement.521.6.10.1 Writing a context manager.521.6.10.2 Using the with: statement.531.6.11 del.541.6.12 case statement.551.7 Functions, Modules, Packages, and Debugging.551.7.1 Functions.551.7.1.1 The def statement.551.7.1.2 Returning values.551.7.1.3 Parameters.561.7.1.4 Arguments.561.7.1.5 Local variables.571.7.1.6 Other things to know about functions.571.7.1.7 Global variables and the global statement.581.7.1.8 Doc strings for functions.601.7.1.9 Decorators for functions.601.7.2 lambda.611.7.3 Iterators and generators.621.7.4 Modules.671.7.4.1 Doc strings for modules.681.7.5 Packages.681.8 Classes.691.8.1 A simple class.691.8.2 Defining methods.701.8.3 The constructor.701.8.4 Member variables.701.8.5 Calling methods.711.8.6 Adding inheritance.711.8.7 Class variables.721.8.8 Class methods and static methods.721.8.9 Properties.741.8.10 Interfaces.751.8.11 New style classes.751.8.12 Doc strings for classes.771.8.13 Private members.771.9 Special Tasks.771.9.1 Debugging tools.77Page 4

A Python Book1.9.2 File input and output.781.9.3 Unit tests.801.9.3.1 A simple example.801.9.3.2 Unit test suites.811.9.3.3 Additional unittest features.831.9.3.4 Guidance on Unit Testing.851.9.4 doctest.851.9.5 The Python database API.871.9.6 Installing Python packages.881.10 More Python Features and Exercises.892 Part 2 Advanced Python.902.1 Introduction Python 201 (Slightly) Advanced Python Topics.902.2 Regular Expressions.902.2.1 Defining regular expressions.902.2.2 Compiling regular expressions.912.2.3 Using regular expressions.912.2.4 Using match objects to extract a value.922.2.5 Extracting multiple items.932.2.6 Replacing multiple items.942.3 Iterator Objects.962.3.1 Example A generator function.982.3.2 Example A class containing a generator method.1002.3.3 Example An iterator class.1022.3.4 Example An iterator class that uses yield.1042.3.5 Example A list comprehension.1052.3.6 Example A generator expression.1052.4 Unit Tests.1062.4.1 Defining unit tests.1062.4.1.1 Create a test class.1062.5 Extending and embedding Python.1092.5.1 Introduction and concepts.1092.5.2 Extension modules.1102.5.3 SWIG.1122.5.4 Pyrex.1152.5.5 SWIG vs. Pyrex.1202.5.6 Cython.1202.5.7 Extension types.1222.5.8 Extension classes.1222.6 Parsing.1222.6.1 Special purpose parsers.123Page 5

A Python Book2.6.2 Writing a

A Python Book A Python Book: Beginning Python, Advanced Python, and Python Exercises Author: Dave Kuhlman Contact: dkuhlman@davekuhlman.org