Python FTW

Python has made programming an absolute pleasure for me. I simply love coding in python. The ease of programming and the importance given to programming logic than programming syntax is an absolute delight. I first learnt python from this tutorial. It is a great website to learn python, especially for the beginners. A more advanced one can be found here. Whenever someone asks me, how is it to code in python? I always remember this xkcd comic.

python_ftw_1

According to me (among the programming languages I know), python is the only true “high” level language. I mean, if programming languages took part in “Game of Thrones” python would be on Iron Throne the whole time uncontested.

Some common observations is the python. There is absolutely very minimal syntax, there are no semicolons, parentheses or type declaration. There are tons and tons of packages that you can import and work on. Writing modules in python is also a cakewalk, you find modules for a lot of things. You want to access UNC path there is a module, want to SFTP, there is a module, want to write a web crawler, there is a module. There are gateway and wrapper modules that supports almost all common programming functions you would need.

Even web programming is amazing. Django and Pylons are the major ones and they take web programming to a whole new level. These web frameworks take a lot of work out of our hands and provide us with a lot of power to simply code and build beautiful things.

Here is another comic that ‘kinda’ compares python with other languages.

python_ftw_2

Some examples of why python is a breeze to learn and use.

How do you print “hello, world” in python?

you say,

print “Hello, World”

How do you print “hello, world” in say, Java?

 class foo{

       public static void main(String v[ ]){

       System.out.println(“Hello, World”);

       }

}

 List Comprehension

Generate a list of even integers below 100

one line,

A = [ x for x in range(2, 100, 2)]

List of square numbers below 10.

No problem, just one line

A = [x * x for x in range(10) ]

Suppose you have a huge list of numbers and you want to extract only even numbers from it.

A = [ 3, 5, 34, 77, 88, 500, 145, 338, ….., 1538]
even = [ x for x in A if x%2 == 0]

Yes, just two lines and the first line does not even count.

Here is a biggie,

I want to rotate a matrix , clockwise by 90 degrees.

consider,

matrix = [[1,2,3],[4,5,6],[7,8,9]]

and you have to rotate it by 90 degrees.

Guess how many lines of code it would take? you guessed it right – one ‘freaking’ line.

print zip(*m[::-1])

Amazing isn’t it. Python eases coding a lot. But don’t get carried away guys, other programming languages are also very good . And depending on the requirement you should choose the appropriate language. Compiled languages are faster and should always be considered before interpreted language. If speed of execution is important then compiled languages, if speed of development is essential then python would be an amazing choice.

Another good example of python ‘awesomeness’ would be the ‘with’ statement.

As a programmer you tend to forget sometimes (mostly many times). You will have open files, sockets or other connections and forget to close it. The python ‘with’ statement comes in handy. One can simply say

with open(“filename”, “r”) as fs:

         do something

and python takes care of closing the file on its own, when the scope is passed. In a broader sense, what ‘with’ statement is trying to accomplish is this

set things up

   try:

       do something

   finally:

       tear things down

Here the ‘tear things down’ will be taken care by ‘with’ statement and does not require explicit ‘finally’ block.

Python is very powerful, I am sure there are plenty more examples out there that show the power of python. I will try to keep this post updated as and when I find them.

About Narayana Ravikumar

Hi, I am Narayana Ravikumar from North Carolina State University. I love web and system programming. I am currently finishing my masters at NCSU. Check out my 'about me' page here.

28. June 2013 by Narayana Ravikumar
Categories: Programming, python | Tags: , , , | 6 comments

Comments (6)

  1. Totally!.. Python is a really cool language now. Try exploring functional languages like erlang and google go! . You will be amazed!

  2. Pingback: Fun Python Packages | Nani's Rants

  3. You know I love your blog!!!

  4. Thanks a lot. I appreciate it :)

Leave a Reply

Required fields are marked *