basic_python/intro.rst
author Santosh G. Vattam <vattam.santosh@gmail.com>
Wed, 26 Aug 2009 16:29:14 +0530
changeset 40 ccdbbc094579
parent 39 932a7a863120
child 41 b81255d096ab
permissions -rw-r--r--
Added content of basic datatypes.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
     1
============
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
     2
Basic Python
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
     3
============
1
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     4
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     5
This document is intended to be handed out at the end of the workshop. It has
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     6
been designed for Engineering students who are Python beginners and have basic
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     7
programming skills. The focus is on basic numerics and plotting using Python.
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     8
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     9
The system requirements:
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    10
  * Python - version 2.5.x or newer.
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
    11
  * IPython
1
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    12
  * Text editor - scite, vim, emacs or whatever you are comfortable with.
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    13
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
    14
Introduction
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
    15
============
1
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    16
31
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    17
The Python programming language was created by a dutch named Guido van Rossum.
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    18
The idea of Python was conceived in December 1989. The name Python has nothing
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    19
to do with the reptilian, but its been named after the 70s comedy series 
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    20
"Monty Python's Flying Circus", since it happens to be Guido's favourite 
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    21
TV series. 
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    22
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    23
Current stable version of Python is 2.6.x, although Python 3.0 is also the stable
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    24
version, it is not backwards compatible with the previous versions and is hence
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    25
not entirely popular at the moment. This material will focus on the 2.6.x series.
1
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    26
  
31
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    27
Python is licensed under the Python Software Foundation License (PSF License) 
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    28
which is GPL compatible Free Software license (excepting license version 1.6 and 2.0)
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    29
It is a no strings attached license, which means the source code is free to modify
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    30
and redistribute.
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    31
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    32
The Python docs define Python as "Python is an interpreted, object-oriented, 
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    33
high-level programming language with dynamic semantics." A more detailed summary
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    34
can be found at http://www.python.org/doc/essays/blurb.html. Python is a language that
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    35
has been designed to help the programmer concentrate on solving the problem at hand
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    36
and not worry about the programming language idiosyncrasies.
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    37
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    38
Python is a highly cross platform compatible language on account of it being an 
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    39
interpreted language. It is highly scalable and hence has been adapted to run on 
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    40
the Nokia 60 series phones. Python has been designed to be readable and easy to use
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    41
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    42
**Resources available for reference**
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    43
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    44
* Web: http://www.python.org
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    45
* Doc: http://www.python.org/doc
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    46
* Free Tutorials:
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    47
    * Official Python Tutorial: http://docs.python.org/tut/tut.html
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    48
    * Byte of Python: http://www.byteofpython.info/
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    49
    * Dive into Python: http://diveintopython.org/
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    50
32
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    51
**Advantages of Python - Why Python??**
31
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    52
32
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    53
* Python has been designed for readability and ease of use. Its been designed in 
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    54
  such a fashion that it imposes readability on the programmer. Python does away
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    55
  with the braces and the semicolons and instead implements code blocks based on 
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    56
  indentation, thus enhancing readability. 
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    57
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    58
* Python is a high level, interpreted, modular and object oriented language.
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    59
  Python performs memory management on its own, thus the programmer need not bother
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    60
  about allocating and deallocating memory to variables. Python provides extensibility
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    61
  by providing modules which can be easily imported similar to headers in C and 
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    62
  packages in Java. Python is object oriented and hence provides all the object oriented
34
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
    63
  characteristics such as inheritance, encapsulation and polymorphism.
32
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    64
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    65
* Python offers a highly powerful interactive programming interface in the form
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    66
  of the 'Interactive Interpreter' which will be discussed in more detail in the 
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    67
  following sections.
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    68
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    69
* Python provides a rich standard library and an extensive set of modules. The 
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    70
  power of Python modules can be seen in this slightly exaggerated cartoon
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    71
  http://xkcd.com/353/
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    72
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    73
* Python interfaces well with most other programming languages such as C, C++ 
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    74
  and FORTRAN.
31
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    75
33
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    76
Although, Python has one setback. Python is not fast as some of the compiled 
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    77
languages like C or C++. Yet, the amount of flexibility and power more than make
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    78
up for this setback.
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    79
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    80
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
    81
The Python Interpreter
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
    82
======================
31
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    83
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
    84
The Interactive Interpreter
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
    85
~~~~~~~~~~~~~~~~~~~~~~~~~~~
34
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
    86
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
    87
Typing *python* at the shell prompt on any standard Unix/Gnu-Linux system and
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
    88
hitting the enter key fires up the Python 'Interactive Interpreter'. The Python
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
    89
interpreter is one of the most integral features of Python. The prompt obtained
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
    90
when the interactive interpreter is similar to what is shown below. The exact
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
    91
appearance might differ based on the version of Python being used. The ``>>>``
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
    92
thing shown is the python prompt. When something is typed at the prompt and the
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
    93
enter key is hit, the python interpreter interprets the command entered and
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
    94
performs the appropriate action.
33
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    95
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    96
::
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    97
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    98
  Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    99
  [GCC 4.3.2] on linux2
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
   100
  Type "help", "copyright", "credits" or "license" for more information.
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
   101
  >>> 
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
   102
34
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   103
Lets try with an example, type ``print 'Hello, World!'`` at the prompt and hit
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   104
the enter key. 
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   105
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   106
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   107
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   108
  >>> print 'Hello, World!'
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   109
  Hello, World!
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   110
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   111
This example was quite straight forward, and thus we have written our first
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   112
line of Python code. Now let us try typing something arbitrary at the prompt.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   113
For example: 
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   114
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   115
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   116
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   117
  >>> arbit word
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   118
    File "<stdin>", line 1
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   119
      arbit word
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   120
              ^
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   121
  SyntaxError: invalid syntax
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   122
  >>>
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   123
    
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   124
The interpreter gave an error message saying that 'arbit word' was invalid
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   125
syntax which is valid. The interpreter is an amazing tool when learning to
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   126
program in Python. The interpreter provides a help function that provides the
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   127
necessary documentation regarding all Python syntax, constructs, modules and
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   128
objects. Typing *help()* at the prompt gives the following output:
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   129
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   130
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   131
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   132
  >>> help()
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   133
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   134
  Welcome to Python 2.5!  This is the online help utility.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   135
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   136
  If this is your first time using Python, you should definitely check out
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   137
  the tutorial on the Internet at http://www.python.org/doc/tut/.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   138
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   139
  Enter the name of any module, keyword, or topic to get help on writing
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   140
  Python programs and using Python modules.  To quit this help utility and
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   141
  return to the interpreter, just type "quit".
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   142
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   143
  To get a list of available modules, keywords, or topics, type "modules",
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   144
  "keywords", or "topics".  Each module also comes with a one-line summary
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   145
  of what it does; to list the modules whose summaries contain a given word
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   146
  such as "spam", type "modules spam".
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   147
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   148
  help> 
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   149
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   150
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   151
As mentioned in the output, entering the name of any module, keyword or topic
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   152
will provide the documentation and help regarding the same through the online
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   153
help utility. Pressing *Ctrl+d* exits the help prompt and returns to the
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   154
python prompt. 
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   155
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   156
Let us now try a few examples at the python interpreter. 
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   157
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   158
Eg 1:
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   159
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   160
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   161
  >>> print 'Hello, python!'
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   162
  Hello, python!
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   163
  >>>
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   164
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   165
Eg 2:
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   166
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   167
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   168
  >>> print 4321*567890
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   169
  2453852690
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   170
  >>> 
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   171
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   172
Eg 3:
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   173
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   174
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   175
  >>> 4321*567890
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   176
  2453852690L
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   177
  >>>
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   178
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   179
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   180
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   181
  Note: Notice the 'L' at the end of the output. The 'L' signifies that the
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   182
  output of the operation is of type *long*. It was absent in the previous
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   183
  example because we used the print statement. This is because *print* formats
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   184
  the output before displaying.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   185
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   186
Eg 4:
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   187
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   188
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   189
  >>> big = 12345678901234567890 ** 3
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   190
  >>> print big
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   191
  1881676372353657772490265749424677022198701224860897069000
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   192
  >>> 
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   193
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   194
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   195
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   196
  This example is to show that unlike in C or C++ there is no limit on the
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   197
  value of an integer.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   198
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   199
*ipython* - An enhanced interactive Python interpreter
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   200
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   201
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   202
The power and the importance of the interactive interpreter was the highlight
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   203
of the previous section. This section provides insight into the enhanced
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   204
interpreter with more advanced set of features called **ipython**. Entering
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   205
*ipython* at the shell prompt fires up the interactive interpreter. 
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   206
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   207
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   208
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   209
  $ ipython
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   210
  Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   211
  Type "copyright", "credits" or "license" for more information.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   212
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   213
  IPython 0.8.4 -- An enhanced Interactive Python.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   214
  ?         -> Introduction and overview of IPython's features.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   215
  %quickref -> Quick reference.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   216
  help      -> Python's own help system.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   217
  object?   -> Details about 'object'. ?object also works, ?? prints more.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   218
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   219
  In [1]: 
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   220
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   221
This is the output obtained upon firing ipython. The exact appearance may
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   222
change based on the Python version installed. The following are some of the
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   223
various features provided by **ipython**:
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   224
  
40
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   225
    Suggestions - ipython provides suggestions of the possible methods and
34
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   226
    operations available for the given python object.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   227
40
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   228
Eg 5:
34
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   229
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   230
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   231
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   232
  In [4]: a = 6
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   233
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   234
  In [5]: a.
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   235
  a.__abs__           a.__divmod__        a.__index__         a.__neg__          a.__rand__          a.__rmod__          a.__rxor__
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   236
  a.__add__           a.__doc__           a.__init__          a.__new__          a.__rdiv__          a.__rmul__          a.__setattr__
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   237
  a.__and__           a.__float__         a.__int__           a.__nonzero__      a.__rdivmod__       a.__ror__           a.__str__
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   238
  a.__class__         a.__floordiv__      a.__invert__        a.__oct__          a.__reduce__        a.__rpow__          a.__sub__
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   239
  a.__cmp__           a.__getattribute__  a.__long__          a.__or__           a.__reduce_ex__     a.__rrshift__       a.__truediv__
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   240
  a.__coerce__        a.__getnewargs__    a.__lshift__        a.__pos__          a.__repr__          a.__rshift__        a.__xor__
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   241
  a.__delattr__       a.__hash__          a.__mod__           a.__pow__          a.__rfloordiv__     a.__rsub__          
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   242
  a.__div__           a.__hex__           a.__mul__           a.__radd__         a.__rlshift__       a.__rtruediv__      
34
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   243
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   244
In this example, we initialized 'a' (a variable - a concept that will be
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   245
discussed in the subsequent sections.) to 6. In the next line when the *tab* key
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   246
is pressed after typing '*a.*' ipython displays the set of all possible methods
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   247
that are applicable on the object 'a' (an integer in this context). Ipython
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   248
provides many such datatype specific features which will be presented in the
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   249
further sections as and when the datatypes are introduced.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   250
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   251
Editing and running a python file
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   252
=================================
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   253
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   254
The previous sections focused on the use of the interpreter to run python code.
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   255
While the interpeter is an excellent tool to test simple solutions and
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   256
experiment with small code snippets, its main disadvantage is that everything
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   257
written in the interpreter is lost once its quit. Most of the times a program is 
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   258
used by people other than the author. So the programs have to be available in 
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   259
some form suitable for distribution, and hence they are written in files. This 
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   260
section will focus on editing and running python files. Start by opening a text 
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   261
editor ( it is recommended you choose one from the list at the top of this page ).
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   262
In the editor type down python code and save the file with an extension **.py** 
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   263
(python files have an extension of .py). Once done with the editing, save the 
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   264
file and exit the editor. 
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   265
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   266
Let us look at a simple example of calculating the gcd of 2 numbers using Python:
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   267
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   268
**Creating the first python script(file)**
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   269
::
34
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   270
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   271
  $ emacs gcd.py
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   272
    def gcd(x,y):
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   273
      if x % y == 0:
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   274
        return y
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   275
      return gcd(y, x%y)
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   276
  
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   277
    print gcd(72, 92)
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   278
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   279
To run the script, open the shell prompt, navigate to the directory that 
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   280
contains the python file and run `python <filename.py>` at the prompt ( in this 
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   281
case filename is gcd.py )
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   282
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   283
**Running the python script**
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   284
::
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   285
  
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   286
  $ python gcd.py
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   287
  4
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   288
  $ 
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   289
40
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   290
Another method to run a python script would be to include the line
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   291
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   292
`#! /usr/bin/python`
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   293
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   294
at the beginning of the python file and then make the file executable by 
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   295
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   296
$ chmod a+x *filename.py*
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   297
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   298
Once this is done, the script can be run as a standalone program as follows:
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   299
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   300
$ ./*filename.py*
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   301
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   302
Basic Datatypes and operators in Python
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   303
=======================================
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   304
40
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   305
Python provides the following set of basic datatypes.
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   306
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   307
  * Numbers: int, float, long, complex
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   308
  * Strings
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   309
  * Boolean
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   310
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   311
Numbers
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   312
~~~~~~~
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   313
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   314
Numbers were introduced in the examples presented in the interactive interpreter
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   315
section. Numbers include types as mentioned earlier viz., int (integers), float 
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   316
(floating point numbers), long (large integers), complex (complex numbers with 
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   317
real and imaginary parts). Python is not a strongly typed language, which means 
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   318
the type of a variable need not mentioned during its initialization. Let us look
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   319
at a few examples.
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   320
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   321
Eg 6:
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   322
::
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   323
  
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   324
  >>> a = 1 #here a is an integer variable
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   325
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   326
Eg 7:
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   327
::
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   328
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   329
  >>> lng = 122333444455555666666777777788888888999999999 #here lng is a variable of type long
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   330
  >>> lng
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   331
  122333444455555666666777777788888888999999999L #notice the trailing 'L'
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   332
  >>> print lng
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   333
  122333444455555666666777777788888888999999999 #notice the absence of the trailing 'L'
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   334
  >>> lng+1
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   335
  122333444455555666666777777788888889000000000L
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   336
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   337
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   338
Long numbers are the same as integers in almost all aspects. They can be used in
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   339
operations just like integers and along with integers without any distinction.
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   340
The only distinction comes during type checking (which is not a healthy practice).
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   341
Long numbers are tucked with a trailing 'L' just to signify that they are long.
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   342
Notice that in the example just lng at the prompt displays the value of the variable
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   343
with the 'L' whereas `print lng` displays without the 'L'. This is because print 
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   344
formats the output before printing. Also in the example, notice that adding an 
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   345
integer to a long does not give any errors and the result is as expected. So for
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   346
all practical purposes longs can be treated as ints.
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   347
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   348
Eg 8:
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   349
::
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   350
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   351
  >>> fl = 3.14159 #fl is a float variable
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   352
  >>> e = 1.234e-4 #e is also a float variable, specified in the exponential form
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   353
  >>> a = 1
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   354
  >>> b = 2
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   355
  >>> a/b #integer division
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   356
  0
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   357
  >>> a/fl #floating point division
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   358
  0.31831015504887655
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   359
  >>> e/fl
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   360
  3.9279473133031364e-05
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   361
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   362
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   363
Floating point numbers, simply called floats are real numbers with a decimal point.
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   364
The example above shows the initialization of a float variable. Shown also in this
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   365
example is the difference between integer division and floating point division.
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   366
'a' and 'b' here are integer variables and hence the division gives 0 as the quotient.
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   367
When either of the operands is a float, the operation is a floating point division,
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   368
and the result is also a float as illustrated.
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   369
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   370
Eg 9:
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   371
::
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   372
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   373
  >>> cplx = 3 + 4j #cplx is a complex variable
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   374
  >>> cplx
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   375
  (3+4j)
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   376
  >>> print cplx.real #prints the real part of the complex number
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   377
  3.0
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   378
  >>> print cplx.imag #prints the imaginary part of the complex number
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   379
  4.0
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   380
  >>> print cplx*fl  #multiplies the real and imag parts of the complex number with the multiplier
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   381
  (9.42477+12.56636j)
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   382
  >>> abs(cplx) #returns the absolute value of the complex number
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   383
  5.0
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   384
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   385
Python provides a datatype for complex numbers. Complex numbers are initialized 
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   386
as shown in the example above. The *real* and *imag* operators return the real and
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   387
imaginary parts of the complex number as shown. The *abs()* returens the absolute
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   388
value of the complex number.