Charles Engelke's Blog

July 26, 2004

Python Quickstart

Filed under: OSCON 2004 — Charles Engelke @ 2:22 pm

My first session at this year’s
Open Source
here in Portland, Oregon, is a
. I’m not really looking to use
Python for anything,
but I’d like to be able to program in it. After all, it’s the
language that Zope and
Chandler are written
in, and I think both those products are very good.

I’m disappointed that we don’t have any written materials for this
session. Maybe the presenter, Paul
will post them on the web, or they may show up at the
web page.

I’ve got the Cygwin Python
interpreter on my PC, so I’ll try to do this hands-on. A weird
thing about this interpreter is that it won’t run directly from a
Windows command shell; I’ve got to start bash and run it from

We start with claims on how good Python is: easy to learn, efficient,
high-level data structions, object-oriented, with an elegant
syntax and dynamic typing. It was invented by Guido van Rossum
in the early 90s. It was intended to be scalable and object-oriented
right from the beginning, rather than just a scripting language.
Python has been used by Google since the beginning. NASA and
Industrial Light & Magic use it, too. Python may well be available
in future Nokia phones.

Python wants there to be a single obvious way to do most things (as
opposed to Perl’s philosophy of “There’s more than one way to do it”).
It’s only innovative where the language designers couldn’t find good
ideas to steal for the language! Python’s available on most
operating systems (it comes included with Mac OS/X). There’s even
a Python interpreter than runs on the Java virtual machine.

Start the Python interactive shell by just typing “python” at the
command line. Then type commands and they’ll execute immediately.
If you just type an expression, you’ll get the result of the
expression displayed (so you don’t have to type “print expression”
all the time). Combine multiple statements on one line by
separating them with semi-colons. (They’re a separator as in Pascal,
but you almost never need them because ends of lines serve the same
purpose. Continue statements over multiple lines by ending each
line with a backslash. If a line end occurs in the middle of a
quoted or parenthesized expression, you don’t need the backslash.)
Finally, Python uses indentation for scoping, instead of curly
braces or keywords. Oddly enough, based on my test just now, you
can’t indent lines that don’t require them, or you get a syntax
error. Tabs cause confusion; don’t use them in Python source if you
don’t want to get confused.

Python’s dynamic typing means that variables don’t have types, but
objects have types. For example, if you assign x the value 10,
the variable x isn’t an integer, but its value is an object that is
an integer. You can assign a string to x next, since the variable
can contain an object of any type. (That’s my interpretation of
what he just said.) How is this different from Perl? In a lot of
ways; here’s a simple example: send the string “10” to a math
function. In Perl, the string will just be treated as if it were
the integer 10, but in Python it’s an error. By the way, error
messages always include a complete traceback of the calling tree.

We’re still hearing about Python, instead of seeing Python
itself. I’m ready to move beyond the evangelizing!

Okay, finally… Start with Python types. There’s int,
float, complex (using j instead of
i for the imaginary part; probably had an electrical engineer create
that specification), and bool
(which is 1 or 0; same as True or False). Watch out for integer
division: 5/2 is 2 (since it returns an integer when given two integers). To warn the
programmer that’s what happens, use an explicit division operator,
as in 5//2. (If you don’t want truncating division, make sure that
at least one of the operands is a float.) (There used to be a “long”
version of int, but the latest versions just treat them both the
same way.)

Everything in Python is an object. Not just integers, characters,
floats, and so on, but also functions and methods. Objects have
types (print type(object) will show it) and a unique identifier
(print id(object) to see it). Note that a literal value (like 1)
when repeated might be the same object or different objects with
the same value.

Attributes are information attached to objects. For example,
if x is 1+2j, then x.real is 1. Methods are attributes that are
functions (x.conjugate() is 1-2j).

None is an object that represents no value (similar to NULL). You
can assign to to variables to keep track of that.

Strings can be in single or double quotes, so you can embed one type
of quote inside a literal string that’s enclosed in the other kind
of quote marks. You can escape quotes with a backslash, too. You
can enclose multiline strings with triple quote marks. String
concatenation uses the + symbol. You can interate over the
characters in a string with a for loop. The
str function converts any object into a string.
String methods include replace, capitalize, title (case), upper.

Lists are a lot like Perl, except that they can contain other lists
(and are written enclosed in square brackets). You can iterate over
lists with a for loop, just as you can iterate over characters in
a string. Lists and strings are both sequences in Python.

Well, that’s the break, and I’m switching sessions. This one is
too slow, and too elementary to be valuable. I’m going to
the Best
Practice Perl
session. I’m probably not going to blog it, though.

Well, maybe I will blog it. Here’s a cool tip: use the values
function to iterate over and change all values in a hash:
foreach (values %h) {$_++}.

Use List::Util and Scalar::Util as if they were built-ins.


Blog at

%d bloggers like this: