Python Scripting in File 

I recommend to install "vim" editor by typing apt-get install vim..

Open a file by typing 

vi hello.py

Then type 

#!/usr/bin/python
print "Hello python"

save it by typing ":wq"

then execute it by typing "python hello.py"
or give full permission to your file by executing command

chmod +x hello.py

Now you can execute your file without typing interpreter name "python"
#!/usr/bin/python  ---> this tells your OS where is the interpreter of this file(hello.py) is present.

Import statements

Two types of import statements in python

  • import  SomeModule
  • From SomeModule import SomeModule

The first statement "import" will just import all the module to its namespace so when want to use a method inside SomeModule you have to call like

SomeModule.Method()

Where as in "from  SomeModule import SomeModule" you can call a method like below (loads all modules to current namespace)

Method()

How is it ? you better to use "from module import *" . . .

Variables naming :

  1. Can start with A-Z or a-b or "_" (Underscores).
  2. Name shouldn't contains "@, #, %" etc
  3. Variables are case sensitive.

Comments : 


# single line comment

""" This is multiline comment
which can span across multiple 
lines """

The above type of comment is also used as doc string in class and methods...  just remember it
in upcoming posts i will cover it.

Python packages  path :

Execute like below shown in python interpreter prompt
>>> import sys
>>> sys.path
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/pymodules/python2.7/gtk-2.0', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/pymodules/python2.7/ubuntuone-storage-protocol', '/usr/lib/pymodules/python2.7/ubuntuone-control-panel', '/usr/lib/pymodules/python2.7/libubuntuone', '/usr/lib/pymodules/python2.7/ubuntuone-client']
>>>
The list if paths are the paths  where python searches for requeried packages

If you want to add any path just execute
>>>
>>> sys.path.append("/your/packeage/path")
>>>


Literals

There are two types
  1. Numbers
  2. Strings

    Numbers : Integer, Long int, float and complex type these are the 4 types of integer constants.
    String

    String:: String is sequence of characters, bunch of words
    Strings are represented in different ways
    1) single coted 'This is string'
  3. "This is double coted string"

    """ This is Triple  coted string
    used to flow across multi lines"""


    Escape sequences :
            Suppose you want to write print ' I'm going to shop ', this gives you error..
            so write it as print  ' I\'m going to shop ', likewise \" escape for " (double cotes)
            \\ , \t (tabspace), \n (newline) ....


    Raw string :  Just like Raw meat.. kiddin If you want to skip noncense processing of escape sequence just mention as r'"I'm going to shop"
     If you want to print \n as it is without using escape sequence use raw string

     Usefull in web url processing, were user is going to enter >> playpython.blogspot.com\index\python, note user wont give escape sequence for \ .
     URL's are considerd as raw string

     Unicode : <unicode link> Read this link, Unicode is standard way of representation of all identified languages script and symbols.
     Python supports unicode, if you want to write kannada word using python

    this is representation of unicode u"This is unicode string"

     <example of writing kannada>

    Strings are IMMUTABLE, ie once created string cannot be modified,  Y? ll  explain in later chapters

    NOTE: Try all the examples using your fav editor or use vi editor.

    "vi filename.py"    hit enter type all the code then   ": wq" to save and exit
    next "python filename.py" you should see output id no error, else you will see errors.. Debug it,, enjoy it, PLAY with it

    Example:
    ---------

    1) print "Hello world"

    2) I have mentioned earlier about "import" and  "from" keywords

    import os
    print dir(os)

    on executing this code you will see hell lot of comma seperated strings, these are functions and variables which are available to use with class "os"

    3)print os.curdir
    gives you current working directory

    4) >>> os.getpid
    <built-in function getpid>  -->  getpid is function so you shuld call like below

    5) >>> os.getpid()
    5391