Pages

                           

Friday, December 17, 2010

A Littile Bit Of Python....

 Of the various popular scripting languages, Python is probably closest in form to traditional system languages . A host of good Python resources can be found on the Internet at http://www.python.org. I will focus on the features of Python that make it a particularly good choice among scripting languages as a first computer programming language.

1.Python is simple

 In general, scripting languages are much simpler than system languages like C++ and Java. Python has a simple, regular syntax.Python programs look like executable pseudo-code. This eliminates a host of troublesome errors for beginning programmers, especially placement of semi-colons, bracketing and indentation. For example, a common error in C++ and Java is the failure to enclose a block in braces as. Python supports the use of functions and classes but does not force it. Simple programs really are simple. 

For example, consider the ``Hello World'' program in Python:

>>print "Hello World!"

C++ requires this program to be wrapped in a function and preceded by a preprocessor directive:

#include <iostream.h>
int main()
{
cout<<"Hello World!";
}
In Java, the situation is even worse, as all code must be inside of a class:

public class helloWorld
{
public static void main(String [] args)
{
System.out.println("Hello World!");
}
}

Python is dynamically typed, so there is no need for variable declarations. Python's for loop is illustrative. It allows a control variable to take on successive values in a sequence. It can be used to iterate through any sequence such as a list (array) or string. For example, the items in a list can be printed as follows:

for item in List:
print item

The range operation produces a sequence of numbers in a given range. For example, range(5) produces the list [0,1,2,3,4]. This can be used to provide numerically-controlled loops. The previous code could have been written as:

 for i in range(len(list))
    print List[i]


2.Python is safe




Python provides full dynamic run-time type checking and bounds checking on array subscripts. Python employs garbage collection so there is no problem with dangling pointers or memory leaks. It is impossible for user code in Python to produce a segmentation violation.

2.Python supports object oriented programming


Although one doesn't have to use classes to write Python programs, Python does support object-oriented programming through a class mechanism similar to that provided by C++ and Java. The class model of Python is a simplification of the C++ model and supports multiple inheritance. Since Python is dynamically typed, there is no need for abstract classes a la C++ or the interface mechanism of Java. 



4.Python is fun

The simplicity of Python makes it easy to learn. In addition to the list (dynamic array) data structure, Python provides tuples (immutable lists) and dictionaries (hash tables). Together with the class mechanism, these can be used to quickly build sophisticated data structures for interesting projects. The absence of type declarations makes for less code and more flexible programming. There is also a huge library of standard and contributed modules providing components for programming GUIs, client-server applications, html viewers, databases, animations, and much more. The rising popularity of scripting languages is directly attributable to the ease with which sophisticated applications can be built by combining off-the-shelf components. Interesting projects can be developed with only a fraction of the code that would be required in a system language.