Learning Python

 

 

 

 

 

 

 

Student Course Workbook

 

 

 

February, 2007

HTML Workbook Version 2.5.1

Copyright © Mark Lutz, 1997—2007


 

 

 

 

Contents

 

 

This is the course workbook root page.  It contains all the material presented during the class, source code for examples and lab exercises, and links to related information on the Net.  Click on unit titles below to go to lecture unit pages. Click on the exercises title at the bottom of the list below to go to lab exercises, or use the links at the end of each lecture unit's page. Use your browser's "back" button to come back to this index, or create a shortcut to this file on your machine.  See also the CD-ROM distribution's "readme" file.

 

The paper workbook handouts (if provided) are essentially the same as the electronic HTML version of the workbook on the CD.  The paper workbook is simply a printed version of the HTML workbook on CD, but with minor formatting changes and some optional-reading items deleted to save space.  However, the HTML workbook also contains active hyperlinks to sections and related information.  In addition, the HTML workbook allows examples to be cut-and-paste into a text editor or the Python command-line (some example code is not in the CD's Examples directory). Therefore, the paper workbook is generally used only for taking notes; students are encouraged to follow along by viewing the HTML workbook in a web browser.

 

 


 

Lecture units

 

Preface

About this class

Daily scheduLE

Course topics

 

1: General Python Introduction      

So what’s Python?    

Why do people use Python?

Some quotable quotes           

A Python history lesson

Advocacy News

What’s Python good for?     

What’s Python not good for?           

The compulsory features list          

Python portability  

On apples and oranges          

Summary: Why Python?         

 

2. Using the Interpreter        

How to run Python programs           

Configuration details           

Module files: a first look    

The IDLE interface     

Other python ides

Time to start coding

Lab session 1  

 

3. Types and operators        

The ‘big picture’           

Numbers           

Strings              

Lists    

Dictionaries  

Tuples

Files    

General object properties   

Summary: Python’s type hierarchies            

Built-in type gotchas

Lab session 2  

 

4. Basic statements    

General concepts      

Assignment    

Expressions    

Print   

If selections  

Python syntax rules               

Truth tests    

While loops    

Break, continue, pass, and the loop else   

For loops         

Loop coding techniques

Comprehensive examples      

Basic coding gotchas             

Preview: program unit statements               

Lab session 3

 

5. Functions       

Function basics          

Scope rules in functions       

More on “global”       

More on “return”       

More on argument passing 

Special argument matching modes              

Odds and ends              

Function design concepts    

Functions are objects: indirect calls         

Function gotchas     

Optional case study set functions

Lab session 4  

 

6. Modules

Module basics              

Module files are a namespace         

Name qualification 

Import variants         

Reloading modules  

Odds and ends              

Module design concepts        

Modules are objects: metaprograms          

Module gotchas

optional Case study: a shared stack module       

Lab session 5

 

7. Classes

class basics

A more realistic example

Using the class statement 

Using class methods               

Customization via inheritance       

Specializing inherited methods       

Operator overloading in classes   

Namespace rules: the whole story               

OOP: inheritance and composition 

Classes are objects: factories         

Methods are objects: bound or unbound  

Odds and ends              

Class gotchas             

optional Case study: a set class    

Summary: OOP in Python        

lab session 6

 

8. Exceptions     

Exception basics         

First examples             

Exception idioms        

Exception catching modes  

Matching variations              

Exception gotchas    

lab session 7

 

9. Built-in tools overview

debugging options     

Inspecting name-spaces         

Dynamic coding tools            

Timing and profiling Python programs      

Packaging programs for delivery 

Summary: Python tool-set layers 

lab session 7

 

10. System interfaces 

                System Modules Overview

                Arguments, Streams, shell variables

file tools

directory tools

forking processes

python thread modules

fork versis spawnv

example: make forward-link files

Example: regression testing               

Example: packing/unpacking text files      

lab session 8  

 

11. GUI programming 

Python GUI Options    

The Tkinter ‘hello world’ program              

Adding buttons, frames, and callbacks

Getting input from a user

More details 

Building GUIs by subclassing frames            

Reusing GUIs by subclassing and attaching            

Advanced widgets: Images, grids, and more            

Sexier examples

Tkinter odds and ends

lab session 8  

 

12. Databases and persistence    

Object persistence: shelves 

Storing class instances       

Pickling objects without shelves   

Using simple dbm files             

Shelve gotchas           

Python SQL DATABASE API

ZODB object-oriented database

Persistence odds and endS

lab session 9

 

13. Text processing    

String objects: review            

Splitting and joining strings             

Regular expressions

Parsing languages   

lab session 10

 

14. Internet scripting   

Using sockets in Python        

The FTP module

email processing

Other client-side tools

Writing server-side CGI scripts          

The Grail web browser

JPython: Python for Java systems 

Active Scripting and com

Other Internet-related tools          

lab session 10

 

15. Extending Python in C/C++     

Review: Python tool-set layers       

Stuff Guido already wrote

Why integration?      

The ‘big picture’ revisited     

Integration modes   

A simple C extension module              

C module structure 

Binding C extensions to Python        

Data conversions: Python  Û  C       

C extension types       

Using C extension types in Python   

Wrapping C extensions in Python    

Writing extensions in C++     

swig example (pp2e)

Python and rapid development       

lab session 11

 

16. Embedding Python in C/C++  

General embedding concepts             

Running simple code strings               

Calling objects and methods           

Running strings: results & name-spaces    

Other code string possibilities         

Registering Python objects and strings     

Accessing C variables in Python      

C API equivalents in Python

Running code files from C    

Precompiling strings into byte-code            

Embedding under C++               

More on object reference counts   

Integration error handling              

Automated integration tools        

lab session 12

 

17. Resources   

Python portability  

Major python packages        

Internet resources   

Python books               

Python in the news: articles, chapters      

Python conferences and services   

And finally

lab session 12

 

 


 

Labwork and examples

 

Laboratory exercises  

Lab 1: Using the interpreter

Lab 2: Types and operators  

Lab 3: Basic statements         

Lab 4: Functions          

Lab 5: Modules              

Lab 6: Classes

Lab 7: Exceptions and built-in tools             

Lab 8: System interfaces and GUIs   

Lab 9: Persistence       

Lab 10: Text processing and the Internet   

Lab 11: Extending Python in C/C++     

Lab 12: Embedding Python in C/C++    

 

Selected exercise solutions

Lab 1:  Using the Interpreter               

Lab 2:  Types and Operators 

Lab 3:  Basic Statements        

Lab 4:  Functions         

Lab 5:  Modules             

Lab 6:  Classes

Lab 7:  Exceptions and built-in toolS

 

Exercise solution files directory    

Lecture example files directory

Final Exam (optional)