Original: March 16, 2012
Update: May 14, 2017
Python is a cross platform scripting language. It uses an interpreter rather than a compiler so writing and testing a script is pretty painless. There is an IDE (integrated development environment) called IDLE in homage to Eric Idle of Monty Python fame—the namesake for the language, but I prefer to edit using TextWrangler and test in terminal.
I taught myself to script in Python, but I already know several languages so it was mostly a matter of learning the Python syntax and some of the unique, and quite brilliant, features of the language. Python has some very nice data structures, including a dictionary class, and it is great for parsing strings. Out of the box it's a little weak when it comes to math, but there are several add on modules that fill in the gaps. When combined with Unix shell scripts Python makes a very powerful automation system.
Regardless of how you decide to proceed you will need to set up a programming environment. If you don't want to commit too much at first, Code Academy has the Python 2 environment (editor and console) set up on their web site; no installation necessary. If you use OS X then Python is already installed, but like Code Academy it's version 2. There are enough differences that I think it's worth installing the latest version 3 available at Python.org. Since I prefer Python 3, and Python 2 is installed by default, I include the first line of the following at the top of each script:
I suggest you install TextWranger for free from the app store. Open a new document and type the following:
After you have typed that much you can click the she-bang (#!) menu item and select 'run in terminal'. If you have typed everything correctly you will be rewarded with a terminal window that will have a bunch of text, but the two lines before the word 'logout' will be the message your program is printing to you. You're off and running.
Play around with Python to get your feet wet and write a few simple scripts just to get a handle on the process. Some programming suggestions to get started:
- hello world
- guess my number game
- generate a fibonacci sequence of length n and then write that list to a file
- pig latinizer—start by writing a function to pig latinize a word and then modify the program to pig latinize each word in a sentence
- a simple replacement cipher
- a multiple-key replacement cipher
Some keywords and syntax for a quick start:
One of the things that helped me to learn some of the unique features of Python was to take the python programming challenge. The challenge is really well constructed. It forces you to research and learn to do things the "Python way" rather than just translating methods better suited to other languages. It's a great challenge and a lot of fun in a geeky sort of way. I highly recommend it. I also do the chanllenges in Advent of Code and Project Euler with Python