Tuesday, 28 June 2011

Python : Interesting String & List

Here i would like to share some interesting behavior of String & List when you use python interpreter.


We all know that the fundamental thing about Python Strings is that they are "Immutable" ,once you created a string you couldn't change it. See some examples....

>>> s = 'fun with string'
>>> s
'fun with string'

you can just add another string by using  '+'  as shown below...

>>> s + '!!!!'
'fun with string!!!!'

Another important feature is String Slices. Just see how "slice" syntax behaves in python.

>>> s = 'fun with string'
>>> s[0]                                                 
>>> s[1:4]
'un '
>>> s[1:]
'un with string'
>>> s[:]
'fun with string'
>>> s[-1]
>>> s[-4]
>>> s[:-3]
'fun with str'
>>> s[-3:]

So now we have a string 's' and we can apply all the methods (i.e functions) that are related to string to 's' as shown below...

>>> s.upper()
>>> s.lower()
'fun with string'
>>> s.startswith('fun')
>>> s.endswith('string')
>>> s.replace('string','python')
'fun with python'

But my favorite is the method called ' s.split() '. Just see what happens if i assigned s.split() function in to a variable 's' itself. If we print it you can see a list of elements.It just transformed into a list.

>>> s = s.split()
>>> s
['fun', 'with', 'string']


So now the variable 's' will behave like  a list.Now you can apply all the methods related to list here with 's'.

>>> s.append('& list')
>>> s
['fun', 'with', 'string', '& list'] 
>>> s.insert(0,'some')
>>> s
['some', 'fun', 'with', 'string', '& list']
>>> s.index('some')
>>> s.index('string')
>>> s.sort()
>>> s
['& list', 'fun', 'some', 'string', 'with']
>>> s.pop(0)
'& list'

See what happens if i call the function " join() ",and assigned it to same variable 's' .When you print 's' you could see the first string.Is it interesting?

>>> s = ['fun', 'with', 'string', '& list']
>>> s = ' '.join(s)
>>> s

'fun with string & list'

Thank You....

Friday, 24 June 2011

Unix to Linux story

The story has begun from the well known AT&T’s Bell Laboratories in the early 1960 ’s.

At that time AT&T needed an operating system for their computer center because there was running various batch jobs .The BESYS  operating system was created for at Bell Labs to deal with these needs. so at the year of 1965 Bell Labs was adopting third generation computer equipment and decided to join with General Electric and MIT to create a Multics (Multiplexed information and Computer service).But at the year of 1969 they made a change that ,to withdraw from Multics and go with GECOS.

When Multics was withdrawn Ken Thompson and Dennis Ritchie needed to rewrite the operating system, And finally they made a system called UNICS (UNiplexed Information and Computing Service) ,

The UNIX operating system was first developed in Assembly language but , by the year of 1973 it had been almost entirely recoded in C. The first edition of the UNIX Programmers Manual includes over 60 commands like : b , boot , cat , chdir , chmod , cp , ls , mv etc.

Second edition of UNIX was released in 1972.But now you all know that it has been split in to various branches developed by AT&T for various commercial purposes.

AT&T made UNIX available to Universities and commercial firms as well as the United States government, under licenses. The licenses include all source code including the machine dependent parts of the kernel. The copies of the UNIX kernel sources circulated widely in late 1970s  in the form of much copied Book by John lions of the University of new south Wales.

 Intel created the first commercially available microprocessor (Intel 4004) in 1971, and one of the first microcomputers in 1972.They emerged from versions to versions like 80386.80486,80586 etc. Pentium i.e Intel 80586 was introduced in 1993.They mainly focused on making it useful for the ordinary people.

 At the year of 1975 the software is emerged as an independent entity. Companies like HP and SUN started to using UNIX as an operating system for their machines. HP-UX 1.0 is released in 1980.SUN unveils Solaris 2 Operating system, specially tuned for symmetric multiprogramming at the year of 1991

AT&T Licensed UNIX system III , and continued to issue license for the older versions . So to end the confusion between all its versions , AT&T combined them into UNIX System V release 1.This introduced a few features like vi editor and courses from the Berkeley Software Distribution of Unix developed at the University of California, Berkeley

This move of AT&T , that to commercialize UNIX system v .actually killed UNIX. The Free Software Foundation (FSF) was founded the same year by Richard Stallman.

Richard Stallman’s contribution

Richard Mathew Satallman (rms),was against the U.S Copy write Act ,i.e restricting the unlicensed access to the software. Stallman proclaimed it and argued that it is blocking the user’s freedom to use it.In 1984 he quit his job from MIT to work full time on GNU project.

The main intention behind GNU Project is to create free Unix-like operating system. With the launch of the GNU Project he founded the Free Software Foundation(FSF).

Stallman popularize the concept of Copyleft ,a legal mechanism to protect the modification and redistribution rights for free software. In 1989 the first program independent GNU General Public License (GPL) was released.  

The GNU system was almost completed by then,he was contributing many naccessary features like Text editor (Emacs), Compiler (GCC) , Debugger (gdb).But one important thing was missing , i.e Kernel. The GNU kernel ,called Hurd ,failed to attract enough attention from developers leaving GNU incomplete.

Linus Torwalds donated the kernel.The existing programs for the GNU projects were ported to run in the new platform. and then formed the GNU/LINUX projects.

Linus Torvalds’s contribution

When he was studied at Helsinki, he began a project that later becomes the Linux kernel.The main intention behind the project was to write a program specifically for the hardware that he was using and that should function independent of  an operating system.

The development was done on MINIX operating system,using the GNU C Compiler. He wrote on his book that he eventually realized that he had written an operating system, and he made a post in Usenet,that “ I am doing a (free) operating system, I’d like any feedback on things people like/dislike on MINIX…”

He made it successful,and the first version of Linux has released.He first published the Linux kernel under its own license, but later licensed under GNU General Public License(GPL). In the first release of Linux 0.01 Torwalds lists the GNU software that is required to run Linux.

Now Linux has many versions and distributions,and proves itself as  one of the worthy opponent to Windows giants.Many IT industries are now started to use Linux as a better alternative.

Thank You..

Monday, 20 June 2011

Python : Script mode

We have already discussed that python is an interpreted language and mainly there are two ways to use the interpreter , i.e Interactive mode & Script modes . In the interactive mode it just displays the result of expressions , We can have the python interpreter to execute our script file also.

In script mode we can store the code in a file and use the interpreter to execute the content of the file which we called a script.The scripts must have saved with the extension .py.

To edit the scripts, you can have common editors like gedit , VIM ,eric4 and eric5 etc , Several type of editors are available for Unix and Windows.

But when you edit python scripts the indentation must be accurate, In all our files we use 2-spaces as the indent and 4-spaces is another popular choice.

Script mode

There are several ways to use the interpreter and have execute our scripts.

  • Explicit Command line execution:
Here python script is executed by providing the script file name as parameter to the python interpreter.In this style we explicitly name both the interpreter and the input script.
we have a sample python script file called sample.py is shown below.When you type python sample.py ,it will provide sample.py file to the python interpreter for execution.

You could see an import statement on top of the program ,by using this syntax you can pass command line arguments,i.e sys.argv[1] will print the argument that you passed along with the file name.
if __name__ == '__main__' is the boilerplate syntax to call the function main().It is mainly used when a python file is run directly.

Output of the following program is displayed below .

  • Implicit command line execution:

We can make the script file itself  executable and directing the shell to locate the python interpreter.For windows we can associate our script file .py with python.exe .There are one or two steps to do this.

  • when you creating a script file make sure that first line of the file contain the following #!/usr/bin/env python.
   If you enter the above code, the whole file will look as follows.

  • For POSIX-standard operating systems,do chmod +x sample2.py to make the file sample2.py executable. 
  • Then you can run the file by using ./sample2.py command.
when you run the file ,you will get the expected output as follows.

Thank You..