Python for fun

Pining for the fjords

Pining for the fjords

A while back I had posted something about Paul Graham’s essay on good hacking environments. As an aside, one of the observations he made (with which I don’t necessarily agree) is that there will be a tendency to find better hackers coding Python than coding Java. I do more Java myself these days, but find I really like Python as well. So let’s talk about Python for a bit. This has become relevant for me because I want to get started on one of the coding projects that has been sitting on my queue at home. After pondering things for a while, I’ve decided to go Python (I already have several Java projects lined up). Python code is pretty, web friendly, object oriented (though you can write it like it isn’t if you really want), and has a lot of useful libraries.

The coolest thing about the language, of course, is that the creator, Guido van Rossum, named it after Monty Python, a little British comedy group that was responsible for finding things like the holy graile, the meaning of life, and starting a religion after a boy called Brian who grew up to be a man called Brian. One of the funky things about the language of Python is that all blocks must be indented. It is part of the syntax as it were. That took me some getting used to, but since pretty code should be nicely indented anyway, that’s not really that intrusive. There are editors that will assist with that, such as emacs (also xemacs), which will run under Windows as well. Python also has its own ide as well, called Eric, which coincidentally is also the name of one of the Monty Python members. Although Python is almost trivially on every Linux system (though you may need to select the software; I did for SuSE) you can have it Windows too.

One of the fun things about Python is its support for imaginary (complex) numbers (square root of -1, for well defined rules of multiplication). Stick a j on a number to make it imaginary. For example, in Python you can write a complex number such as a = 3.0 + j5.0 . Then establish that a.real is 3.0 and a.imag is 5.0. With that comes a lot of manipulations, etc. You can do the same thing in Java, but you would need to find a library for it, or write it yourself.

So what else does Python give you? One of the powerful web application servers out there is Zope, written in Python. I have not played with this yet, but am reasonably confident that if you are writing Python apps, you could easily leverage what Zope offers. You can do graphics using the TK (toolkit) interface arising out of TCL/TK by using TKInter, and I’ve recently found out about vpython, another library that allows you to do 3d graphics and animation. Apparently a lot of colleges are using this to have inexperienced (from a coding point of view) students write their own physics demos. I expect I’ll play with this a bit going forward.

What does code in Python look like? Here’s an example of some really simple throwaway code I did several years ago. This sends email to a specified address (maintained in a separate configuration file called

import smtplib, string, sys, time, mailconfig, os
mailserver = mailconfig.smtpservername

interestingStuff = sys.argv[1]

fd = os.popen('hostname')
myhostret = fd.readlines()[0].split('\n')

From = mailconfig.sender
To = mailconfig.recipient
Subj = 'Some Alert!'

date = time.ctime(time.time())
text = ('From: %s\nTo: %s\nDate: %s\nSubject: %s\n\n'
% (From, To, date, Subj))

text = text + "Something interesting happened on " + myhostret[0] + " for " + interestingStuff

text = text + "\n\n" + mailconfig.signature

print 'Connecting...'
server = smtplib.SMTP(mailserver)
failed = server.sendmail(From, To, text)

Of course, one can add to this and make it as complicated as one wants. The import at the beginning just specifies some libraries like the stmplib that the Python interpreter needs to do its work. Python can look even more simple. All you need to do is run the interpreter and type in equations to use it as a calculator (that’s what I often do for quick calculations…either that or Lisp).

So, go ahead, if you are not a software developer, try your hand at some simple Python code. I am of the opinion that everyone should know a little coding, just like everyone knows how to hammer a nail in a piece of wood. Not everybody is going to build fancy furniture and houses, but should be able to do a few simple things here and there. Python is a great language to get started playing around and pick up a few things. I promise it won’t kill your parrot.


Tags: , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: