Python: Thinking differently

By   |  September 26, 2006

For those who followed my PHP tutorials, it should be pretty clear by now that I’m a PHP fan at heart. That’s why it was a surprise even to myself that I started learning — and enjoying — Python.

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 begin and 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 ; key.

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.

Comments

3 Responses to “Python: Thinking differently”

  1. Stmok
    September 26th, 2006 @ 12:00 am

    I\’m also starting Python myself. I found your article reasonably interesting. (Useful tip in regards to Dive into Python\” in Ubuntu repository.)

    You should also mention about \”Twisted\” (an open source network framework for Python). Twisted was used by the author of the original BitTorrent client. (yes, that was written in Python) : )

    There\’s also plenty of other modules and such…
    pygame = SDL
    wxPython = wxWidgets
    pyQt = Qt
    and so on…

    If you\’re a little worried about speed, there\’s also Psyco which is a module to improve the execution time for Python code.

    In terms of applications, I know Blender uses Python for scripting. While that Windows game,
    \”Act of War: Direct Action\”, uses Python in parts of the game. (You can see they use Python 2.3 in the installed directory!)

    Overall, once you get into Python…It feels like it opens up quite a number of opportunities.

  2. Tom
    September 26th, 2006 @ 12:00 am

    I have been programming full time since 1991. Started in C then moved to Clipper. With Windows becoming mainstream I have moved to Delphi. Now it\’s C# and .NET

    In heart I\’m Python coder, love this language and using it since 2001
    Biggest strength is that it runs virtually unmodified on Linux and Windows if you follow simple rules like using forward slashes in directory names

    In terms of getting things done, it\’s fast to code and it fits your brain instead of underlying CPU

    This language should be first taught instead of BASIC

  3. vocaro
    September 27th, 2006 @ 12:00 am

    \”Everything, and I mean everything, in Python is an object.\”

    No. Python is definitely not a pure object-oriented language; it\’s a hybrid of procedural and OO design. If you want an \”everything is an object\” language, try Ruby, where everything, and I mean everything, is an object. Example:

    1.5.round
    >> 2

Comments are closed