Home » Writings » Python » Introduction to the Python Programming Language

Last updated: 2004.05.20

Python is a nice little scripting language that is well designed. That means that its features and syntax actually make sense. Here are some notes on using Python. Additional information can be found at Python.org, the home of Python.

One of the coolest little Python software out there is the edna MP3 server. It really shows what can be done in Python. Edna also contains my first open source software work, even if it is a tiny tiny bit of a patch to the software !

What's cool about Python

Some articles that will help you learn Python

The Language

This is a quick summary of language features:

None or Null

There is a single global object, call None, that is useful for indicating null or nothing.

Data Type Conversions

int(string) -> int
float(string) -> float
str(int) -> str
str(unicode_str)->str
list(imatable_tuple)->list

Loop

Q: How do I write for x = 0 to 9?

A: for x in range(0,10): print x

Lists

List are represented by square brackets:

[1,2,3,6,7] is a list of integers
[ (a,1), (c,3)] is a list of tuples

List Initialization

To create/initialize a list, use the repeat operator on lists.
For example, initialize a list of length 10, with zeros:

L1 = [0] * 10

List Concatination

L1.extend(L2)
is the same as:
L1[len(L1):] = L2

List Copying, cloning, aliasing

L1 = [1,2,3]
L2 = L1[:]

note:
L2 = L1 just makes L2 an alias of L1, i.e. L1 and L2 are references pointing to the same actual list object -- dangerous.
prove that by doing
print id(L1), id(L2)
you will see both id's are the same.

Use tuples if your objects can be immutable !

List Sorting

L1.sort()
sorts it in place.

Lambda Function

The lambda function is a great tool for writing simple function in line. Typically you can use this with the map and filter functions. [tbw]

Map and Filter

The filter function let you apply a functional filter to a list. The map function let you transform a list. [tbw]

Slicing

a = '123456'
a[start:end]
a[0] = '1' # note: starts from zero
a[5] = '6'
a[0:6] = '123456' # note: end is non inclusive
Cool stuff: negative values can be used in both start and end index:
a[0:-1] = '12345'
Leaving out index means beginning or end
a[:-1] = '12345'

Tuples vs Sequences

Tuples are immutable.

File Processing

Reading a File

Reading text file is slow if you read the file line by line. The prefered method is to use the readlines() method to read the entire file into memory:

f = open("path/to/filename")
   lines = f.readlines()
   for line in lines:
       # do something with line
       line = line[:-1] # remove newline at the end

If the file is too large, you can use the optional size hint argument to tell readlines to read complete lines from the file, but not to use up all memory:

 

Dictionary

A dictionary is setup using the {} braces. dictionary = {'key1':'data1', 'key2','data2' }

Database

One big problem with Python is that it is not as easy to setup to connect to real databases (like Oracle) then P**l. So let's focus on something that Python does well -- a simple object database using the shelve.py module. Or use the more elaborated ZODB, part of the ZOPE web application platform. ZODB is available as a standalone package for use outside of ZOPE.

Shelve.py

This module lets you store Python objects in a disk based database. It uses the underlying anydbm.py module to store the data, after converting the object to a string using pickle.py.

Cool Python Apps

Be careful with default arguments in function

i = 9

def f(n=i):
      print 'in f(), n=',n

f(1)
f()
i=2
f()

This will produce:

in f(), n= 1
in f(), n= 9
in f(), n= 9  <=== not 2

This is because the default argument referencing the global "i" is only evaluated once when the parser parses the function definition. You have to set the default to None and test for None to get the more logical behaviour:

i = 9

def f(n=None):
      if n==None:
           n=i
      print 'in f(), n=',n

f(1)
f()
i=2
f()

This will produce:

in f(), n= 1
in f(), n= 9
pin f(), n= 2

Unicode and XML

The xml library returns unicode strings, which may upset string comparison?

Be careful with Tuples of Strings Constants

To specify a sequence/tuple/list of strings with a single member, don't forget the trailing comma, otherwise, due to the wonderful semantics of Python, you ended up passing in a list of characters in the case of tuples:

def i_want_a_list(mylist):
     for e in mylist:
          print e

i_want_a_list( ('this is wrong'))
i_want_a_list( ('this is right',))
i_want_a_list( ['this is ok'])
i_want_a_list( ('this is ok also',])


Problems with Python

python is a great language, but why is it still so much less popular that the other P* languages (Perl, PHP)?

  1. Lack of tutorial materials.
  2. Lack easy database integration -- you cannot download a Oracle driver without going through some big compile and build step.

Some quick Tips:

Q: 	How do I convert a string to a list?
A:	Use the split function to split the string. You can also specify
	the separator (defaults to whitespaces) by calling split
	with an addition argument specifing a single character for used
	as the separator.

	import string

	str = 'this is a string of words'
	mylist = string.split(str)
	print mylist


Q:  How do I parse command line arguments?
A:	Use the getopt module. It works like the Unix getopt function.
	You specify a list of options and getopt.getopt will parse them
	out and return the list in a list.
	You should catch the getopt.error exception to handle bad
	arguments:

	import sys, getopt

	# note: use argv[1:] to skip the name of the script
	# parses -d for debug, -f to specify a file.
	try:
		opts, args = getopt.getopt(sys.argv[1:],"df:")
	except getopt.error:
		print "usage: cmd -d -f 


Q:	How do I use the calendar module?
A:	For some reason the calendar module is not documentd.

Q:  How do I know what exception was thrown?
A:  use:
     (t,v) = sys.exc_info[:2]
     print '%s %s' % (t,v)

     or traceback.print_exc()

     They get you to the sys.exc_type and sys.exc_value value.



Q:  How do I read a file and convert the content to a list,
	but drop the annoying end of line character?
A:
     f = open('filepath')
	 lines = map(lambda s: s[:-1],f.readlines())
	 return lines

Q:  How do I get a list of file names from a directory,
	and filter the file name?
A:	The filter functin is usefule here:

	names = dircache.listdir('/somedirectory/')
	# Now filter file names using regular expression
	# only take java source files
	names = filter(lambda f: re.match('^[a-z][A-Z].*\.java',f),names)

Q:  How do I run system commands?
A:  Use the commands module, which will return the output
	as well as the return code:

    (status,output) = commands.getstatusoutput('some command')

Q:  How do I setup Peer Web Server in NT to use Python to write cgi?
A:  Use regedit and enter the following string value:
	at:
		HKEY_LOCAL_MACHINE
			System
				CurrentControlSet
					Services
						W3SVC
							Parameters
								Script_Map
		Add: ".cgi" = "c:\Program Files\Python\Python.exe %s %s"

		Adjust path accordingly.