Friday, June 6, 2008

Introduction to Python

It started with 'C', then C++, then java, then perl and many more…

Technology gets better day by day, and it's easy to get started with Python in few minutes.

  • Python is a very high level language.
  • Programmers, who have used c, c++ or java can smell the familiarity, while appreciating the ease of coding.
  • Python can also be used as a object oriented notation.
  • Python is a interpreted language. (each line is read @ runtime)
  • Python is extensible from other languages and also to other languages.
  • The code looks simple and less complex, compared to other base technologies. For instance, there is no open curly brace and close curly brace, defining a block, but a tab would do.
  • Python can be used with GUI too.
  • Python coders can use data structures effectively for design and implementation, than in any other language.

Downloads Required :

python download (available for windows, mac and unix)

A must see full python tutorial, which is cool!

Why Python?

It is a high level language and you cannot just keep going on with C and C++, for the whole of your life. A more convincing reason - Google search is coded in python, and so are so many other Google products.

Lets get started. (I assume you have downloaded Python and put the IDE shortcut on your desktop)

You must add the python directory to your class path. In windows, you right click My computer -> Properties -> (Advanced Tab) -> System Variables ->

Add or edit variable "path" to append your python directory path.

Or simply in DOS (requires restart of DOS)

set path=%path%;C:python24

Lets get started with the code..

Resisting the temptation to print a "hello world" , which you can achieve by just saying "hello world" (enclosed in double quotes or in single quotes) and pressing the return key (I told you, its simple), we ll write a function to print the Fibonacci series up to n.

def fib(n):
a, b = 0, 1
while b < n:
a, b = b, a+b
print b,

>>> fib (10)
1 1 2 3 5 8
>>> fib (100)
1 1 2 3 5 8 13 21 34 55 89
>>>

Things to notice:

1. This must go first actually, the code looks simpler and less complex than c or java.

2. The >>> is the prompt, which you can change like in unix or solaris.

3. The def command declared a function, by getting its signature and there are no irritating return types or even data types for that matter.

4. The tab after the first line specifies that, all code inside it, are enclosed in a block. The same holds good, when we implement the while loop.

5. After the while command, I did not specify any braces to enclose the condition. Python is more intelligent to know it by itself. The return type need not be a boolean (actually there is no boolean).

6. I did not compile or do any thing to the program and it got executed fine.

7. The print command spits the value of the variable to the screen and a return feed ("\n" in java). The comma that follows it, overrides it to maintain the output in the same line.

No comments: