�

Learning Python

�

<![if !vml]>Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: python_conf_ora<![endif]>

Student Workbook

�

�

�

July, 2016

HTML Workbook Version 2.7-3.5

Copyright � Mark Lutz, 1997�2016


�

�

�

�

Introduction

�

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

<![if !supportLists]>������ <![endif]>Always start here, and click on the titles below to go to lecture unit pages.

<![if !supportLists]>������ <![endif]>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.

<![if !supportLists]>������ <![endif]>To return here, use your browser's "back" button, or create a shortcut to this file on your desktop.

General

<![if !supportLists]>������ <![endif]>As of October 2015, this workbook should render well in all browsers (Internet Explorer is no longer preferred).

<![if !supportLists]>������ <![endif]>Copy the Workbook folder to a hard drive or USB stick if pages open too slowly from a CD or server copy.

Other tips

<![if !supportLists]>������ <![endif]>For reference material, see Python's manuals, or the ebook copy of Python Pocket Reference in Extras (CD/USB version only)

<![if !supportLists]>������ <![endif]>See also the distribution package's top-level "README.txt" file for more usage notes.

<![if !supportLists]>������ <![endif]>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.

�

�

�

�

�

Contents

�

�

Preface

Python 2.X or 3.x?

About this class

Course topics

Daily ScheduLe

�

�

�

Part I: Python Language

�

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 Python RUNS programs

How you run 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��������

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

Lab session 2�

�

4. Basic Statements�����

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����������

Lab session 3

�

5. Functions��������

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

Lab session 4�

�

6. Modules

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�

Lab session 5

�

7. Classes�

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����

lab session 6

�

8. Exceptions������

Exception basics������

First examples����������

Exception idioms������

Exception catching modes

Class exceptions�����

Exception gotchas�

lab session 7

�

9. Built-in Tools Overview

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�����������

lab session 7

�

�

�

Part II: Python Applications

�

10. System Interfaces

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�����

lab session 8�

�

11. GUI Programming��

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

lab session 8�

�

12. Databases and Persistence�����

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

lab session 9

�

13. Text Processing�����

String objects: review��������

Splitting and joining strings��������

Regular expressions������������

Parsing languages

XML Parsing: regex, SAX, DOM, and Etree

lab session 10

�

14. Internet Scripting����

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�����

lab session 10

�

15. Extending Python in C/C++�������

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���

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. Advanced Topics

unicode text and binary data

Managed attributes

decorators

metaclasses

Context managers

python 3.X changes

lab session 13

�

18. Resources����

Internet resources��������������

Python books������������

Python conferences and services������������

And finally

�

�

�

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++�������������

Lab 13: Decorators and metaclasses�����

�

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)

�