Learning Python

Student Workbook
July, 2016
HTML Workbook Version 2.7-3.5
Copyright � Mark Lutz, 1997�2016
This is the root page of the class workbook.� The workbook contains all the material
presented during the class, source code for examples and lab exercises, and
links to related information on the web.�
Usage tips:
Navigation
● Always start here, and click
on the titles below to go to lecture unit pages.
● To go to lab exercises, click
either the links at the end of each lecture unit page, or the exercises link
near the end of this page.
● To return here, use your
browser's "back" button, or create a shortcut to this file on your
desktop.
General
● As of October
2015, this workbook should render well in all browsers (Internet Explorer is no longer
preferred).
● Copy the
Workbook folder to a hard
drive or USB stick if pages open too slowly from a CD or server
copy.
Other tips
● For reference material, see
Python's manuals, or the ebook copy of Python
Pocket Reference in Extras (CD/USB
version only)
● See also the
distribution package's top-level "README.txt"
file for more usage notes.
● This workbook
is mostly a conversation
starter�the class
goes off-page often, and is driven by your input.
The usual first question: for
pointers on which version of Python to install and use for the class (2.X or
3.X), see the Preface below, or wait for the first lab session.
Python 2.X or 3.x?
About this class
Course topics
Daily ScheduLe
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?������
How Python RUNS programs
How you run programs������
Configuration details�������
Module files: a first look
The IDLE interface��
Other python ides
Time to start coding
A first pass
The �big picture��������
Numbers���������
dynamic typing interlude
Strings������������
Lists���
Dictionaries�
Tuples��������������
Files���
General object properties��������������
Summary: Python�s type hierarchies������
Built-in type gotchas
General syntax concepts
Assignment���
Expressions��
Print��
If selections
Python syntax rules
Documentation sources interlude���������
Truth tests��
While loops��
Break, continue, pass, and the loop else�������������
For loops
Comprehensions and iterations��
Loop coding techniques
Comprehensive loop examples�����
Basic coding gotchas����������
Preview: program unit statements����������
Function basics��������
Scope rules in functions���
More on �global� (and �nonlocal�)��������
More on �return�����
More on argument passing�������������
Special argument matching modes���������
Odds and ends�����������
Generator expressions and functions
Function design concepts
Functions are objects: indirect calls���
Function gotchas���
Optional case study: set functions
Module basics�����������
Module files are a namespace�����
Name qualification��������������
Import variants�������
Reloading modules��������������
package imports
Odds and ends�����������
Module design concepts���
Modules are objects: metaprograms�����
Module gotchas
optional Case study: a shared stack module�
OOP: the big picture
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 examples: inheritance and composition����
Classes and methods are objects�������������
Odds and ends�����������
new style classes
Class gotchas�����������
optional Case study: a set class
Summary: OOP in Python����
Exception basics������
First examples����������
Exception idioms������
Exception catching modes
Class exceptions�����
Exception gotchas�
The secret handshake
debugging options�
Inspecting name-spaces������
Dynamic coding tools��������
Timing and profiling Python programs�
file types and Packaging options
development tools for larger projects������������
Summary: Python tool-set layers�����������
System Modules Overview
running shell commands
Arguments, Streams, shell variables
file tools
directory tools
forking processes
Thread modules and Queues
The Subprocess and multiprocessing modules
IPC tools: pipes, sockets, signals
fork versis spawnv
Larger exampleS�����
Python GUI Options�
The Tkinter �hello world� program�������
Adding buttons, frames, and callbacks
Getting input from a user
ASSORTED tkinter details�
Building GUIs by subclassing frames������
Reusing GUIs by subclassing and attaching�����
Advanced widgets: Images, grids, and more�����
LARGer examples
Tkinter odds and ends
Object persistence: shelves�����������
Storing class instances����
Pickling objects without shelves������������
Using simple dbm files���������
Shelve gotchas��������
ZODB object-oriented database
Python SQL DATABASE API
Persistence odds and endS
String objects: review��������
Splitting and joining strings��������
Regular expressions������������
Parsing languages
XML Parsing: regex, SAX, DOM, and Etree
Using sockets in Python����
The FTP module
email processing
Other client-side tools
building web sites with python
writing server-side CGI scripts����
Jython: Python for Java systems��������������
Active Scripting and com
Other Internet-related tools�����
Python Integration model
Review: Python tool-set layers����������������
Why integration?���
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 (pp)
Python and rapid development���
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�����
unicode text and binary data
Managed attributes
decorators
metaclasses
Context managers
python 3.X changes
Internet resources��������������
Python books������������
Python conferences and services������������
And finally
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++�������������
Lab 13: Decorators and metaclasses�����
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