Python Notes

Python is a free programming language that many feel is a perfect first language. Features easy syntax, from simple scripts, to multi-media GUI applications and games. It's available for Linux, Windows, Macintosh, and probably others.

Python.org

Python.org is the offical site, which includes a download link for the interpreter and several tutorials for the new as well as experienced programmer. These pages are only my own notes.

Error "undefined symbol:PyFPE_jbuf"
Could be an unlinked version of python on the system. If /usr/bin and /usr/local/bin contain python, delete the /usr/local/bin/python, and re-log in. Debian puts python in /usr/bin, but python.org puts it in /usr/local/bin. Confusing, but if you are using apt-get, stay with Debian's version.

Key Python Debian packages are python2.2, python2.2-dev,python2.2-doc, python2.2-mysqldb, python2.2-tk.
With Debian3 (woody), don't remove python2.1, it's required/linked by many system apps. Version 2.2 peacefully co-exsists.

Keywords (cannot be used as variable names):




and = (boolean)
assert = (debugging)
break = (loop)
class = evaluates lists and defines a class object.
close() = closes an open file. See open.
continue = (loop) continues next cycle of the nearest enlosing loop.
ctrl-d = quit python command line in Linux/UNIX.
ctrl-z = quit python command line in Windows.
def = defines a user object.
del = removes items.
elif = (loop) part of " if ".
else = (loop) part of " if ".
except = (loop) part of " try ".
exec = dynamic execution of python code.
finally = (loop)part of " try ".
for = (loop) iterate over the elements of a sequence.

from
global
help() = interactive help. (help, modules, topics)
if = (loop) evaluates expressions one by one until one is true, then executes it.
import = insert modules.
in
input('print prompt') = takes in user's input.
is
lambda = (boolean)
not = (boolean)
open("filename","r") = open a file. "r" = read "w" = write, "rb" = read binary, "wb" = write binary.
or = (boolean)
ord() = convert opened file into printable format.
outp = create a new file to write to. See inp.
pass
path = #! /usr/bin/python.
print = write to output (the screen by default).
raise
readline(): = read the opened file (see imp) one line at a time.
readlines(): = read the entire opened file (see imp).
return
try = specifies exception handlers &/or cleanup for a group of statements.
while = repeated execution as long as an expression is true.


Operands:


() = Parentheses
** = exponent
* = multiply
/ = divide
+ = add
- = subtract
order = "Please Excuse My Dear Aunt Sally"
% = modulo operator (prints remainders).
output formats:
%d = decimal number
%s = string
%x = hexadecimal
%4d = pad number out to 4 digits.
%0.2d = 2 figures after decimal


Escape Sequences

\ = start escape
\\ = slash, \n = newline, \t = tab, \000 = octal character.


Collections


list = [item1, item2] editable collection.
tuple = (item1, item2) uneditable collection.
dictionary = {item1, item2} aka " hash " = indexed list.
array = fixed sized index.
stack = LIFO, last in first out.
queue = FIFO, first in first out.
set = one of each item with properties like memberships.

Modules (aka libraries)

import modulename = bring module into current program.
dir() = view module contents.
dir(__builtins__) = view defalt python module contents. Note double underscore.
A few modules: sys, os, string, math, time, sockets, cgi, Tkinter, dbm.
1