Python: Thinking differently
The best part of the jump from PHP is the change of lexicon, which really helps to change the way you think about coding. Forget associative arrays, simple data structures and even your good old-fashioned variable. Leave behind your concept of true and false. Everything, and I mean everything, in Python is an object.
Your main data types are lists, tuples, and dictionaries. And don’t forget about the good old-fashioned string. A list is kind of like an array, and a tuple like an immutable array. A dictionary can be equated to an associative array. But nothing in Python is quite the same.
Any data type can contain any other data type, which means that you can assign deep-level object associations to a list or tuple and perform some really powerful operations on the entire set or just fire off object functions deep within the list. Suddenly you’re working on an entirely different level from anything you’ve done before, with some crazy code structures that open up a world of different possibilities.
The early days
Python saw the light of day in the early nineties. The brain-child of Dutchman Guido van Rossum, known as Python’s Benevolent Dictator For Life, Python is based on a language called ABC, and is often compared to other object-orientated languages like C++, Java and SmallTalk. It’s cross-platform nature, running on Windows, Linux, Mac OS X and a host of other operating systems, has made it a particularly popular language for those needing something that isn’t OS-dependent.
The name comes from Monty Python’s Flying Circus, and this sense of comedy is often found hidden in Python code. You’ve got to love the Dutch.
What really sets Python apart is the predictable way you work with every type of variable. If you want every element in a tuple except the last one, a command like
mytuple[:-1] will do the business. Working on a list instead?
mylist[:-1]. Maybe you’re processing a string:
mystring[:-1]. So slicing is completely standard and predictable, and so is pretty much everything else.
What takes some head-space change is the way code blocks work. There are no curly brackets or
end statements in Python — code blocks use indentation to begin and end themselves. The great thing is that Python code is always properly indented — you really have no choice.
There are also no semicolon or other line enders — a line break will do. If you’re used to Java or PHP, you’re going to have to get your baby finger off that
Python includes an interactive console, which is really awesome for both learning and testing out code snippets. Most of the Python tutorials take advantage of this interactive console, as you can see what various commands do at every step of the process.
If you’re looking for a good tutorial, head directly for Dive Into Python by Mark Pilgrim. It takes you through some real world examples and will get you up to speed in record time. If you’re running Ubuntu, you should find Dive Into Python in your repository, and will find it in
/usr/share/doc/diveintopython/html once it’s installed.
While you’re there, check out the great libraries available for Python. I’ve already made extensive use of PyGame, a gaming library that will have you writing your own best-selling title in no time, as well as NumPy — a scientific library that has great support for matrixes through an array data type.
NumPy and similar projects have grabbed the attention of MatLab users (primarily scientists and engineers) looking for something a little more open source and cross-platform. PyMat allows Python to interface with MatLab with NumPy’s array functions, which means you can do the real work in Python and use MatLab’s serious graphing functions to draw your pretty pictures. Matplotlib hopes to remove your reliance on MatLab even further by doing the plotting straight from Python.
Although I’m just starting my Python adventure, the change of language already has me thinking of new algorithms and ways of coding that just aren’t possible in PHP.