Python should be available on both SG and Linux workstations in the Berry Patch (slate, granite, capri, quartz, talc; psyche, calypso, circe, calliope, orestes, theseus, electra, alcestis, cleo, medea, antiope, penelope, iphitus, omphale). To run Python on any CS machine, type python at the prompt.
To use Python from xemacs:
.py to invoke Python
mode..py
buffer) executes the current buffer in the Python interpreter
process.The official Python language website is www.python.org.
You can download a copy to your PC from http://www.python.org/download/
The following Python tutorials are available on the web:
/usr/doc/python-docs-1.5.2/cheatsheetThe Python documentation, along with links to more tutorials, is available at http://www.python.org/doc/.
You might find especially useful the
Python Library Reference.
Python is an interpreted language implemented mostly in C/C++.
The Python interpreter provides a command-line interface.
You can invoke the interpreter as follows:
In interactive mode, the interpreter can be used as calculator:
If you prefer to enter the interactive mode after your file has been
executed, use:
To exit from the interpreter:
In order to access command line arguments, you need to import the
appropriate variable from the module
You can import standard modules or your own files. To import the
whole module:
To import a particular variable or function:
Indentation after a colon is used to mark blocks:
Comments start with '#', as in Perl.
Variables don't have types, so no variable declarations are necessary.
Numeric types are standard, except long integers have unlimited
precision in Python:
Python has the following built-in data types:
The first 3 types are sequence types, and are basically similar to
arrays in other programming languages. Strings are arrays of
characters. Tuples and lists are arrays of arbitrary values.
Consequently, tuples and lists can be nested. Strings and tuples
are immutable. That is, e.g. unlike in C, you can not assign
Lists are mutable.
All standard escape sequences apply to Python strings:
Sequence types are indexed as arrays:
You can obtain slices of a given sequence with the slicing operator
":". The second argument is non-inclusive,
i.e. Slices of any sequence type can be concatenated using "+":
Lists are mutable, i.e. you can modify any element of the list and
also modify slices of lists:
Also, the List data type has methods such as
Dictionaries are associative arrays or hashes. Any immutable Python
object can be used as a key in a dictionary.
The following values are false in Python:
All other values are true (except for instances of user-defined
classes, if that class defines a __nonzero__() method or a __len__()
method, and that method returns zero)
Boolean operations:
For sequences, there is a membership testing operator
You can also compare sequence objects of the same type (using
lexicographic order):
All the standard loop and conditional constructs are present:
A Perl loop that iterates over the elements of an array
If you wish to iterate over a list that is being changed within
the loop, iterate over its copy:
In Python, one customarily uses
A standard C/Java/Perl style loop
These are used just like in C. Else clause on loops is executed
only if the loop was not exited through break:
Python allows default arguments:
By the way, Python allows function objects to returned or stored in
variables:
Printing in Python is easy:
Note that in the above writing `x` is the same as str(x), the
latter being an builtin function. So
You can also build strings with standard C-style format strings:
Some common commands for dealing with file objects:
Always compile regular expressions you are going to use
more than once .. it saves time.
So, instead of
(1) Methods
(2) Match Objects
(3) Match Object methods and fields:
Example:
(4) Raw strings
Python's raw string notation for regular expression patterns:
<- backlashes are treated as literals in these, i.e.
Class and instance variables and methods can be accessed through
internal hashtables. Each class has a dictionary that stores class
members. Each instance has a dictionary for instance variables.
Normally, you do not need to use this way of accessing class and
instance variables, but sometimes it may be useful.
You can also test in this manner the type of object, i.e. whether it's
a list, a tuple, a string, etc.
A Quick Intro to Python
Invoking the interpreter
[your_handle@cleo ~]$ python
Python 1.5.2 (#1, Feb 1 2000, 16:32:16) [GCC egcs-2.91.66
19990314/Linux (egcs- on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> _
>>> a = 3
>>> a
3
>>> 3*3
9
.py. To run a file with your code:
[yourhandle@cleo ~]$ python <filename>.py <arg1>
<arg2>
[yourhandle@cleo ~]$ python -i <filename>.py <arg1>
<arg2>
chmod u+x <filename>), and add the
following standard shell instruction to the top of your file:
#!/usr/bin/python
C-d
sys.
Importing modules
>>> import sys
>>> sys.argv
[<filename>.py, <arg1>, <arg2>]
>>> from sys import exit
>>> exit()
[yourhandle@cleo ~]$ _
Basics
if a < 0:
print "a is negative"
else:
print "a is non-negative"
Variables and Built-in Data Types
>>> a = 44444444444444444422222222222222222444444444444444444L
>>> a * a
1975308641975308640000000000000000020246913580246913530864197530864197599999999999999999802469135802469136L
>>> a = "There is no \"place\" like home"
>>> a
'There is no "place" like home'
>>> b = "There's no place like home"
>>> b
"There's no place like home"
>>> c = 'There\'s no place like home'
>>> c
"There's no place like home"
>>> a = "blah\tblah"
>>> a
'blah\011blah'
>>> print a
blah blah
>>> alist = ['There', 'is', 'no', 'place', 'like', 'home']
>>> blist = ['There', 0.34]
>>> clist = [['There', 0.34], ['is', 0.57]]
>>> dlist = [('There', 0.34), ('is', 0.57)]
>>> atuple = ('There', 0.34)
>>> btuple = ('bigram', [('There', 'is'), 0.34])
>>> a
'There is no "place" like home'
>>> a[0]
'T'
>>> a[1]
'h'
>>> alist
('There', 'is', 'no', 'place', 'like', 'home')
>>> alist[0]
>>> btuple
('bigram', [('There', 'is'), 0.34])
>>> btuple[1]
[('There', 'is'), 0.34]
alist[m:n] gets you the portion of the list
containing all elements beginning with position m and
ending with position n-1. Either argument can be
omitted.
>>> a[:4]
>>> alist
['There', 'is', 'no', 'place', 'like', 'home']
>>> alist[0:3]
['There', 'is', 'no']
>>> alist[:3]
['There', 'is', 'no']
>>> alist[2:]
['no', 'place', 'like', 'home']
>>> a
'There is no "place" like home'
>>> a[:4]
'Ther'
>>> (1, 2) + (3, 4)
(1, 2, 3, 4)
>>> alist = [1, 2, 3, 4, 5]
>>> alist[0] = 'a'
>>> alist[1:3] = ['b', 'c']
>>> alist
['a', 'b', 'c', 4, 5]
append(x),
insert(i, x), index(x), sort(), etc.
>>> a = ['There', 'is', 'no', 'place', 'like', 'home']
>>> a.sort()
>>> a
['There', 'home', 'is', 'like', 'no', 'place']
>>> adict = {}
>>> bdict = {"home": [1.12, 2.34], ('home', 'NN'): .45}
>>> bdict[('home', 'NN')]
0.45
>>> bdict['home']
[1.12, 2.34]
Truth Values and Boolean Operations
None
not, and, or
>>> a = ''
>>> b = 32
>>> a or b
32
>>> a or not b
0
in:
>>> b = 32
>>> b in [21, 32]
1
>>> (1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
1
Control Flow
if
if not a == 0:
print b/float(a)
else:
print "Attempted division by 0!"
while
while (a % 2 == 0):
a = a/2
for
translates into Python as:
foreach $elem (@myarray) {..}
myarray = [e1, e2, e3]
for e in myarray:
<do the loop commands>
myarray = [e1, e2, e3]
for e in myarray[:]:
<do the loop commands>
range() function
for the loop variable in for loop.
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(5, 10)
[5, 6, 7, 8, 9]
>>> range(0, 10, 3)
[0, 3, 6, 9]
>>> range(-10, -100, -30)
[-10, -40, -70]
translates into Python as:
for(i = 0; i < n; i++) {
<do the loop commands>
}
for i in range(n):
<do the loop commands>
>>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print n, 'equals', x, '*', n/x
... break
... else:
... print n, 'is a prime number'
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
Functions
>>> def modsum(a, b, c = 10):
... return (a+b)%c
...
>>> modsum(22, 33)
5
>>> def square(x): return x*x
>>> def cube(x): return x*x*x
>>> def compose(f1, f2):
... return lambda x, f1=f1, f2=f2: f1(f2(x))
>>> f = compose(square, cube)
>>> f(2)
64
Reading and writing files
>>> x = 10 * 3.14
>>> y = 200 * 200
>>> s = 'The value of x is ' + `x` + ', and y is ' + `y` + '...'
>>> print s
The value of x is 31.400000000000002, and y is 40000...
is the same as
>>> s = 'The value of x is ' + `x` + ', and y is ' + `y` + '...'
>>> s = 'The value of x is ' + str(x) + ', and y is ' + str(y) + '...'
>>> x = math.pi
>>> a = '%f %f %f' % (x, x*x, x*x*x)
>>> print a
3.141593 9.869604 31.006277
>>> f=open('/tmp/workfile', 'w')
>>> f.read()
'This is the entire file.\n'
>>> f.readline()
'This is the first line of the file.\n'
f.write(string) writes the contents of string to the
file, returning None.
>>> f.write('This is a test\n')
f.close() is used to... close the file.
Some relevant modules
string
string.split(str, sep)
>>> from string import split
>>> split("There is no place like home")
['There', 'is', 'no', 'place', 'like', 'home']
re
Using regular expressions in Python:
Use
result = re.search("
Always use re.search() or p.search(), unless you're matching from the
start of the string
pat = re.compile("
compile("<pattern>")
- useful flags are I or IGNORECASE - all patterns are case insensitive
S or DOTALL - dot also matches newlines
# flags seem absent from 1.5.2 (!)
search()
match()
findall()
- finds all nonoverlapping matches of pattern in a string
finditer() # seems absent from 1.5.2
- returns iterator over results of findall()
split(<pattern>, <string>)
- returns list
escape(
compile() returns a Regular Expression Object, with the same
methods as re module.
search() returns a Match Object.
group([group1, ...])
Returns one or more subgroups of the match. If there is a single
argument, the result is a single string; if there are multiple
arguments, the result is a tuple with one item per argument.
groups([default])
Return a tuple containing all the subgroups of the match, from 1
up to however many.
span([group])
For MatchObject m, return the 2-tuple (m.start(group), m.end(group)).
- The above three functions default argument to 0, i.e. the
whole match, rather than specific groups in the match indicated
with '(' and ')' in the regular expression
re
- Regular expression object whose match() or search() method
produced this MatchObject instance.
string
- string on which searching was performed
>>> m = re.search("(n't|'ve|'ll|'er|'em|'re)", "I haven't noticed")
>>> m.groups()
("n't",)
>>> m.group(1)
"n't"
>>> m.span(1)
(6, 9)
>>> m.string
"I haven't noticed"
>>> m.re
r"<string>"
r'<string>'
r"\n"
REF: http://www.python.org/doc/current/lib/node99.html
Classes
__init__(self)
>>> class Boo:
... def __init__(self):
... pass
>>> a = Boo()
>>> class Boo:
... myClassField = 'some value'
... def __init__(self):
... self.myField = (1,2,3)
>>> a = Boo()
>>> a.__dict__['myField'] = (1,2,3)
>>> Boo.__dict__
{'myClassField': 'some value', '__doc__': None, '__module__': '__main__', '__init__':
>>> isinstance(a,Boo)
1
>>> types.ListType
<type 'list'>
>>> isinstance([], types.ListType)
1
Misc. remarks
>>> def group(*choices): return '(' + string.join(choices, '|') + ')'
>>> group('a','b','c')
'(a|b|c)'
>>> a = 3
>>> eval('a')
3