Wednesday, April 07, 2010

Overcoming Python in Ubuntu 9.10

Python's rise to power is imminent by claims that Python alongside PHP/ Perl is the "P" in LAMP. This is equivalent to knighthood amongst geeks.

Python fascinates developers who are very much conscious about productivity. The first step is to ensure Python is installed so here is the command to do so in Ubuntu 9.10. (Most likely it would already be installed).
$ sudo apt-get install python 
The first step to testing a language is to write a hello world program.

Hello World Program
Python's manner of saying hello world to the world is different. Go to terminal and write the following command to invoke python.
$ python
Once inside the shell. Type print "hello world" and the output hello world will appear.

>>> print "hello world"
hello world
Another way of saying hello from the Python world is via creating a text file and writing the same in it and then saving it say "" and then on the command prompt executing the following statement.
$ python
Or to write the following in the interactive shell. (Please note that .py is not written)
>>>import helloworld
The Next Step Forward
A very easy but powerful command can be used is called "subprocess". This helps in executing the shell commands. This simply executes the "df -h" command. The format is to first import the subprocess and then
>>> import subprocess
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             1299.4G  5.0G  4.0G  56% /
udev                  490M  228K  490M   1% /dev
Functions in Python
Lets take a leap here and write a recursive function so that our imagination is tested and concepts build on sound footing. Write the following code in a text file and name it
 def fact(n):
        if n==1:
                return 1
        else :
                return fact(n-1)*n
Notice how similar it is to the factorial code written in any other C-lookalike language. The keyword def indicates the start of a function. Now execute the code in the interactive shell using the following commands.
Examples include finding 2!, 10! and 4! respectively
>>> import factorial

>>> factorial.fact(2)
>>> factorial.fact(10)
>>> factorial.fact(4)
Now lets write another function but this time its not recursive rather its iterative version of Fibonacci Serics. Write the following in a text file and save as Notice how multiple assignments and increments are being performed.
def fib(n):
        a, b = 0, 1
        while b < n:
                print b,
                a,b = b, a+b
Execute the function in the interactive shell using the following commands.
>>> import fibonnaci
>>> fibonnaci.fib(10)
1 1 2 3 5 8
>>> fibonnaci.fib(100)
1 1 2 3 5 8 13 21 34 55 89
Object-Oriented Python
Python being a very clean language also helps one to define classes in a sleek manner. Write the following in a text file and save as
class NameClass :
        myFirstName, myLastName = "Alexia", "Candella"
        def printName(self):
                print self.myFirstName
                print self.myLastName
Now write the following in the interactive shell to test not only the class method but also to create the instance of the class.{Notice that PrintName is the name of the module loaded. Hence required to create instance of the class NameClass}
>>> import PrintName
>>> x=PrintName.NameClass()
>>> x.printName()

Its believed that after doing this little exercise the python language has been overcome. Now the next part is to move on and do some serious stuff. This can be accomplished by using the tools of the language and combining to solve the problems and learning the language along the way. A very good reference is available here.

No comments: