basic_python/intro.rst
changeset 39 932a7a863120
parent 34 7a243a6d8625
child 40 ccdbbc094579
equal deleted inserted replaced
35:8f43cab360aa 39:932a7a863120
     1 =====================
     1 ============
     2 Basic Python Workshop
     2 Basic Python
     3 =====================
     3 ============
     4 
     4 
     5 This document is intended to be handed out at the end of the workshop. It has
     5 This document is intended to be handed out at the end of the workshop. It has
     6 been designed for Engineering students who are Python beginners and have basic
     6 been designed for Engineering students who are Python beginners and have basic
     7 programming skills. The focus is on basic numerics and plotting using Python.
     7 programming skills. The focus is on basic numerics and plotting using Python.
     8 
     8 
     9 The system requirements:
     9 The system requirements:
    10   * Python - version 2.5.x or newer.
    10   * Python - version 2.5.x or newer.
    11   * IPython 
    11   * IPython
    12   * Text editor - scite, vim, emacs or whatever you are comfortable with.
    12   * Text editor - scite, vim, emacs or whatever you are comfortable with.
    13 
    13 
    14 1. Introduction
    14 Introduction
    15 ===============
    15 ============
    16 
    16 
    17 The Python programming language was created by a dutch named Guido van Rossum.
    17 The Python programming language was created by a dutch named Guido van Rossum.
    18 The idea of Python was conceived in December 1989. The name Python has nothing
    18 The idea of Python was conceived in December 1989. The name Python has nothing
    19 to do with the reptilian, but its been named after the 70s comedy series 
    19 to do with the reptilian, but its been named after the 70s comedy series 
    20 "Monty Python's Flying Circus", since it happens to be Guido's favourite 
    20 "Monty Python's Flying Circus", since it happens to be Guido's favourite 
    76 Although, Python has one setback. Python is not fast as some of the compiled 
    76 Although, Python has one setback. Python is not fast as some of the compiled 
    77 languages like C or C++. Yet, the amount of flexibility and power more than make
    77 languages like C or C++. Yet, the amount of flexibility and power more than make
    78 up for this setback.
    78 up for this setback.
    79 
    79 
    80 
    80 
    81 1.1 The Python Interpreter
    81 The Python Interpreter
    82 --------------------------
    82 ======================
    83 
    83 
    84 1.1.1 The Interactive Interpreter
    84 The Interactive Interpreter
    85 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    85 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    86 
    86 
    87 Typing *python* at the shell prompt on any standard Unix/Gnu-Linux system and
    87 Typing *python* at the shell prompt on any standard Unix/Gnu-Linux system and
    88 hitting the enter key fires up the Python 'Interactive Interpreter'. The Python
    88 hitting the enter key fires up the Python 'Interactive Interpreter'. The Python
    89 interpreter is one of the most integral features of Python. The prompt obtained
    89 interpreter is one of the most integral features of Python. The prompt obtained
    90 when the interactive interpreter is similar to what is shown below. The exact
    90 when the interactive interpreter is similar to what is shown below. The exact
   194 ::
   194 ::
   195   
   195   
   196   This example is to show that unlike in C or C++ there is no limit on the
   196   This example is to show that unlike in C or C++ there is no limit on the
   197   value of an integer.
   197   value of an integer.
   198 
   198 
   199 1.1.2 *ipython* - An enhanced interactive Python interpreter
   199 *ipython* - An enhanced interactive Python interpreter
   200 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   200 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   201 
   201 
   202 The power and the importance of the interactive interpreter was the highlight
   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
   203 of the previous section. This section provides insight into the enhanced
   204 interpreter with more advanced set of features called **ipython**. Entering
   204 interpreter with more advanced set of features called **ipython**. Entering
   205 *ipython* at the shell prompt fires up the interactive interpreter. 
   205 *ipython* at the shell prompt fires up the interactive interpreter. 
   230 ::
   230 ::
   231   
   231   
   232   In [4]: a = 6
   232   In [4]: a = 6
   233   
   233   
   234   In [5]: a.
   234   In [5]: a.
   235   a.__abs__           a.__divmod__        a.__index__         a.__neg__       
   235   a.__abs__           a.__divmod__        a.__index__         a.__neg__          a.__rand__          a.__rmod__          a.__rxor__
   236     a.__rand__          a.__rmod__          a.__rxor__
   236   a.__add__           a.__doc__           a.__init__          a.__new__          a.__rdiv__          a.__rmul__          a.__setattr__
   237   a.__add__           a.__doc__           a.__init__          a.__new__       
   237   a.__and__           a.__float__         a.__int__           a.__nonzero__      a.__rdivmod__       a.__ror__           a.__str__
   238     a.__rdiv__          a.__rmul__          a.__setattr__
   238   a.__class__         a.__floordiv__      a.__invert__        a.__oct__          a.__reduce__        a.__rpow__          a.__sub__
   239   a.__and__           a.__float__         a.__int__           a.__nonzero__   
   239   a.__cmp__           a.__getattribute__  a.__long__          a.__or__           a.__reduce_ex__     a.__rrshift__       a.__truediv__
   240     a.__rdivmod__       a.__ror__           a.__str__
   240   a.__coerce__        a.__getnewargs__    a.__lshift__        a.__pos__          a.__repr__          a.__rshift__        a.__xor__
   241   a.__class__         a.__floordiv__      a.__invert__        a.__oct__       
   241   a.__delattr__       a.__hash__          a.__mod__           a.__pow__          a.__rfloordiv__     a.__rsub__          
   242     a.__reduce__        a.__rpow__          a.__sub__
   242   a.__div__           a.__hex__           a.__mul__           a.__radd__         a.__rlshift__       a.__rtruediv__      
   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 
   243 
   252 In this example, we initialized 'a' (a variable - a concept that will be
   244 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
   245 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
   246 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
   247 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
   248 provides many such datatype specific features which will be presented in the
   257 further sections as and when the datatypes are introduced.
   249 further sections as and when the datatypes are introduced.
   258 
   250 
   259 1.2 Editing and running a python file
   251 Editing and running a python file
   260 -------------------------------------
   252 =================================
   261 
   253 
   262 The 
   254 The previous sections focused on the use of the interpreter to run python code.
       
   255 While the interpeter is an excellent tool to test simple solutions and
       
   256 experiment with small code snippets, its main disadvantage is that everything
       
   257 written in the interpreter is lost once its quit. Most of the times a program is 
       
   258 used by people other than the author. So the programs have to be available in 
       
   259 some form suitable for distribution, and hence they are written in files. This 
       
   260 section will focus on editing and running python files. Start by opening a text 
       
   261 editor ( it is recommended you choose one from the list at the top of this page ).
       
   262 In the editor type down python code and save the file with an extension **.py** 
       
   263 (python files have an extension of .py). Once done with the editing, save the 
       
   264 file and exit the editor. 
       
   265 
       
   266 Let us look at a simple example of calculating the gcd of 2 numbers using Python:
       
   267 
       
   268 **Creating the first python script(file)**
       
   269 ::
       
   270 
       
   271   $ emacs gcd.py
       
   272     def gcd(x,y):
       
   273       if x % y == 0:
       
   274         return y
       
   275       return gcd(y, x%y)
       
   276   
       
   277     print gcd(72, 92)
       
   278 
       
   279 To run the script, open the shell prompt, navigate to the directory that 
       
   280 contains the python file and run `python <filename.py>` at the prompt ( in this 
       
   281 case filename is gcd.py )
       
   282 
       
   283 **Running the python script**
       
   284 ::
       
   285   
       
   286   $ python gcd.py
       
   287   4
       
   288   $ 
       
   289 
       
   290 Basic Datatypes and operators in Python
       
   291 =======================================
       
   292