Introduction To Python

gnhlug, 25 June 2003

Author: Bill Sconce
Contact: sconce@in-spec-inc.com
Organization: In Spec, Inc.

Abstract

A selective introduction to Python, with primary goals of:

This will not be a complete introduction to Python. Most of the wonders of Python, such as its OO capabilities, or the richness of its standard library, will have to be another session. Or an exercise for the student!

Table of Contents

1 Foreword

Python Worship
Python worship was common amongst tribes in central Africa and killing a python was regarded as a serious crime, punishable by death. From here, slaves took their religion to the West Indies, especially to Haiti, where it is still (surreptitiously) practised under the name of voodoo. Snakes of the World, [Mattison86]

2 "It Fits Your Brain"

EEs in the audience may appreciate a metaphor of impedance matching.

A programmer's brain works in ways (mysterious to us!) which are sometimes powerful, sometimes remarkably narrow and obtuse. Our ability to grasp generalities and visual analogues is astounding. Our ability to resist distractions or see through syntactic cruft is pitiful.

Consider an imagined interface between this remarkable instrument, our brain, and a complex task such as creating a computer program. Information must get in; information and control must get out.

In electronic terms an interface is characterized by the impedance on each side. Efficiency of transfer is maximized when the impedances match.

Python's homely mantra of "it fits your brain" describes this interface, and suggests the goals sought by Python's designers of matching Python's capabilities to the kinds of things which our human brains do well. The impedance match, although not yet perfect, is better than that of almost any other language.

Each time you "enlarge your envelope", pushing through a boundary of knowledge to learn something new about Python, you will be pleasantly surprised. You will often say, "Yes! That's the way I'd have built it."

My challenge is to demonstrate that "Yes!".

3 Introduction

I want to give you a head start on your own experience of learning Python. That means building a program or two, and our emphasis tonight will be hands-on.

"Head start" means a selection of tips and illuminations which you're likely to find valuable, which you would find out from your own investigation, but of which you might say, "I wish I'd known about that sooner".

Certain things I'll assume to be obvious, such as what a for loop does.

Certain other things, such as Python's excellent object-oriented facilities, will wait for another evening.

Please do ask questions as we go along. I'd rather give a good introduction to a few things.

Python

Tonight's definition

Python is designed and developed for the programmer's productivity, convenience, accomplishment, and enjoyment. Contrast with almost every other programming language. If Python is your first language you are especially lucky: Python offers the best learning environment out there. Getting started is easy, every step is rewarding.

After Python you will probably find that no other language will taste fully cooked.

A more usual definition
Python is an interactive, byte-code-compiled, very high level, dynamically typed, object-oriented language. It is often compared to Tcl, Perl, Scheme, or Java. [pycomps]
The official synopsis says
Python combines remarkable power with very clear syntax. It has modules, classes, exceptions, very high level dynamic data types, and dynamic typing. There are interfaces to many system calls and libraries, as well as to various windowing systems (X11, Motif, Tk, Mac, MFC). New built-in modules are easily written in C or C++. Python is also usable as an extension language for applications that need a programmable interface. [pysum]
Another "official" definition
python, (Gr. Myth. An enormous serpent that lurked in the cave of Mount Parnassus and was slain by Apollo) 1. any of a genus of large, non-poisonous snakes of Asia, Africa and Australia that suffocate their prey to death. 2. popularly, any large snake that crushes its prey. 3. totally awesome, bitchin' language that will someday crush the $'s out of certain other so-called VHLL's ;-) [pysum]

3.1 The Why of Python

To ease the programmer's burdens, and to make programming fun.

When a language decision must be made which trades off almost anything else for a programmer's comfort and productivity, Python decides in favor of the programmer.

Obviously, building a language requires a multitude of decisions which will affect programmers' lives.

Sometimes tradeoffs seem necessary in favor of performance. Although Python is in fact no slouch in performance (performance does matter!), you as a programmer will almost never be required to put up with wart in the language so that the computer will run faster.

Sometimes tradeoffs seem necessary for backward compatibility. Backward compatibility does matter, especially so to Pythonistas. But when experience in the field has demonstrated a mistake in the language's design which hurts programmers, Python has been changed to fix it.

With Python you will be free of cruft which was created to help compilers read paper tape 30 years ago.

3.2 Clarity

Clarity is one of Python's core design values. You will find that the most natural way of writing Python code happens to yield code which you can easily read next week or next year. Your convenience in picking up and understanding existing code is paramount, and matters greatly in real-world programming.

Language clarity also encourages concept clarity, and this speeds up the programming process.

3.3 What Really Counts - You

Everyone who uses Python seems to be having a good time. Python is clean. Using Python you will understand problems more clearly. You will often work much more rapidly, and you will produce better code. You will find great satisfaction in working with Python, and not infrequently amazement as well.

3.4 Python's Beginnings

1989 - Guido, then working on the Amoeba distributed operating system at Centrum voor Wiskunde en Informatica in the Netherlands, chooses "Python" (in honor of Monty Python's Flying Circus) as the name for a "hobby project" which would become a replacement for the language ABC.

1991 - First public release.

1994 - comp.lang.python usenet group founded.

Much (or most) of Python and its features have come from good ideas in other languages, among them indentation, namespaces, I/O, virtual machines, lists, dictionaries.

For an authentic tale of genesis, see Guido's foreword to Mark Lutz's book "Programming Python". [GvR96]

From the beginning Python has been a Free language.

3.5 Python's Character

Python is one of the few languages in wide use which do not owe allegiance to proprietary interests. It is Free Software - no marketroid distorts its features, no corporation tries to set standards, and there are no limitations on platform.

Definition - PEPs
Python Enhancement Proposals. Enhancements and changes to Python are proposed and discussed in the community, sometimes with considerable warmth and at considerable length, before a new release of Python incorporates them.

Changes to Python are well vetted.

(And, of course, flamed sometimes. See the discussions on the proposed ternary operator, for example!)

3.6 Some Mandatory Mentions

There's insufficient time to explore even a sampling of Python's features, but no introduction to Python should completely overlook the ones below. Some of these features will be obvious almost immediately as you learn Python, while others may dawn on you only weeks or months into your Python experience.

  • Everything is an object
  • There is no goto statement
  • There are no pointers
  • Buffer overflow errors, an expensive class of errors, are impossible!
  • Strongly and richly typed
  • Dynamic typing
  • No overhead or lost-thinking-time for declarations
  • Introspection - your program can examine its own inner workings
  • Compiles to bytecode, execution is by VM - similar to Java
  • Compile step is automatic and invisible
  • No linking or make step at all
  • In fact, Python can compile to Java bytecode (Jython)!
  • Namespaces
  • Booleans (an example of re-engineering to ease the programmer's burden)
  • No need for help-the-braindamaged-compiler cruft (no braces for blocks)

3.7 Some "Character References"

Where does Python run?

  • Linux
  • Java (and anywhere Java runs) - Jython is Python implemented in 100% Pure Java
  • Microsoft W32 (very, very well! - knows about COM, etc. - Mark Hammond)
  • Palmtop computers
  • Mainframes, Alphas, SPARCs
  • Macs
  • Embedded systems

Who uses Python?

  • Google - "Python has been an important part of Google since the beginning, and remains so as the system grows and evolves. Today dozens of Google engineers use Python, and we're looking for more people with skills in this language." - Peter Norvig, Director of Search Quality
  • Industrial Light & Magic - "Python plays a key role in our production pipeline. Without it a project the size of Star Wars: Episode II would have been very difficult to pull off. From crowd rendering to batch processing to compositing, Python binds all things together." - Tommy Burnette, Senior Technical Director
  • NASA - "We chose Python because it provides maximum productivity, code that's clear and easy to maintain, strong and extensive (and growing!) libraries, and excellent capabilities for integration with other applications on any platform. All of these characteristics are essential for building efficient, flexible, scalable, and well-integrated systems, which is exactly what we need. Python has met or exceeded every requirement we've had." - Steve Waterbury, Software Group Leader
  • Eric S. Raymond - "When you're writing working code nearly as fast as you can type and your misstep rate is near zero, it generally means you've achieved mastery of the language. But that didn't make sense, because it was still day one and I was regularly pausing to look up new language and library features! This was my first clue that, in Python, I was actually dealing with an exceptionally good design. Most languages have so much friction and awkwardness built into their design that you learn most of their feature set long before your misstep rate drops anywhere near zero. Python was the first general-purpose language I'd ever used that reversed this process." http://pythonology.org/success&story=esr
  • Bruce Eckel, author of Thinking in Java and Thinking in C++ - "To me, Python and Java present a very potent combination for program development because of Java’s architecture and tool set, and Python’s extremely rapid development (generally considered to be 5-10 times faster than C++ or Java)."
  • Bruce Eckel - "I fell in love with Perl for a couple of months, and explored all the (dark) corners. It's what eventually drove me to Python." http://mindview.net/Books/Python/ThinkingInPython.html
  • Red Hat - for installation, configuration, and package management
  • RealNetworks - load testing and feature testing on all supported platforms
  • BEA - testing e-commerce software
  • NIH - genetic analysis
  • NWS - weather forecasting
  • Lawrence Livermore National Laboratories - a numerical engineering environment
  • Theoretical Physics Division, Los Alamos National Laboratories - control of large-scale physics codes on massively parallel supercomputers and clusters

Some of these applications give the lie to objections about "performance" of an app which isn't written in a low-level language. In fact, real evaluation of performance rests on objective profiling, and the "glue" logic executed by the Python VM may be an infinitesimal fraction of the overall application's machine load. (Much of the work may well be done in a low-level, compiled language - NumPy is an excellent example, as are most GUI frameworks.)

4 Development - Three Levels of Features

Python's facilities for rapid development depend on features which make it extremely easy to prototype, to make a tentative test run, to "try it and throw it away".

4.2 Interactive Window

A Python interactive window is right up there with the print statement for lightweight usefulness.

If you want to test something, or you "almost" know the syntax of something you want to do, a few statements in an interactive window let you check things out.

>>> def sum(a, b):
...     return a, b, a+b
...
>>> sum(3, 5)
(3, 5, 8)
>>>

4.3 Python-Aware Editor

For real programming, of course, you need an editor.

For programming efficiency, your editor should be Python-aware. One common feature is syntax coloring, which is nice. What is essential, however, is a facility for instantly running a piece of code while it's being developed.

There are many such editors out there, and there are full-blown IDEs too. More on this later.

5 Python's Objects - a Quick Picture

Although we're not doing object-oriented design tonight, a quick picture of Python's objects will be a big help in visualizing what's going on even when you're doing common tasks.

Mostly you don't have to visualize what's going on when you're doing common tasks, but when you're doing advanced tasks it's much easier in Python because the same machinery underlies everything.

5.1 Assignment - "n = 123"

Executing the statement:

>>> n = 2 + 2

does four things.

  • creates an integer object
  • calculates the sum 4 and gives that value to the new integer object
  • creates a name n in the current namespace
  • binds the name n to the new integer object

5.2 Multiple Labels - "m = n"

An object can be bound to more than one label at a time:

>>> m = n
>>> m
4
>>> n
4

results in our integer object having two names.

Your program can now refer to our integer object by either n or m. There are two names, but only one object. (So of course the value associated with each name is the same, 4.)

Just for fun, here's a first example of Python's introspection:

>>> m == n
True
>>> m is n
True

The first test of these two tests compares the value of the object bound to m (4) to the value of the object bound to n (4).

The second test is introspection. It checks to see whether the names m and n are bound to the same object.

To finish with the fun (don't worry about this, we won't need it tonight):

>>> s = 'spam'
>>> t = s
>>> s == t
True
>>> s is t
True
>>> t = 'sp' + 'am'
>>> t
'spam'
>>> s == t
True
>>> s is t
False

(By the way, using True and False to report Boolean values is one of the very small, and very nifty, features of 2.3.)

5.3 Everything is an Object

In Python everything is an object. Try:

>>> n
4
>>>
print dir(a)

The result shows that even so "simple" an object as an integer has a number of characteristics, most notably methods (see the add & subtract?). The double underscores signify that these characteristics are internal - they're Python's worry, although they're not protected:

['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__','__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__']

However, if you don't need to use OO features you don't have to. Not any. Python is designed so that you can go as deeply into its object structure as you wish, yet if your needs are simple you can safely ignore the powerful machinery running behind the curtains.

(This also gives you a platform for learning about OO techniques, if you wish to use Python to do that. No other language makes exploration of OO so easy.)

We'll talk more about this if we have time.

5.4 A Richer Example - a file Object

Our demo program is going to need to read a file. Here's a file object:

>>> i = open('spam.txt', 'w')
<open file 'spam.txt', mode 'w' at 0x008DD598>
>>> dir(i)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'close', 'closed', 'fileno', 'flush', 'isatty', 'mode', 'name','newlines', 'next', 'read', 'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines', 'xreadlines']

Note the methods read and write (expected):

>>> i.read
<built-in method read of file object at 0x008DD598>

(Note that Python doesn't tell you i, the name bound to the file object, but only file object at <some_address>. That's because any Python may be bound to more than one name. Or to no name at all.)

For an example of an attribute look at closed:

>>> i.closed
False
>>>

Finally, note the next method. It's one of the handiest things about file objects - it lets Python make a file available to you in a for loop.

6 Three Environments

Back to development. Let's get ready to program something. I recommend that you consider keeping three environments available at almost all times during your Python development.

  1. An interactive Python window. A "sandbox" or "scratchpad".
  2. A good editor. The one I'm using for this talk is Neil Hodgson's SciTE. Lean, effective, highly recommended. [SciTE]
  3. The distro's HTML documentation - for instance, open in a browser window, or at least bookmarked.

Your favorite printed book will also be handy.

7 Demonstration: Getting the Weather

As tonight's primary example let's solve a text-processing problem.

Let's say that you're a pilot, and that you use the Web to get weather information. One important class of weather data is winds aloft, which the National Weather Service forecasts for various altitudes at various locations. This information is available on a NWS Web site.

However, the Web site for the winds suffers from bloat. It has color logos, a panel of links down the left side, a search-engine pulldown window. Let's say that you have to live with a 28.8K modem. Slow.

Finally, only a portion of the real data themselves are usually of interest. Let's say that you usually only want to look at Boston, Albany, New York City, Pittsburgh, and Elkins. Also, let's say that your airplane can't reach altitudes above 18,000 feet. Sounds like an application for a filter.

7.1 NWS Winds Aloft Site Page Source

The part of the NWS page which contains the real data is about two-thirds the way down in 429 lines of javascript and HTML. Here's an extract starting at about line 294:

DATA BASED ON 201200Z
VALID 210000Z   FOR USE 2100-0600Z. TEMPS NEG ABV 24000

FT  3000    6000   9000    12000   18000   24000  30000  34000  39000
BDL 0917 0817+09 1312+04 1612+00 1827-12 2032-23 225139 226548 235655
BGR 0210 0309+10 0306+07 3306+01 2610-11 2416-24 254340 255749 254655
CAR 3510 0510+11 0213+07 0111+02 0308-12 0512-25 041641 041549 282355
PWM 0312 0113+09 9900+07 9900+01 2111-11 2415-23 234539 245949 245356
EMI 0215 9900+08 2207+02 2118-03 2032-16 2139-27 224839 226044 235149
ACK 0720 0815+11 1207+07 1908+02 2115-09 2123-21 234337 235947 246556
BOS 0620 0514+10 1006+05 1709+01 1916-11 2125-23 224839 236248 236056
BML 0206 0406+07 9900+07 9900+01 2009-12 2212-24 243741 244849 243755
ACY 1111 1508+10 2012+05 2121+00 2137-12 2149-23 237538 238346 236953
ALB 0618 0920+09 1016+04 1213-01 1717-13 1727-24 202840 225350 234253
BUF 0515 0612+06 0815+02 0722-03 1021-15 1025-27 092844 082649 091149
JFK 0925 1018+09 1511+04 1816-01 1938-11 2039-23 226139 227447 236053
[...]

7.2 Python to the Rescue - winds.py

Here's one solution to this filtering problem, using Python. It's the program I'll develop for you.

Please make notes and ask questions:

progname = 'winds'              # wjs 03.6.20
    
I = 'winds_aloft_pagesource.html'
i = open(I, 'r')
for iLine in i:
    iTokens = iLine.split()
    if len(iTokens):
        if iTokens[0] == 'DATA':
            print iLine,
        if iTokens[0] == 'VALID':
            print iLine
        elif iTokens[0] in ('FT', 'BOS', 'ALB', 'JFK', 'AGC', 'EKN'):
            print iLine[:41]
i.close()

7.3 And the Results

And here's SciTE's log window from running winds.py:

$
$ python winds.py
DATA BASED ON 201200Z
VALID 210000Z   FOR USE 2100-0600Z. TEMPS NEG ABV 24000

FT  3000    6000   9000    12000   18000
BOS 0620 0514+10 1006+05 1709+01 1916-11
ALB 0618 0920+09 1016+04 1213-01 1717-13
JFK 0925 1018+09 1511+04 1816-01 1938-11
AGC 0218 0218+05 0312+00 9900-06 0311-19
EKN      3613+05 3409+00 2906-05 2906-19
$

7.4 Extra Credit - winds_url.py

It wouldn't be fair to fail to give at least the first hint of credit to Python's amazing standard library.

Once we have our little filter program running (developed using a page source text file captured by a browser, perhaps), our pilot might wonder: how hard would it be to have Python do the HTTP retrieval as well as the filtering?

The answer: easy.

The changed lines, not counting the program name, are the three lines marked with #URL. That is, all we do is change the form of the "file" we use as the input. Essentially, Python lets you treat a URL as a file object. All of the low-level stuff, HTTP protocol, socket handling, and so on are handled for you. This is typical of the Python library.

Note that the urllib module also takes care of proxies:

progname = 'winds_url'           # wjs 03.6.20
    
from urllib import urlopen                                                         #URL
I = 'http://aviationweather.gov/fdwinds/text/boston_fd2.shtml'                     #URL
i = urlopen(I, proxies = {'http': 'http://someproxyserver.in-spec-inc.com:8000'})  #URL
for iLine in i:
    iTokens = iLine.split()
    if len(iTokens):
        if iTokens[0] == 'DATA':
            print iLine,
        if iTokens[0] == 'VALID':
            print iLine
        elif iTokens[0] in ('FT', 'BOS', 'ALB', 'JFK', 'AGC', 'EKN'):
            print iLine[:41]
i.close()

8 Other Selected Topics (Time Permitting)

8.1 Recovering from Braces - A "2-Step Program"

OK, OK, so we know someone will want to discuss how scary it is to go "cold turkey" about braces.

First, a side note: it's not accurate to say that in Python blocking == indentation. In Python blocking is signalled by a colon.

What follows a colon may be on the same line, for example:

if 2+2 == 4: print 'spam'; print 'eggs'; pi = 3.14

Indentation is one of the structure tools (an indispensable one to be sure), but its purpose is to support the colon notation. I make this perhaps pedantic distinction because the power of the colon's visual cue is easily overlooked, and because clarity of visual clues is fundamental to Python.

The nested structure in:

if file_is_empty: break

is instantly apparent because the ":" smacks you between the eyes.

(Besides: in Python braces are too valuable to waste. They're needed for something worthwhile - designation of the dictionary data type. Beginners may not know it, but they really want dictionaries.)

Tongue in cheek, then, here's a possible two step program to begin a recovery from braces.

Starting with an arbitrary snippet of original 1970s-language code:

if (x)
  {
   if (y)
     {
      f1()
     }
    f2()
   }

apply a simple brute force translation to yield legal Python syntax:

if (x):
  #{
    if (y):
      #{
        f1()
      #}
    f2()
  #}

We can quit at this point. Python is happy, We're happy. Maybe.

Reduction 1 - in Python the "if" statement knows that a Boolean expression is sure to follow the keyword. So the parentheses around conditionals are just extra typing:

if x:
  #{
    if y:
      #{
        f1()
      #}
    f2()
  #}

Reduction 2 - languages of 1970 had to handle paper tape, and compilers needed help from human beings about where blocks begin and end. If your eyes are happier seeing braces you can code them, as above. Python is happy. We're happy. Maybe.

But Python, since it was designed after the invention of screen editors, knows where your blocks are. (You do indent.) (Don't you?) So why not save some further typing:

if x:
    if y:
        f1()
    f2()

Now everybody's happy. (Aren't we?)

8.2 Tuple Packing

Python uses collections of objects to make your life easier. An important example is shortcuts having to do with tuples. This is legal (and handy):

a, b, c = 1, 2, 3

Tuples are being processed behind the scenes. An especially handy case:

x, y = y, x         # An exchange, in a single statement

Another (more important!) case is an idiom in functions, which in Python can return anything object, including a tuple - i.e., multiple values:

def lookup(employee_id):
  ...
  ...
  return firstname, lastname, phone_number

and the corresponding function call can be like:

fname, lname, pnumber = lookup('123456')

This feature saves all kinds of grief with temptations to misuse globals, passing pointers to structures, and similar dangerous stuff.

8.3 Regular Expressions

Regular expressions are one of the things that many newcomers to Python ask about.

Python has regular expressions, and they are extremely powerful. The literature says that they are equivalent to those in Perl.

Two comments:

  1. For many searching and replacing tasks, Python's string methods will do the job. When they do, writing, debugging, and later understanding are a whole lot easier. See the Python Cookbook for more discussion on this.
  2. When the going is tough you need regular expressions. Unfortunately, when you're using regular expressions the going can get tough - they're cryptic. One things which Python helps with is the ability to compile regular expressions - and your sources can be spread over multiple lines and can include comments.

For example (note the complilation flag re.VERBOSE):

charref = re.compile(r"""
 &[#]                          # Start of a numeric entity reference
 (
   [0-9]+[^0-9]                # Decimal form
   | 0[0-7]+[^0-7]             # Octal form
   | x[0-9a-fA-F]+[^0-9a-fA-F] # Hexadecimal form
 )
""", re.VERBOSE)

Without the verbose setting, this RE would look like:

charref = re.compile("&#([0-9]+[^0-9]|0[0-7]+[^0-7]|x[0-9a-fA-F]+[^0-9a-fA-F])")

This nice example, together with a complete introduction to Python's regular expressions, was written by A.M. Kuchling. [amk]

8.4 CSV

From the Python Library Reference:

12.20 csv -- CSV File Reading and Writing

New in version 2.3.

The so-called CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases. There is no "CSV standard", so the format is operationally defined by the many applications which read and write it. The lack of a standard means that subtle differences often exist in the data produced and consumed by different applications. These differences can make it annoying to process CSV files from multiple sources. Still, while the delimiters and quoting characters vary, the overall format is similar enough that it is possible to write a single module which can efficiently manipulate such data, hiding the details of reading and writing the data from the programmer.

The csv module implements classes to read and write tabular data in CSV format. It allows programmers to say, "write this data in the format preferred by Excel," or "read data from this file which was generated by Excel," without knowing the precise details of the CSV format used by Excel. Programmers can also describe the CSV formats understood by other applications or define their own special-purpose CSV formats.

8.5 Data Types - Highlights

Based on Simon Brunning's "Python Quick Reference" [pqr]

  • Mappings (noted first on purpose, the King of Python's data types:
    • Dictionaries - {firstname: 'Linus', lastname: 'Torvalds'}
  • Sequences:
    • Tuples - ('1', '2', '3', '4')
    • Lists - ['1', '2', '3', '4']
  • Numbers::
    • Decimal integer - 1234, 1234567890546378940L (or l)
    • Octal integer - 0177, 0177777777777777777L (begin with a 0)
    • Hex integer - 0xFF, 0XFFFFffffFFFFFFFFFFL (begin with 0x or 0X)
    • Long integer (unlimited precision) - 1234567890123456L (ends with L or l)
    • Float (double precision) - 3.14e-10, .001, 10., 1E3
    • Complex - 1J, 2+3J, 4+5j (ends with J or j, + separates (float) real and imaginary parts)
  • Strings::
    • "a string enclosed by double quotes"
    • 'another string delimited by single quotes and with a " inside'
    • '''a string containing embedded newlines and quote (') marks, can be delimited with triple quotes.'''
    • """ may also use 3- double quotes as delimiters """
    • u'a unicode string'
    • U"Another unicode string"
    • r'a raw string where \ are kept (literalized): handy for regular expressions and windows paths!'
    • R"another raw string"    -- raw strings cannot end with a \
    • ur'a unicode raw string'
    • UR"another raw unicode"

8.6 Generators

Generators, new in Python 2.2, are a powerful construct for managing control flow. In cases where they are useful, such as gathering and subtotalling several members of an input file, the errors they avoid will leave you in tears for time wasted in your youth using lesser tools.

Too large a topic for a first evening's introduction, generators are nevertheless documented fully in the distribution.

Also, an article by David Mertz, Ph.D., at

http://gnosis.cx/publish/programming/charming_python_b1.html

provides a a good introduction.

9 Some Comparisons/Myths/Confusions

None of these is necessarily a big deal, but they may be "significantly illuminating" (tm).

9.1 Call by ref / call by value

In a Python class I taught last December the question came up, "Does Python pass by value or by reference?"

The question stymied me. I tried "by value" and came up with contradictions and counterexamples. I tried "by reference" with the same result.

The correct answer is that the question is phrased in terms of other languages, and doesn't really have meaning for Python. What's important is binding of names, and namespaces, and mutability of objects. A different design philosophy.

Good material for another evening's Python talk.

9.2 OO Versus BASIC

OO languages or at least a few of them (e.g., C++) have given OO a bad rap. It is not OO itself which is large and kludgy, it's the implementation.

You don't need to be put off by the fact that Python is object oriented from its foundations up, for two reasons.

  • Python's OO facilities are gracefully done, easy to use, and easy to learn. They have none of the cruft required by C++ (much of which derives from C++'s need to be backward compatible with C).
  • Better still, you don't have to use OO at all to get good value from Python. One of Python's strengths is that you have things like the print statement, and the ability to do simple jobs simply. Or even difficult jobs simply.

9.3 Dictionaries

Python's "Dictionary" mapping data type was one of the things I was slow to catch on to.

Integers and strings seemed obvious.

Tuples and lists took a little work, but they became obvious quickly.

I left dictionaries until last, probably because they seemed complicated and frought with overhead.

This was a mistake, and a beginner is well counseled to make the acquaintance of dictionaries early on. They are immensely powerful. They are also highly optimized, and are used as the backbone of Python's internals. (Namespaces are implemented as dictionaries, for example.) See the Python Cookbook for a discussion.

9.4 Performance

Python's performance varies with the application.

Some things are slow, and some things can be implemented to run faster using a low-level language.

But I've used Python for years, and only once had to change code to improve performance. And even then the fix was to write smarter Python code, not resort to assembly language.

Actually, I've never heard of a project which failed to deliver the goods because of performance issues. I've heard of many projects which failed because the development couldn't get done in time, however.

10 Recap

I hope this tiny selection of Python topics has illustrated Python's productivity, pleasure of use, subsequent maintainability, and incredible rapidity.

I hope it has whetted your appetite to make a better acquaintance of Python.

11 Appendices

11.1 Appendix A - "It Fits Your Brain"

"It Fits Your Brain" - Python 9 Keynote, in Linux Journal
Bruce Eckel is the person who invented the "Python Fits Your Brain" slogan as well as last year's "Life is Better Without Braces". He's also a well-known C++ and Java design consultant. In his closing keynote, Bruce presented the top ten reasons why he loves Python. [Eckels01]

--

  1. Reduced clutter.
  1. It's not backward-compatible with other languages. (This came with some hilarious one-liners: "C++'s backward compatibility with C is both its strength and its bane"; "Java causes repetitive-strain syndrome"; "Perl is compatible with every hacky syntax of every UNIX tool ever invented"; "C# and Microsoft .NET are backward-compatible with Microsoft's previous marketing campaigns"; and "Javascript is not even compatible with itself".)
  1. It doesn't value performance over my productivity.
  1. It doesn't treat me like I'm stupid. Java insists operator overloading is bad because you can make ugly code with it. Bruce observes, "And we all know there's no ugly Java code out there."
  1. I don't have to wait forever for a full implementation of the language.
  1. It doesn't make assumptions about how we discover errors.
  1. Marketing people are not involved in it (yet).
  1. I don't have to type so much. But what I do type is the right typing.
  1. My guesses are usually right.
  1. Python helps me focus on my concepts rather than on fighting with the language.

11.2 Appendix B - The Zen of Python

by Tim Peters [PetersZ]

  • Beautiful is better than ugly.
  • Explicit is better than implicit.
  • Simple is better than complex.
  • Complex is better than complicated.
  • Flat is better than nested.
  • Sparse is better than dense.
  • Readability counts.
  • Special cases aren't special enough to break the rules.
  • Although practicality beats purity.
  • Errors should never pass silently.
  • Unless explicitly silenced.
  • In the face of ambiguity, refuse the temptation to guess.
  • There should be one-- and preferably only one --obvious way to do it.
  • Although that way may not be obvious at first unless you're Dutch.
  • Now is better than never.
  • Although never is often better than right now.
  • If the implementation is hard to explain, it's a bad idea.
  • If the implementation is easy to explain, it may be a good idea.
  • Namespaces are one honking great idea -- let's do more of those!

11.3 Appendix C - A Parable

Edsger W. Dijkstra [EWD594]

(Recently I found the following text in manuscript among old papers of mine. It must have been written in the middle of 1973, but I don't think that in the intervening three years it has lost anything of its significance. Hence I now incorporate it in the EWD-series.)

A parable.

Years ago a railway company was erected and one of its directors --probably the commercial bloke-- discovered that the initial investments could be reduced significantly if only fifty percent of the cars would be equipped with a toilet, and, therefore, so was decided.

Shortly after the company had started its operations, however, complaints about the toilets came pouring in. An investigation was carried out and revealed that the obvious thing had happened: despite its youth the company was already suffering from internal communication problems, for the director's decision on the toilets had not been transmitted to the shunting yard, where all cars were treated as equivalent, and, as a result, sometimes trains were composed with hardly any toilets at all.

In order to solve the problem, a bit of information was associated with each car, telling whether it was a car with or without a toilet, and the shunting yard was instructed to compose trains with the numbers of cars of both types as equal as possible. It was a complication for the shunting yard, but, once it had been solved, the people responsible for the shunting procedures were quite proud that they could manage it.

When the new shunting procedures had been made effective, however, complaints about the toilets continued. A new investigation was carried out and then it transpired that, although in each train about half the cars had indeed toilets, sometimes trains were composed with nearly all toilets in one half of the train. In order to remedy the situation, new instructions were issued, prescribing that cars with and cars without toilets should alternate. This was a move severe complication for the shunting people, but after some initial graumbling [sic], eventually they managed.

Complaints, however, continued and the reason turned out to be that, as the cars with toilets had their toilet at one of their ends, the distance between two successive toilets in the train could still be nearly three car lengths, and for mothers with children in urgent need --and perhaps even luggage piled up in the corridors-- this still could lead to disasters. As a result, the cars with toilets got another bit of information attached to them, making them into directed objects, and the new instructions were, that in each trains [sic] the cars with toilets should have the same orientation. This time, the new instructions for the shunting yard were received with less than enthusiasm, for the number of truntables [sic] was hardly sufficient; to be quite fair to the shunting people we must even admit that according to all reasonable standards, the number of turntables was insufficient, and it was only by virtue of the most cunning ingenuity, that they could just manage.

With all toilets equally spaced along the train the company felt confident that now everything was alright, but passengers continued to complain: although no passenger was more than a car length away from the nearest toilet, passengers (in urgent need) did not know in which direction to start their stumbling itinary [sic] along the corridor! To solve this problem, arrows saying "TOILET" were fixed in all corridors, thereby also making the other half of the cars into directed objects that should be properly oriented by the shunting procedure.

When the new instruction reached the shunting yard, they created an atmosphere ranging from despair to revolt: it just couln't [sic] be done! At that critical moment a man whose name has been forgotten and shall never be traced, made the following observation. When each car with a toilet was coupled, from now until eternity, at its toileted end with a car without a toilet, from then onwards the shunting yard, instead of dealing with N directed cars of two types, could deal with N/2 identical units that, to all intents and purposes, could be regarded as symmetrical. And this observation solved all shunting problems at the modest price of, firstly sticking to trains with an even number of cars only --the few additional cars needed for that could be paid out of the initial savings effected by the commercial bloke!-- and, secondly, slightly cheating with regard to the equal spacing of the toilets. But, after all, who cares about the last three feet?

Although at the time that this story took place, mankind was not blessed yet with automatic computers, our anonymous man who found this solution deserves to be called the world's first competent programmer.

--

I have told the above story to different audiences. Programmers, as a rule, are delighted by it, and managers, invariably, get more and more annoyed as the story progresses; true mathematicians, however, fail to see the point.

Platasnstreat 5 prof.dr.Edsger W. Dijkstra NL-4565 NUENEN Burroughs Research Fellow The Netherlands

12 Reading Recommendations

For further reading, these are Bill's selections made as though the list could contain only a few entries. (There are many, many good Python references.) This tiny list emphasizes one entry each from a physical book and from the distro or the 'net.

12.1 Tutorial Reading

http://www.python.org/doc/Newbies.html
Wherein are a number of good links, including a link to the "official" Tutorial (which is as good a starting place as you'll find. It only takes an hour or two!)
Learning Python, Lutz and Ascher, O'Reilly
A good first book on Python, as is the custom with O'Reilly's Learning series.

12.2 Intermediate Reading

Dive Into Python, http://diveintopython.org/
An introduction to Python for programmers experienced with other languages.
Python Quick Reference, http://www.brunningonline.net/simon/python/PQR.html
A handy, very condensed online/downloadable HTML reference. (Limitation: current only as of Python 2.1)

12.3 Reference Reading

The Python distribution's own reference pages
These pages are the reference. HTML, accurate, up-to-date (with every release! How do they do it?) Well indexed for on-line use.
Python in a Nutshell, Alex Martelli, O'Reilly
Concise, no-condescension, complete description of Python.

12.4 Advanced Reading

Python Cookbook, Martelli, Ascher, and the Python community.
Not unlike the Perl Cookbook. In a class by itself, for the quality of problem solving it encourages, and for the spirit and number of contributors who made it possible. In a class by itself. Indispensable.

13 Credits, References

The "Fount of All Things Python" is: http://www.python.org/

Personal credits for the stuff in this presentation:

References:

[EWD594]A Parable, Dijkstra, http://www.cbi.umn.edu/collections/inv/burros/ewd594.htm
[Eckels01]From Conversations: It Fits Your Brain: the Ninth Annual International Python Conference, covered by Mike Orr in Linux Journal, April 25, 2001; http://www.linuxjournal.com/article.php?sid=4731
[GvR96]Guido van Rossum, http://www.python.org/doc/essays/foreword.html
[amk]A.M. Kuchling, Regular Expression Howtwo, http://www.amk.ca/python/howto/regex/
[Mattison86]Snakes of the World, Chapter 8, "Snakes and Man"; Christopher Mattison, Facts On File, Inc, New York, 1986, ISBN --8160-1082-X
[PetersZ]The Zen of Python, by Tim Peters; http://www.python.org/doc/Humor.html#zen
[leo]The Leo outlining editor, http://personalpages.tds.net/~edream/front.html, http://sourceforge.net/projects/leo
[pqr]Python Quick Reference (for version 2.1), by Simon Brunning, http://www.brunningonline.net/simon/python/PQR.html
[pycomps]http://www.python.org/doc/Comparisons.html
[pysum](1, 2) http://www.python.org/doc/Summary.html
[SciTE](1, 2) http://www.scintilla.org/SciTE.html