Assignment Statements, Comparisons & Observations
Font: Size: + -

Why Python Rocks

Friday, September 28, 2007
I was checking out the Python programming language, and I must say, this reminded my a lot on the basic set theory I learnt when I was in the 11th grade. Its like as if, Python (Py for short) is made for math geeks. Its not about the math or any other library that has a load of math functions - its the way the language is designed - where the language is itself very much like writing 'math code'.
  • Calculator : Has a direct interpreter-cum-IDE that doesn't require you to store any code in a file first. All executed statements are stored temporarily in RAM. This something like the Immediate Window Microsoft QuickBasic offered.
  • Exponentiation : To find the value of xn in Java, .NET, PHP, JavaScript etc, built-in pow function is to be used. In Py, this function call is saved by an arithmetic operation **. x ** n. ( Though pow() function does exist in the math library )
  • Functions can return more than one value :
    def foo(a, b):
        return (a + b, a - b)
    x, y = foo(5, 3)
    print x, y
    (5, 3) is called a tuple which is different from [5, 3] which is a list (an array). Even C# doesn't have this functionality - the maximum you can come close to it is, using the out keyword :
    void foo(out int a, out int b)
    but even this doesn't replicate the actual "return more than one value function"
  • substring : No need for a substring() function - strings natively support accessing part of the string using 2 indices separated by a colon.
    >>> s = 'Python'
    >>> s[3] # print s[3], if print is not mentioned, it'll automatically do a print
    >>> s[2:5] # from 2 to 4
    >>> s[:3]  # from 0 to 2
    >>> s[3:]  # 3 to last
  • Parallel assignments : In most languages, if you do int a = 5, b = 6; it would get read sequentially - declare a of type int and assign it to 5 and then declare b of type int and assign it to 6.
    In Py, you can just do a, b = 5, 6 making the assignment in a parallel fashion. a, b, c, d, e = 56, 98, 'b', False, 1.01
  • Complex numbers : The last time I ever used complex numbers in a computer program was in school. Yet, this is another bonus for the math people - built-in support for Complex numbers !
    >>> a = 2+3j + 3+2j
    >>> a
    >>> a.real
    >>> a.imag
    Odd why j is used to represent imaginary instead of the i we're used to.
  • String multiplication : Yet another string thing - multiplying a string with a number n is equivalent to concatenating it n times.
    >>> word = 'Python'
    >>> word * 5
  • Compound Data Types : One thing that makes it easier for PHP coders is that they got to deal with a single data type to deal with lists and the like - array. Its everything you need to store a tree. Most of the functions dealing with arrays are already built in having the array_ prefix. In Py, there are different data types that are essentially like PHP's arrays but handled differently - Lists, Tuples, Sequences, Sets and Dictionaries ! Its a whole less keystrokes to type, but if you're too used to ArrayList() and its method calls, it'll probably be a bit too confusing to get used to Python's paradigm.
  • Compiled Code : Though Python is an interpreted language, it provides a feature that Java first came up with - byte-compiled version of the source code. Compiling source-code (.py) to byte-compiled version (.pyc), the latter just loads faster. This is may not be very relevant in terms of speed execution, atleast we can hide the source code directly.
  • Multiple Inheritance : And you thought all new languages wouldn't support multiple inheritance ! Well, this is one that's at par with C++. Even though many might dread the pitfalls of multiple inheritance, Py relies on conventions to avoid accidental conflicts.
If this post has inspired you start learning Python now, you might as well dive into the newer super-major release of Python - Python 3000 (a.k.a. "Py3k", and released as Python 3.0). From Guido van Rossum's blog :

Python 3000 (a.k.a. "Py3k", and released as Python 3.0) is a new version of the language that is incompatible with the 2.x line of releases. The language is mostly the same, but many details, especially how built-in objects like dictionaries and strings work, have changed considerably, and a lot of deprecated features have finally been removed.

There are some changes in the basic language constructs which are updated in the What's New section.