basic_python/intro.rst
changeset 34 7a243a6d8625
parent 33 b850a2b9fc21
child 39 932a7a863120
equal deleted inserted replaced
33:b850a2b9fc21 34:7a243a6d8625
    58 * Python is a high level, interpreted, modular and object oriented language.
    58 * Python is a high level, interpreted, modular and object oriented language.
    59   Python performs memory management on its own, thus the programmer need not bother
    59   Python performs memory management on its own, thus the programmer need not bother
    60   about allocating and deallocating memory to variables. Python provides extensibility
    60   about allocating and deallocating memory to variables. Python provides extensibility
    61   by providing modules which can be easily imported similar to headers in C and 
    61   by providing modules which can be easily imported similar to headers in C and 
    62   packages in Java. Python is object oriented and hence provides all the object oriented
    62   packages in Java. Python is object oriented and hence provides all the object oriented
    63   characterstics such as inheritence, encapsulation and polymorphism.
    63   characteristics such as inheritance, encapsulation and polymorphism.
    64 
    64 
    65 * Python offers a highly powerful interactive programming interface in the form
    65 * Python offers a highly powerful interactive programming interface in the form
    66   of the 'Interactive Interpreter' which will be discussed in more detail in the 
    66   of the 'Interactive Interpreter' which will be discussed in more detail in the 
    67   following sections.
    67   following sections.
    68 
    68 
    79 
    79 
    80 
    80 
    81 1.1 The Python Interpreter
    81 1.1 The Python Interpreter
    82 --------------------------
    82 --------------------------
    83 
    83 
    84 Typing python at the shell prompt on any standard Unix/Gnu-Linux system fires up
    84 1.1.1 The Interactive Interpreter
    85 the Python 'Interactive Interpreter'. The Python interpreter is one of the most 
    85 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    86 integral features of Python. The prompt obtained when the interactive interpreter
    86 
    87 is similar to what is shown below. The exact appearance might differ based on the
    87 Typing *python* at the shell prompt on any standard Unix/Gnu-Linux system and
    88 version of Python being used. The ``>>>`` thing shown is the python prompt. 
    88 hitting the enter key fires up the Python 'Interactive Interpreter'. The Python
    89 When something is typed at the prompt and the enter key is hit, the python interpreter
    89 interpreter is one of the most integral features of Python. The prompt obtained
    90 interprets the command entered and performs the appropriate action.
    90 when the interactive interpreter is similar to what is shown below. The exact
       
    91 appearance might differ based on the version of Python being used. The ``>>>``
       
    92 thing shown is the python prompt. When something is typed at the prompt and the
       
    93 enter key is hit, the python interpreter interprets the command entered and
       
    94 performs the appropriate action.
    91 
    95 
    92 ::
    96 ::
    93 
    97 
    94   Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
    98   Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
    95   [GCC 4.3.2] on linux2
    99   [GCC 4.3.2] on linux2
    96   Type "help", "copyright", "credits" or "license" for more information.
   100   Type "help", "copyright", "credits" or "license" for more information.
    97   >>> 
   101   >>> 
    98 
   102 
       
   103 Lets try with an example, type ``print 'Hello, World!'`` at the prompt and hit
       
   104 the enter key. 
       
   105 
       
   106 ::
       
   107 
       
   108   >>> print 'Hello, World!'
       
   109   Hello, World!
       
   110 
       
   111 This example was quite straight forward, and thus we have written our first
       
   112 line of Python code. Now let us try typing something arbitrary at the prompt.
       
   113 For example: 
       
   114 
       
   115 ::
       
   116   
       
   117   >>> arbit word
       
   118     File "<stdin>", line 1
       
   119       arbit word
       
   120               ^
       
   121   SyntaxError: invalid syntax
       
   122   >>>
       
   123     
       
   124 The interpreter gave an error message saying that 'arbit word' was invalid
       
   125 syntax which is valid. The interpreter is an amazing tool when learning to
       
   126 program in Python. The interpreter provides a help function that provides the
       
   127 necessary documentation regarding all Python syntax, constructs, modules and
       
   128 objects. Typing *help()* at the prompt gives the following output:
       
   129 
       
   130 ::
       
   131   
       
   132   >>> help()
       
   133   
       
   134   Welcome to Python 2.5!  This is the online help utility.
       
   135   
       
   136   If this is your first time using Python, you should definitely check out
       
   137   the tutorial on the Internet at http://www.python.org/doc/tut/.
       
   138   
       
   139   Enter the name of any module, keyword, or topic to get help on writing
       
   140   Python programs and using Python modules.  To quit this help utility and
       
   141   return to the interpreter, just type "quit".
       
   142   
       
   143   To get a list of available modules, keywords, or topics, type "modules",
       
   144   "keywords", or "topics".  Each module also comes with a one-line summary
       
   145   of what it does; to list the modules whose summaries contain a given word
       
   146   such as "spam", type "modules spam".
       
   147   
       
   148   help> 
       
   149   
       
   150 
       
   151 As mentioned in the output, entering the name of any module, keyword or topic
       
   152 will provide the documentation and help regarding the same through the online
       
   153 help utility. Pressing *Ctrl+d* exits the help prompt and returns to the
       
   154 python prompt. 
       
   155 
       
   156 Let us now try a few examples at the python interpreter. 
       
   157 
       
   158 Eg 1:
       
   159 ::
       
   160   
       
   161   >>> print 'Hello, python!'
       
   162   Hello, python!
       
   163   >>>
       
   164   
       
   165 Eg 2:
       
   166 ::
       
   167   
       
   168   >>> print 4321*567890
       
   169   2453852690
       
   170   >>> 
       
   171   
       
   172 Eg 3:
       
   173 ::
       
   174   
       
   175   >>> 4321*567890
       
   176   2453852690L
       
   177   >>>
       
   178 
       
   179 ::
       
   180   
       
   181   Note: Notice the 'L' at the end of the output. The 'L' signifies that the
       
   182   output of the operation is of type *long*. It was absent in the previous
       
   183   example because we used the print statement. This is because *print* formats
       
   184   the output before displaying.
       
   185   
       
   186 Eg 4:
       
   187 ::
       
   188   
       
   189   >>> big = 12345678901234567890 ** 3
       
   190   >>> print big
       
   191   1881676372353657772490265749424677022198701224860897069000
       
   192   >>> 
       
   193 
       
   194 ::
       
   195   
       
   196   This example is to show that unlike in C or C++ there is no limit on the
       
   197   value of an integer.
       
   198 
       
   199 1.1.2 *ipython* - An enhanced interactive Python interpreter
       
   200 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       
   201 
       
   202 The power and the importance of the interactive interpreter was the highlight
       
   203 of the previous section. This section provides insight into the enhanced
       
   204 interpreter with more advanced set of features called **ipython**. Entering
       
   205 *ipython* at the shell prompt fires up the interactive interpreter. 
       
   206 
       
   207 ::
       
   208   
       
   209   $ ipython
       
   210   Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
       
   211   Type "copyright", "credits" or "license" for more information.
       
   212   
       
   213   IPython 0.8.4 -- An enhanced Interactive Python.
       
   214   ?         -> Introduction and overview of IPython's features.
       
   215   %quickref -> Quick reference.
       
   216   help      -> Python's own help system.
       
   217   object?   -> Details about 'object'. ?object also works, ?? prints more.
       
   218   
       
   219   In [1]: 
       
   220   
       
   221 This is the output obtained upon firing ipython. The exact appearance may
       
   222 change based on the Python version installed. The following are some of the
       
   223 various features provided by **ipython**:
       
   224   
       
   225   * Suggestions - ipython provides suggestions of the possible methods and
       
   226     operations available for the given python object.
       
   227 
       
   228 Eg:
       
   229   
       
   230 ::
       
   231   
       
   232   In [4]: a = 6
       
   233   
       
   234   In [5]: a.
       
   235   a.__abs__           a.__divmod__        a.__index__         a.__neg__       
       
   236     a.__rand__          a.__rmod__          a.__rxor__
       
   237   a.__add__           a.__doc__           a.__init__          a.__new__       
       
   238     a.__rdiv__          a.__rmul__          a.__setattr__
       
   239   a.__and__           a.__float__         a.__int__           a.__nonzero__   
       
   240     a.__rdivmod__       a.__ror__           a.__str__
       
   241   a.__class__         a.__floordiv__      a.__invert__        a.__oct__       
       
   242     a.__reduce__        a.__rpow__          a.__sub__
       
   243   a.__cmp__           a.__getattribute__  a.__long__          a.__or__        
       
   244     a.__reduce_ex__     a.__rrshift__       a.__truediv__
       
   245   a.__coerce__        a.__getnewargs__    a.__lshift__        a.__pos__      
       
   246     a.__repr__          a.__rshift__        a.__xor__
       
   247   a.__delattr__       a.__hash__          a.__mod__           a.__pow__      
       
   248     a.__rfloordiv__     a.__rsub__          
       
   249   a.__div__           a.__hex__           a.__mul__           a.__radd__     
       
   250     a.__rlshift__       a.__rtruediv__      
       
   251 
       
   252 In this example, we initialized 'a' (a variable - a concept that will be
       
   253 discussed in the subsequent sections.) to 6. In the next line when the *tab* key
       
   254 is pressed after typing '*a.*' ipython displays the set of all possible methods
       
   255 that are applicable on the object 'a' (an integer in this context). Ipython
       
   256 provides many such datatype specific features which will be presented in the
       
   257 further sections as and when the datatypes are introduced.
       
   258 
       
   259 1.2 Editing and running a python file
       
   260 -------------------------------------
       
   261 
       
   262 The