Friday, March 25, 2011

Run Python script in Linux

Hello everyone,

Now its time to give it a try for python. Pythons is general-purpose high level programming language. The python's syntax is very clear and human readable.
The interesting fact about python is, It never forces programmer to write a code in OOPS style like java or in structural style like in c. It is all up to programmer how he/she wants to implement it.
Python is influenced by ABC, ALGOL 68, C, C++, Java, Haskell, Pearl etc.
Python can be run on multiple platforms.

Now lets begin with our first program i.e. how to print hello world. For that just go to your terminal
from Applications>>Accessories>>Terminal

step 1. Just type python
it will show you

Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)

[GCC 4.4.5] on linux2

Type "help", "copyright", "credits" or "license" for more information.


step 2. This is nothing but your python version installed on the system.

Step 3. Now you are in python mode. Means not at primary prompt. i.e. >>>

step 4. Just write print “hello world” and hit enter. Thats it , yes you are done. It will show you
the output at next line like 'hello world'

1. you can also use python as desktop calculator at go to terminal just type python and at python prompt (which is >>>) just type 5+2 and it will give you the answer.

2. You can also edit bigger python code in your favorite text editor with following command
gedit and hit enter you will see new file open in gedit which is default
text editor for linux.

3. Remember .py is file extention of python files but some times we also uses .pyw, .pyc, .pyo, .pyd

4. If you are python prompt (which is after typing python at primary prompt) you can terminate
any operation by pressing the CTRL+C

5. If you want to terminate everything from the terminal and go back to the primary prompt
then simply type CTRL+D

6. As python is influenced from C language so you can write the same code except curly
braces ( {} ) are not welcome here instead use proper indentation.

7. Python also requires proper indentation while programming if not it will generate
indentation error
like e.g.

if a==2:
print “a is equal to two”

the above code will generate indentation error so right way is to do is put a tab of space before
print statement
Right way is



... print "a is equal to two"

... else:

... print "a is not equal to two"

now the output will be

a is equal to two

If you want the beginners python pdf you can get it from here.

So bye for now and feel free to comment on any of my post.

Enjoy & be open.
long live linux

No comments:

Post a Comment