Friday 3 October 2014

Python - venomous programming language

What is Python?
  • Python is a powerful modern computer programming language.
  • It bears some similarities to Fortran, one of the earliest programming languages, but it is much more powerful than Fortran.
  • Python allows you to use variables without declaring them (i.e., it determines types implicitly),and it relies on indentation as a control structure. You are not forced to define classes in Python(unlike Java) but you are free to do so when convenient.
  • Python was developed by Guido van Rossum, and it is free software. Free as in “free beer,” in that you can obtain Python without spending any money. But Python is also free in other important ways, for example you are free to copy it as many times as you like, and free to study the source code, and make changes to it. There is a worldwide movement behind the idea of free software,initiated in 1983 by Richard Stallman.
 First Python Program:
Interactive Mode Programming:

Invoking the interpreter without passing a script file as a parameter brings up the following prompt:

$ python
Python 2.4.3 (#1, Nov 11 2010, 13:34:43)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Type the following text to the right of the Python prompt and press the Enter key:

>>> print "Hello, Python!";

If you are running new version of Python, then you would need to use print statement with parenthesis like print ("Hello, Python!");. However at Python version 2.4.3, this will produce following result:

Hello, Python!

Script Mode Programming:

  • Invoking the interpreter with a script parameter begins execution of the script and continues until the script is finished. When the script is finished, the interpreter is no longer active.
  • Let us write a simple Python program in a script. All python files will have extension .py. So put the following source code in a test.py file.

print "Hello, Python!";
Here, I assumed that you have Python interpreter set in PATH variable. Now, try to run this program as follows:

$ python test.py

This will produce the following result:

Hello, Python!

Let's try another way to execute a Python script. Below is the modified test.py file:

#!/usr/bin/python

print "Hello, Python!";

Here, I assumed that you have Python interpreter available in /usr/bin directory. Now, try to run this program as follows:

$ chmod +x test.py     # This is to make file executable
$./test.py

This will produce the following result:

Hello, Python!

0 comments:

Post a Comment