The language Series: Python


My Python Story

I had always wanted to learn Python ever since I was an undergraduate but my commitments took all my time. Alhamdulilah, I finally got my chance in my first semester at MASDAR when I had to do the Artificial Intelligence project in Python. It was a pretty challenging one; it involved building a framework that used brute-force in solving problems with humongous search spaces.

It has been a nifty tool for my research work too: starting with stackoverflow to openstreetmap data. Python provides easy-to-use tools for data cleaning, processing and graphing; some of its libraries are a sheer pleasure to use: matplotlib, networkx, scipy, numpy etc. More importantly, I can write my full processing stack in Python without having to resort to other languages or tools. Although, there are speed and memory consumption trade-offs; overall, it’s been a pretty good bargain.

The Good Parts

  • The language has an almost non-existent learning curve; it’s soooooo easy to learn.
  • Comes in handy when you want to write a script or solve a little problem.
  • An awesome community, nice package management and great libraries – examples include scientific libs (matplotlib, numpy, scipy) SQLAlchemy, PyTables, Sage, networkx.
  • Cross-platform and supports full-stack development.
  • Great readability, high expressiveness and a very simple grammar – almost reads like English at times.
  • Allows returning multiple values from a method.
  • Some cool supported operations in Python:
    • Swapping: x, y = y, x
    • Floor division: 5.0//2 = 2.
    • Comparisons: if 3 > x > 1: print x; I don’t know of any other language that has this feature.
    • Iterating through two lists simultaneously using the zip keyword.
    • List comprehensions are super-awesome.

The Bad Parts

  • Speed; Python is slow :(.
  • Memory consumption; you can’t explicitly free memory and it’s not suited for situations requiring extremely small memory footprints.
  • Inadvertent overwriting of default functions or settings. For example, I once did this str = “blah blah blah”; Python raised no objections, it meekly overwrote the str() function.
  • Multi-threading support is not too good.
  • Scoping issues: variables fall out of scope so it’s essential to choose unique names.
  • .3/.1 = 2.99999999999999996. The floating point issue rears its ugly head again.
  • Constants… Python does not support constant declarations out of the box; seems JavaScript got friends.

The Other Parts

  • The indentation can be a pain if your editor gets it all messed up. Use a good editor (vim/Emacs) and make sure your tabs are converted to 4 spaces.
  • IDE support ain’t that great since it’s a dynamic language and there are lots of ways to do the same thing.
  • Weird syntax for the ternary operator : True if condition else False
  • Installing libraries can be a really painful operation if you get it all wrong.
  • Since it’s not a statically typed language, typos aren’t detected until your program goes KABOOM!
  • How do you declare private variables in classes? I still don’t know.
  • Explicit addition of self all the time; I think it is needed to know the current execution scope but still….
  • There is no switch statement in the language; workarounds exist though.

Beginning Python?

Do it! Just jump into the language, learn the pythonic way and slowly you’ll come to see its beauty. It might feel weird if you come from a ‘braceful‘ language (i.e. Java et. al. ) and some concepts might initially perplex you. However, once you get used to the language, I bet you’ll wonder how you managed to get stuff done in other languages.

The sheer expressiveness is amazing at times: you write so little and achieve so much. I think it’s fantastic for data processing, little scripts and heavy lifting ( provided you have enough memory and processing power; some libraries allow you to leverage highly optimized C/FORTRAN code so it’s not totally bad).

Also learn to use the interpreter (you can also try ipython if you’re just starting out) – I use it a lot to speed up my development process and for debugging.

Rating

8.5 / 10

One of the easiest languages to learn and use for scripting needs and comes in handy for  rapid prototyping. Extra bonuses include loads of great libraries, support for OOP and functional programming. Sure, it has a few issues but what language is perfect?

Python will probably make you lazy but why do more if you can get the same results with less? :)

Did you enjoy this post? Check out my reviews of C, Java, PHP and JavaScript too.

UPDATE: Thanks to a reader for clarifying my wrong assumption about Python’s ternary operation support.

16 Comments

·

Leave a Reply

  1. Good day sir, I am desperately in need of where i can learn python. pls help me out before our school resumes. Thanks in advance.

    Like

  2. You said it well. I can’t think of a more beautiful language (I haven’t tried Ruby). It also gets the job done. I hear the multi threading issue a lot but I’ve never bothered about it since I don’t need it, for now. :). I suppose this is also related to GIL? Prescence of awesome frameworks even makes it better. Barakallah feehi. First semester exams are over! And oh, list comprehensions are super duper awesome, no need for those “ifs”.

    Like

  3. Salam Alaykum Akhee, this is one of the best episodes of the language series so far, Masha Allah :)

    I’m not a python aficionado, though you’ve just inspired me to learn it. I do know however that Python 3 has an equivalent of the Ternary Operator. I have a Lynda Video Tutorial that mentioned it in the introductory stages. Can’t remember the construct anymore but I’m sure you can easily find out. :)

    Like

  4. About the floating point issue and some data analysis tasks, a way to go might be to use RPy2, an interface to R from Python. I just typed .3/.1 at the R console and the request returned 3.

    RPy2 is easy to use if you know some R. Definitely read the RPy2 documentation and work through the examples to get a feel for it. R is slow and memory hungry, so RPy2 isn’t going to be a good solution for some use cases.

    Like

  5. Well, there is ternary operator in Python, only it is quite unusual:
    >>> “yes” if cond else “no”
    This will return “yes” if cond evaluates to True, and “no” otherwise.
    Also there is no such thing in Python as “private variable”. But you can override class’ __getattr__ method so it won’t allow accessing some of variables or methods.
    And final thing: self is needed, as, for example, you can decorate method with @classmethod decorator and make it sort of ‘static’ method, which receives class object and not an instance.

    Like

  6. Great writeup on Python. I learned a thing or two while researching floating point representation in Python. About the floating point error, it turns out that this is in fact the way the computer actually works. Because computers don’t actually store decimals in memory in decimal form but binary, there are going to be times when the approximation doesn’t quite work as we expect. The decimal module was designed to compensate for these kinds of problems – so instead of:

    >>> 0.3/0.1
    2.9999999999999996

    You should use:

    >>> from decimal import Decimal
    >>> Decimal(‘0.3’) / Decimal(‘0.1’)
    Decimal(‘3’)

    You can find out more here[1] and here[2].

    [1] http://docs.python.org/2/tutorial/floatingpoint.html
    [2] http://docs.python.org/2/library/decimal.html

    Like

    • Thanks a lot!

      I think the floating point issue is probably due to the IEEE number representation format and it affects quite a couple of other languages too.

      But I never knew about the Decimal package; thanks a lot for the update!

      Like

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.