With the final release of Python 2.5 we thought it was about time Builder AU gave our readers an overview of the popular programming language. Builder AU’s Nick Gibson has stepped up to the plate to write this introductory article for beginners.
Python is a high level, dynamic, object-oriented programming language similiar in some ways to both Java and Perl, which runs on a variety of platforms including Windows, Linux, Unix and mobile devices. Created over 15 years ago as an application specific scripting language, Python is now a serious choice for scripting and dynamic content frameworks. In fact it is being used by some of the world’s dynamic programming shops including NASA and Google, among others. Python is also the language behind Web development frameworks Zope and Django. With a healthy growth rate now could be the perfect time to add Python to your tool belt. This quickstart guide will give you an overview of the basics of Python, from variables and control flow statements to exceptions and file input and output. In subsequent articles I’ll build upon this foundation and offer more complex and specific code and advice for using Python in real world development.
Why learn Python?
- It’s versatile: Python can be used as a scripting language, the “glue” that sticks other components together in a software system, much in the same way as Perl. It can be used as an applications development language, just like Java or C#. It can be used as a Web development language, similiarly to how you’d use PHP. Whatever you need to do, chances are you can use Python.
- It’s free: Python is fully open source, meaning that its free to download and completely free to use, throw in a range of free tools and IDE’s and you can get started in Python development on a shoestring.
- It’s stable: Python has been around for more than fifteen years now, which makes it older than Java, and despite regular development it has only just has reached version 2.5. Any code you write now will work with future versions of Python for a long time.
- It plays well with others: It’s easy to integrate Python code with C or Java code, through SWIG for C and Jython for Java, which allows Python code to call C or Java functions and vice versa. This means that you can incorporate Python in current projects, or embed C into your Python projects whenever you need a little extra speed.
- It’s easy to learn and use: Python’s syntax closely resembles pseudo code, meaning that writing Python code is straightforward. This also makes Python a great choice for rapid application development and prototyping, due to the decrease in development time.
- It’s easy to read: It’s a simple concept, a language that is easy to write should also be easy to read, which makes it easier for Python developers to work together.
Okay, enough already, I’m sold. Where do I get it?
Easy, in fact if you’re running Mac or Unix, chances you’ve already got it, just pull up a terminal and type “python” to load up the interpreter. If you don’t have it, or you’re looking to upgrade to the latest version head on over to the download page.
Alternatively you could install ActivePython, a binary python distribution that smooths out many of the hassles. There is a graphical installer under most platforms, all that you need to do is click through the dialogues, setting the install path and components. In Windows, you can then start up the interpreter by browsing to its entry in the start menu, or on any system simply by typing ‘python’ in a terminal. While ActivePython is generally easier to set up, it tends to lag behind official Python releases, and at the time of writing is only available for Python 2.4.
Now it’s time to load up the interpreter in interactive mode, this gives you a prompt, similiar to a command line where you can run Python expressions. This lets you run simple expressions without having to write a Python program every time, let’s try this out:
Python 2.5 (r25:51908, Oct 6 2006, 15:24:43) [GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print "Hello World" Hello World >>>
The first two lines are the Python environment information, and are specific to my install, so your mileage may vary. Interactive mode is useful for more than just friendly greetings however, it also makes a handy calculator in a pinch, and being part of a programming language allows you to use intermediate variables for more complicated calculations.
>>> 2+2 4 >>> 2.223213 * 653.9232 1453.8105592415998 >>> x,y = 5,20 >>> x + y 25 >>> tax = 52000 * (8.5/100) >>> print tax 4420.0 >>> "hello" + "world" 'helloworld' >>> "ring " * 7 'ring ring ring ring ring ring ring '
Another Python type that is useful to know is the list; a sequence of other types in order. Lists can be added and multiplied like strings, and can also be indexed and cut into sublists, called slices:
>>> x = [1,2,3,4,5,6,7,8,9,10] >>> x [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> x +  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] >>> x +  * 2 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 12] >>> x, x, x (1, 2, 10) >>> x[1:3], x[4:], x[2:-2] ([2, 3], [5, 6, 7, 8, 9, 10], [3, 4, 5, 6, 7, 8]) >>> x[:] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The interactive mode is good for quick and dirty calculations, but once you start writing anything longer than a couple lines, or you would like to save your programs to use again later, it’s time to let it go and start writing python programs. Thankfully this is easy, just type your commands into a .py file and run it with the same command.
Just like your favourite programming language, Python has all the usual ways of controlling the execution of programs through if, while and for statements. In Python, an if statement looks like this:
if a > b: print a else: print b
You’ll see that unlike languages such as C or Java, Python does not use braces to group statements together. Instead statements are grouped together by how far they are indented from the margin, the body of a statement extends for as long as there are lines below it with the same or greater indentation.
x = 3 y = 4 if x > y: x = x + 1 y = y + 1
For example, in the above code x = 3 and y = 4 at the end of the program.
x = 3 y = 4 if x > y: x = x + 1 y = y + 1
In the second example, however, y finishes equal to 5.
Python also has loops, both of the while and for variety. While loops are straightforward:
while a > b: a = a + 1
For loops work a little differently from what you might be used to from programming in other languages, rather than increasing a counter they iterate over sucessive items in a sequence, similiar to a Perl or PHP foreach. If you do need to have that counter its no problem, you can use the built-in range function to generate a list of numbers like so:
for i in range(10): print i
If you need it, you can have finer control over your loops in Python by using either break or continue statements. A continue statement jumps execution to the top of the loop, whilst a break statement finishes the loop prematurely.
for x in range(10): if x % 2 == 0: continue if x > 6: break; print x
This code will produce the following output:
1 3 5
A real program: cat
Now we almost have the tools at our disposal to write a complete, albeit small, program, such as the common unix filter cat (or type in Windows). cat takes the names of text files as arguments and prints their contents, or if no filenames are given, repeats user input back into the terminal. Before we can write this program however, we need to introduce a few new things:
Opening and reading files.
In Python files are objects like any other type, and have methods for reading and writing. Say for example we have a file called lines.txt which contains the following:
line1 line2 line3
There are two main ways we can view the contents of this file, by character or by line. The following code demonstrates the difference:
>>> lines1 = file("lines.txt") >>> lines1.read() 'line1\nline2\nline3\n' >>> lines2 = file("lines.txt") >>> lines2.readlines() ['line1\n', 'line2\n', 'line3\n']
The read() method of a file object reads the file into a string, whilst the readlines() method returns a list of strings, one per line. It is possible to have finer control over file objects, for example reading less than the entire file into memory at once, for more information see the python library documentation (http://docs.python.org/lib/bltin-file-objects.html).
Importing modules and querying command line arguments.
Python comes with a wide variety of library modules containing functions for commonly used tasks, such as string and text processing, support for common internet protocols, operating system operations and file compression – a complete list of standard library modules can be found here http://docs.python.org/lib/lib.html. In order to use these functions you must import their module into your programs namespace,
>>> import math >>> math.sin(math.radians(60)) 0.8660254037844386
In this example you can see that we refer to the imported functions by their module name (ie. math.sin). If you are planning to use a module a lot and don’t want to go to the trouble of typing it’s module name every time you use it then you can import the module like this:
>>> from math import * >>> sin(radians(60)) 0.8660254037844386
Access to command line arguments is in another module: sys, which provides access to the python interpreter. Command line arguments are kept in a list in this module called argv, the following example demonstrates a program that simply prints out all the command line arguments one per line.
import sys for argument in sys.argv: print argument
This produces the following output when run with multiple arguments, the first argument is always the name of the script.
% python args.py many command line arguments args.py many command line arguments
Input from the console
It seems maybe a little archaic in the era of GUIs and Web delivered content, but console input is a necessity for any program that is intended to be used with pipes. Python has a number of methods for dealing with input depending on the amount of control you need over the standard input buffer, however for most purposes a simple call to
raw_input() will do.
raw_input() works just like a readline in C or Java, capturing each character until a newline and returning a string containing them, like so:
name = raw_input() print "Hello ", name
Error handling with exceptions
When you’re a programmer, runtime errors are a fact of life; even the best and most robust code can be susceptible to user error, hardware failures or simply conditions you haven’t thought of. For this reason, Python, like most modern languages, provides runtime error handling via exceptions. Exceptions have the advantage that they can be raised at one level of the program and then caught further up the stack, meaning that errors that may be irretrievable at a deep level but can be dealt with elsewhere need not crash the program. Using exceptions in Python is simple:
try: filename = raw_input() filehandle = file(filename) print len(filehandle.readlines()) except EOFErrror: print "No filename specified" except IOError: print filename, ": cannot be opened"
In this example, a block of code is placed inside a try statement, indicating that exceptions may be raised during the execution of the block. Then two types of exceptions are caught, the first EOFError, is raised when the
raw_input() call reaches an end of line character before a newline, the second, IOError is raised when there is a problem opening the file. In either case if an exception is raised then the line that prints the number of lines in the file will not be reached. If any other exceptions are raised other than the two named, then they will be passed up to a higher level of the stack. Exceptions are such a clean way to deal with runtime errors that soon enough you’ll be wanting to raise them in your own functions, and you’ll be pleased to know that this is easy enough in Python.
try: raise ValueError, "Invalid type" except ValueError: print "Exception Caught"
The Final Product
Now that you’ve got all the tools at your disposal to write our filter. Let’s take a look at the program in full first, before reading on, put your new knowledge to use by working out what each line does:
import sys if len(sys.argv) < 2: while 1: try: line = raw_input() except: break print line else: for filename in sys.argv[1:]: try: file_contents = open(filename).read() print file_contents, except IOError: print "Error: Cannot open", filename
The program is simple, if there are no command line arguments (except for the script name, of course), then the program starts reading lines from the console until an interrupt is given. If there are command line arguments, then they are opened and printed in order.
So there you have it, Python in a nutshell. For my next article I’ll be walking you through the development of a Python program for finding all of the images used on a web page. If you’d like to suggest a topic for me to cover on Python drop me a line at firstname.lastname@example.org.