sttp/basic_python/intro.rst
author amit@thunder
Tue, 02 Mar 2010 18:52:05 +0530
changeset 1 a9832fcf7c96
parent 0 27e1f5bd2774
permissions -rw-r--r--
test
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     1
============
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     2
Basic Python
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     3
============
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     4
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     5
This document is intended to be handed out at the end of the workshop. It has
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     6
been designed for Engineering students who are Python beginners and have basic
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     7
programming skills. The focus is on basic numerics and plotting using Python.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     8
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     9
The system requirements:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    10
  * Python - version 2.5.x or newer.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    11
  * IPython
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    12
  * Text editor - scite, vim, emacs or whatever you are comfortable with.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    13
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    14
Introduction
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    15
============
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    16
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    17
The Python programming language was created by a dutch named Guido van Rossum.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    18
The idea of Python was conceived in December 1989. The name Python has nothing
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    19
to do with the reptilian, but its been named after the 70s comedy series 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    20
"Monty Python's Flying Circus", since it happens to be Guido's favourite 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    21
TV series. 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    22
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    23
It is a test.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    24
This is a test
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    25
1
amit@thunder
parents: 0
diff changeset
    26
This is a test lets see what happens
amit@thunder
parents: 0
diff changeset
    27
amit@thunder
parents: 0
diff changeset
    28
0
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    29
Current stable version of Python is 2.6.x, although Python 3.0 is also the stable
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    30
version, it is not backwards compatible with the previous versions and is hence
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    31
not entirely popular at the moment. This material will focus on the 2.6.x series.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    32
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    33
Python is licensed under the Python Software Foundation License (PSF License) 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    34
which is GPL compatible Free Software license (excepting license version 1.6 and 2.0)
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    35
It is a no strings attached license, which means the source code is free to modify
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    36
and redistribute.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    37
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    38
The Python docs define Python as "Python is an interpreted, object-oriented, 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    39
high-level programming language with dynamic semantics." A more detailed summary
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    40
can be found at http://www.python.org/doc/essays/blurb.html. Python is a language that
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    41
has been designed to help the programmer concentrate on solving the problem at hand
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    42
and not worry about the programming language idiosyncrasies.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    43
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    44
Python is a highly cross platform compatible language on account of it being an 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    45
interpreted language. It is highly scalable and hence has been adapted to run on 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    46
the Nokia 60 series phones. Python has been designed to be readable and easy to use
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    47
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    48
**Resources available for reference**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    49
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    50
* Web: http://www.python.org
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    51
* Doc: http://www.python.org/doc
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    52
* Free Tutorials:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    53
    * Official Python Tutorial: http://docs.python.org/tut/tut.html
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    54
    * Byte of Python: http://www.byteofpython.info/
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    55
    * Dive into Python: http://diveintopython.org/
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    56
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    57
**Advantages of Python - Why Python??**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    58
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    59
* Python has been designed for readability and ease of use. Its been designed in 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    60
  such a fashion that it imposes readability on the programmer. Python does away
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    61
  with the braces and the semicolons and instead implements code blocks based on 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    62
  indentation, thus enhancing readability. 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    63
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    64
* Python is a high level, interpreted, modular and object oriented language.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    65
  Python performs memory management on its own, thus the programmer need not bother
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    66
  about allocating and deallocating memory to variables. Python provides extensibility
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    67
  by providing modules which can be easily imported similar to headers in C and 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    68
  packages in Java. Python is object oriented and hence provides all the object oriented
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    69
  characteristics such as inheritance, encapsulation and polymorphism.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    70
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    71
* Python offers a highly powerful interactive programming interface in the form
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    72
  of the 'Interactive Interpreter' which will be discussed in more detail in the 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    73
  following sections.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    74
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    75
* Python provides a rich standard library and an extensive set of modules. The 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    76
  power of Python modules can be seen in this slightly exaggerated cartoon
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    77
  http://xkcd.com/353/
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    78
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    79
* Python interfaces well with most other programming languages such as C, C++ 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    80
  and FORTRAN.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    81
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    82
Although, Python has one setback. Python is not fast as some of the compiled 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    83
languages like C or C++. Yet, the amount of flexibility and power more than make
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    84
up for this setback.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    85
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    86
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    87
The Python Interpreter
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    88
======================
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    89
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    90
The Interactive Interpreter
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    91
~~~~~~~~~~~~~~~~~~~~~~~~~~~
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    92
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    93
Typing *python* at the shell prompt on any standard Unix/Gnu-Linux system and
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    94
hitting the enter key fires up the Python 'Interactive Interpreter'. The Python
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    95
interpreter is one of the most integral features of Python. The prompt obtained
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    96
when the interactive interpreter is similar to what is shown below. The exact
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    97
appearance might differ based on the version of Python being used. The ``>>>``
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    98
thing shown is the python prompt. When something is typed at the prompt and the
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    99
enter key is hit, the python interpreter interprets the command entered and
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   100
performs the appropriate action. All the examples presented in this document are
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   101
to be tried hands on, on the interactive interpreter.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   102
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   103
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   104
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   105
  Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   106
  [GCC 4.3.2] on linux2
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   107
  Type "help", "copyright", "credits" or "license" for more information.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   108
  >>> 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   109
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   110
Lets try with an example, type ``print 'Hello, World!'`` at the prompt and hit
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   111
the enter key. 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   112
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   113
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   114
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   115
  >>> print 'Hello, World!'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   116
  Hello, World!
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   117
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   118
This example was quite straight forward, and thus we have written our first
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   119
line of Python code. Now let us try typing something arbitrary at the prompt.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   120
For example: 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   121
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   122
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   123
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   124
  >>> arbit word
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   125
    File "<stdin>", line 1
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   126
      arbit word
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   127
              ^
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   128
  SyntaxError: invalid syntax
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   129
  >>>
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   130
    
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   131
The interpreter gave an error message saying that 'arbit word' was invalid
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   132
syntax which is valid. The interpreter is an amazing tool when learning to
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   133
program in Python. The interpreter provides a help function that provides the
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   134
necessary documentation regarding all Python syntax, constructs, modules and
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   135
objects. Typing *help()* at the prompt gives the following output:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   136
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   137
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   138
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   139
  >>> help()
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   140
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   141
  Welcome to Python 2.5!  This is the online help utility.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   142
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   143
  If this is your first time using Python, you should definitely check out
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   144
  the tutorial on the Internet at http://www.python.org/doc/tut/.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   145
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   146
  Enter the name of any module, keyword, or topic to get help on writing
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   147
  Python programs and using Python modules.  To quit this help utility and
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   148
  return to the interpreter, just type "quit".
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   149
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   150
  To get a list of available modules, keywords, or topics, type "modules",
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   151
  "keywords", or "topics".  Each module also comes with a one-line summary
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   152
  of what it does; to list the modules whose summaries contain a given word
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   153
  such as "spam", type "modules spam".
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   154
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   155
  help> 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   156
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   157
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   158
As mentioned in the output, entering the name of any module, keyword or topic
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   159
will provide the documentation and help regarding the same through the online
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   160
help utility. Pressing *Ctrl+d* exits the help prompt and returns to the
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   161
python prompt. 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   162
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   163
Let us now try a few examples at the python interpreter. 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   164
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   165
Eg 1:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   166
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   167
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   168
  >>> print 'Hello, python!'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   169
  Hello, python!
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   170
  >>>
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   171
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   172
Eg 2:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   173
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   174
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   175
  >>> print 4321*567890
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   176
  2453852690
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   177
  >>> 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   178
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   179
Eg 3:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   180
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   181
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   182
  >>> 4321*567890
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   183
  2453852690L
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   184
  >>>
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   185
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   186
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   187
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   188
  Note: Notice the 'L' at the end of the output. The 'L' signifies that the
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   189
  output of the operation is of type *long*. It was absent in the previous
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   190
  example because we used the print statement. This is because *print* formats
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   191
  the output before displaying.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   192
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   193
Eg 4:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   194
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   195
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   196
  >>> big = 12345678901234567890 ** 3
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   197
  >>> print big
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   198
  1881676372353657772490265749424677022198701224860897069000
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   199
  >>> 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   200
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   201
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   202
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   203
  This example is to show that unlike in C or C++ there is no limit on the
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   204
  value of an integer.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   205
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   206
Try this on the interactive interpreter:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   207
``import this``
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   208
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   209
*Hint: The output gives an idea of Power of Python*
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   210
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   211
*ipython* - An enhanced interactive Python interpreter
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   212
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   213
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   214
The power and the importance of the interactive interpreter was the highlight
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   215
of the previous section. This section provides insight into the enhanced
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   216
interpreter with more advanced set of features called **ipython**. Entering
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   217
*ipython* at the shell prompt fires up the interactive interpreter. 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   218
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   219
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   220
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   221
  $ ipython
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   222
  Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   223
  Type "copyright", "credits" or "license" for more information.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   224
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   225
  IPython 0.8.4 -- An enhanced Interactive Python.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   226
  ?         -> Introduction and overview of IPython's features.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   227
  %quickref -> Quick reference.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   228
  help      -> Python's own help system.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   229
  object?   -> Details about 'object'. ?object also works, ?? prints more.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   230
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   231
  In [1]: 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   232
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   233
This is the output obtained upon firing ipython. The exact appearance may
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   234
change based on the Python version installed. The following are some of the
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   235
various features provided by **ipython**:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   236
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   237
    Suggestions - ipython provides suggestions of the possible methods and
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   238
    operations available for the given python object.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   239
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   240
Eg 5:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   241
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   242
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   243
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   244
  In [4]: a = 6
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   245
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   246
  In [5]: a.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   247
  a.__abs__           a.__divmod__        a.__index__         a.__neg__          a.__rand__          a.__rmod__          a.__rxor__
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   248
  a.__add__           a.__doc__           a.__init__          a.__new__          a.__rdiv__          a.__rmul__          a.__setattr__
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   249
  a.__and__           a.__float__         a.__int__           a.__nonzero__      a.__rdivmod__       a.__ror__           a.__str__
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   250
  a.__class__         a.__floordiv__      a.__invert__        a.__oct__          a.__reduce__        a.__rpow__          a.__sub__
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   251
  a.__cmp__           a.__getattribute__  a.__long__          a.__or__           a.__reduce_ex__     a.__rrshift__       a.__truediv__
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   252
  a.__coerce__        a.__getnewargs__    a.__lshift__        a.__pos__          a.__repr__          a.__rshift__        a.__xor__
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   253
  a.__delattr__       a.__hash__          a.__mod__           a.__pow__          a.__rfloordiv__     a.__rsub__          
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   254
  a.__div__           a.__hex__           a.__mul__           a.__radd__         a.__rlshift__       a.__rtruediv__      
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   255
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   256
In this example, we initialized 'a' (a variable - a concept that will be
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   257
discussed in the subsequent sections.) to 6. In the next line when the *tab* key
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   258
is pressed after typing '*a.*' ipython displays the set of all possible methods
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   259
that are applicable on the object 'a' (an integer in this context). Ipython
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   260
provides many such datatype specific features which will be presented in the
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   261
further sections as and when the datatypes are introduced.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   262
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   263
Editing and running a python file
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   264
=================================
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   265
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   266
The previous sections focused on the use of the interpreter to run python code.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   267
While the interpeter is an excellent tool to test simple solutions and
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   268
experiment with small code snippets, its main disadvantage is that everything
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   269
written in the interpreter is lost once its quit. Most of the times a program is 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   270
used by people other than the author. So the programs have to be available in 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   271
some form suitable for distribution, and hence they are written in files. This 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   272
section will focus on editing and running python files. Start by opening a text 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   273
editor ( it is recommended you choose one from the list at the top of this page ).
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   274
In the editor type down python code and save the file with an extension **.py** 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   275
(python files have an extension of .py). Once done with the editing, save the 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   276
file and exit the editor. 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   277
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   278
Let us look at a simple example of calculating the gcd of 2 numbers using Python:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   279
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   280
**Creating the first python script(file)**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   281
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   282
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   283
  $ emacs gcd.py
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   284
    def gcd(x,y):
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   285
      if x % y == 0:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   286
        return y
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   287
      return gcd(y, x%y)
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   288
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   289
    print gcd(72, 92)
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   290
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   291
To run the script, open the shell prompt, navigate to the directory that 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   292
contains the python file and run ``python <filename.py>`` at the prompt ( in this 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   293
case filename is gcd.py )
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   294
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   295
**Running the python script**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   296
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   297
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   298
  $ python gcd.py
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   299
  4
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   300
  $ 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   301
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   302
Another method to run a python script would be to include the line
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   303
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   304
``#! /usr/bin/python``
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   305
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   306
at the beginning of the python file and then make the file executable by 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   307
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   308
$ chmod a+x *filename.py*
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   309
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   310
Once this is done, the script can be run as a standalone program as follows:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   311
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   312
$ ./*filename.py*
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   313
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   314
Basic Datatypes and operators in Python
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   315
=======================================
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   316
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   317
Python provides the following set of basic datatypes.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   318
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   319
  * Numbers: int, float, long, complex
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   320
  * Strings
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   321
  * Boolean
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   322
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   323
Numbers
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   324
~~~~~~~
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   325
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   326
Numbers were introduced in the examples presented in the interactive interpreter
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   327
section. Numbers include types as mentioned earlier viz., int (integers), float 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   328
(floating point numbers), long (large integers), complex (complex numbers with 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   329
real and imaginary parts). Python is not a strongly typed language, which means 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   330
the type of a variable need not mentioned during its initialization. Let us look
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   331
at a few examples.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   332
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   333
Eg 6:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   334
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   335
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   336
  >>> a = 1 #here a is an integer variable
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   337
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   338
Eg 7:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   339
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   340
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   341
  >>> lng = 122333444455555666666777777788888888999999999 #here lng is a variable of type long
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   342
  >>> lng
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   343
  122333444455555666666777777788888888999999999L #notice the trailing 'L'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   344
  >>> print lng
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   345
  122333444455555666666777777788888888999999999 #notice the absence of the trailing 'L'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   346
  >>> lng+1
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   347
  122333444455555666666777777788888889000000000L
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   348
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   349
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   350
Long numbers are the same as integers in almost all aspects. They can be used in
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   351
operations just like integers and along with integers without any distinction.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   352
The only distinction comes during type checking (which is not a healthy practice).
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   353
Long numbers are tucked with a trailing 'L' just to signify that they are long.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   354
Notice that in the example just lng at the prompt displays the value of the variable
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   355
with the 'L' whereas ``print lng`` displays without the 'L'. This is because print 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   356
formats the output before printing. Also in the example, notice that adding an 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   357
integer to a long does not give any errors and the result is as expected. So for
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   358
all practical purposes longs can be treated as ints.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   359
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   360
Eg 8:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   361
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   362
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   363
  >>> fl = 3.14159 #fl is a float variable
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   364
  >>> e = 1.234e-4 #e is also a float variable, specified in the exponential form
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   365
  >>> a = 1
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   366
  >>> b = 2
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   367
  >>> a/b #integer division
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   368
  0
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   369
  >>> a/fl #floating point division
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   370
  0.31831015504887655
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   371
  >>> e/fl
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   372
  3.9279473133031364e-05
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   373
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   374
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   375
Floating point numbers, simply called floats are real numbers with a decimal point.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   376
The example above shows the initialization of a float variable. Shown also in this
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   377
example is the difference between integer division and floating point division.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   378
'a' and 'b' here are integer variables and hence the division gives 0 as the quotient.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   379
When either of the operands is a float, the operation is a floating point division,
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   380
and the result is also a float as illustrated.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   381
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   382
Eg 9:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   383
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   384
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   385
  >>> cplx = 3 + 4j #cplx is a complex variable
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   386
  >>> cplx
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   387
  (3+4j)
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   388
  >>> print cplx.real #prints the real part of the complex number
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   389
  3.0
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   390
  >>> print cplx.imag #prints the imaginary part of the complex number
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   391
  4.0
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   392
  >>> print cplx*fl  #multiplies the real and imag parts of the complex number with the multiplier
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   393
  (9.42477+12.56636j)
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   394
  >>> abs(cplx) #returns the absolute value of the complex number
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   395
  5.0
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   396
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   397
Python provides a datatype for complex numbers. Complex numbers are initialized 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   398
as shown in the example above. The *real* and *imag* operators return the real and
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   399
imaginary parts of the complex number as shown. The *abs()* returns the absolute
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   400
value of the complex number.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   401
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   402
Variables
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   403
~~~~~~~~~
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   404
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   405
Variables are just names that represent a value. Variables have already been 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   406
introduced in the various examples from the previous sections. Certain rules about
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   407
using variables:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   408
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   409
  * Variables have to be initialized or assigned a value before being used.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   410
  * Variable names can consist of letters, digits and underscores(_).
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   411
  * Variable names cannot begin with digits, but can contain digits in them.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   412
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   413
In reference to the previous section examples, 'a', 'b', 'lng', 'fl', 'e' and 'cplx'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   414
are all variables of various datatypes.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   415
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   416
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   417
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   418
  Note: Python is not a strongly typed language and hence an integer variable can at a
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   419
  later stage be used as a float variable as well.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   420
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   421
Strings
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   422
~~~~~~~
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   423
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   424
Strings are one of the essential data structures of any programming language.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   425
The ``print "Hello, World!"`` program was introduced in the earlier section, and
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   426
the *"Hello, World!"* in the print statement is a string. A string is basically 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   427
a set of characters. Strings can be represented in various ways shown below:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   428
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   429
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   430
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   431
  s = 'this is a string'              # a string variable can be represented using single quotes
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   432
  s = 'This one has "quotes" inside!' # The string can have quotes inside it as shown
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   433
  s = "I have 'single-quotes' inside!"
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   434
  l = "A string spanning many lines\
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   435
  one more line\
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   436
  yet another"                        # a string can span more than a single line.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   437
  t = """A triple quoted string does  # another way of representing multiline strings.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   438
  not need to be escaped at the end and
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   439
  "can have nested quotes" etc."""
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   440
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   441
Try the following on the interpreter:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   442
``s = 'this is a string with 'quotes' of similar kind'``
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   443
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   444
**Exercise: How to use single quotes within single quotes in a string as shown 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   445
in the above example without getting an error?**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   446
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   447
String operations
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   448
-----------------
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   449
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   450
A few basic string operations are presented here. 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   451
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   452
**String concatenation**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   453
String concatenation is done by simple addition of two strings.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   454
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   455
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   456
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   457
  >>> x = 'Hello'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   458
  >>> y = ' Python'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   459
  >>> print x+y
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   460
  Hello Python
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   461
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   462
*Try this yourself:*
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   463
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   464
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   465
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   466
  >>> somenum = 13
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   467
  >>> print x+somenum
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   468
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   469
The problem with the above example is that here a string variable and an integer
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   470
variable are trying to be concantenated. To obtain the desired result from the 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   471
above example the str(), repr() and the `` can be used.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   472
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   473
**str()** simply converts a value to a string in a reasonable form.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   474
**repr()** creates a string that is a representation of the value.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   475
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   476
The difference can be seen in the example shown below:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   477
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   478
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   479
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   480
  >>> str(1000000000000000000000000000000000000000000000000L)
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   481
  '1000000000000000000000000000000000000000000000000'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   482
  >>> repr(1000000000000000000000000000000000000000000000000L)
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   483
  '1000000000000000000000000000000000000000000000000L'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   484
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   485
It can be observed that the 'L' in the long value shown was omitted by str(), 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   486
whereas repr() converted that into a string too. An alternative way of using 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   487
repr(value) is ```value```. 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   488
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   489
A few more examples:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   490
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   491
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   492
  >>> x = "Let's go \nto Pycon"
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   493
  >>> print x
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   494
  Let's go 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   495
  to Pycon
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   496
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   497
In the above example, notice that the '\n'(newline) character is formatted and 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   498
the string is printed on two lines. The strings discussed until now were normal 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   499
strings. Other than these there are two other types of strings namely, raw strings
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   500
and unicode strings.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   501
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   502
**Raw strings** are strings which are unformatted, that is the backslashes(\) are 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   503
not parsed and are left as it is in the string. Raw strings are represented with
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   504
an 'r' at the start of a string. 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   505
Let us look at an example
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   506
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   507
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   508
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   509
  >>> x = r"Let's go \nto Pycon"
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   510
  >>> print x
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   511
  Let's go \nto Pycon
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   512
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   513
Note: The '\n' is not being parsed into a new line and is left as it is.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   514
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   515
*Try this yourself:*
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   516
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   517
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   518
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   519
  >>> x = r"Let's go to Pycon\"
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   520
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   521
**Unicode strings** are strings where the characters are Unicode characters as 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   522
opposed to ASCII characters. Unicode strings are represented with a 'u' at the 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   523
start of the string.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   524
Let us look at an example:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   525
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   526
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   527
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   528
  >>> x = u"Let's go to Pycon!"
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   529
  >>> print x
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   530
  Let's go to Pycon!
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   531
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   532
Boolean
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   533
~~~~~~~
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   534
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   535
Python also provides special Boolean datatype. A boolean variable can assume a 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   536
value of either *True* or *False* (Note the capitalizations). 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   537
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   538
Let us look at examples:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   539
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   540
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   541
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   542
  >>> t = True
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   543
  >>> f = not t
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   544
  >>> print f
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   545
  False
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   546
  >>> f or t
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   547
  True
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   548
  >>> f and t
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   549
  False
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   550
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   551
The **while** loop
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   552
==================
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   553
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   554
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   555
The Python **while** loop is similar to the C/C++ while loop. The syntax is as
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   556
follows:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   557
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   558
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   559
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   560
  statement 0
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   561
  while condition:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   562
    statement 1 #while block
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   563
    statement 2 #while block
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   564
  statement 3 #outside the while block.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   565
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   566
Let us look at an example:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   567
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   568
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   569
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   570
    >>> x = 1  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   571
    >>> while x <= 5:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   572
    ...   print x
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   573
    ...   x += 1
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   574
    ... 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   575
    1
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   576
    2
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   577
    3
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   578
    4
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   579
    5
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   580
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   581
The **if** conditional
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   582
======================
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   583
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   584
The Python **if** block provides the conditional execution of statements. 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   585
If the condition evaluates as true the block of statements defined under the if 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   586
block are executed.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   587
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   588
If the first block is not executed on account of the condition not being satisfied,
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   589
the set of statements in the **else** block are executed.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   590
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   591
The **elif** block provides the functionality of evaluation of multiple conditions
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   592
as shown in the example.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   593
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   594
The syntax is as follows:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   595
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   596
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   597
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   598
  if condition :
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   599
      statement_1
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   600
      statement_2
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   601
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   602
  elif condition:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   603
      statement_3
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   604
      statement_4
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   605
  else:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   606
      statement_5
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   607
      statement_6
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   608
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   609
Let us look at an example:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   610
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   611
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   612
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   613
   >>> n = raw_input("Input a number:")
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   614
   >>> if n < 0:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   615
         print n," is negative"
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   616
         elif n > 0:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   617
         print n," is positive"
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   618
         else:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   619
         print n, " is 0"
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   620
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   621
**raw_input()**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   622
===============
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   623
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   624
In the previous example we saw the call to the raw_input() subroutine. 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   625
The **raw_input()** method is used to take user inputs through the console.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   626
Unlike **input()** which assumes the data entered by the user as a standard python
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   627
expression, **raw_input()** treats all the input data as raw data and converts
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   628
everything into a string. To illustrate this let us look at an example.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   629
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   630
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   631
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   632
  >>> input("Enter a number thats a palindrome:")
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   633
  Enter a number thats a palindrome:121
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   634
  121
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   635
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   636
  >>> input("Enter your name:")
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   637
  Enter your name:PythonFreak
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   638
  Traceback (most recent call last):
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   639
    File "<stdin>", line 1, in <module>
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   640
    File "<string>", line 1, in <module>
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   641
  NameError: name 'PythonFreak' is not defined
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   642
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   643
As shown above the **input()** assumes that the data entered is a valid Python
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   644
expression. In the first call it prompts for an integer input and when entered
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   645
it accepts the integer as an integer, whereas in the second call, when the string
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   646
is entered without the quotes, **input()** assumes that the entered data is a valid
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   647
Python expression and hence it raises and exception saying PythonFreak is not 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   648
defined.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   649
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   650
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   651
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   652
  >>> input("Enter your name:")
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   653
  Enter your name:'PythonFreak'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   654
  'PythonFreak'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   655
  >>> 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   656
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   657
Here the name is accepted because its entered as a string (within quotes). But
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   658
its unreasonable to go on using quotes each time a string is entered. Hence the
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   659
alternative is to use **raw_input()**.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   660
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   661
Let us now look at how **raw_input()** operates with an example.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   662
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   663
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   664
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   665
  >>> raw_input("Enter your name:")
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   666
  Enter your name:PythonFreak
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   667
  'PythonFreak'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   668
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   669
Observe that the **raw_input()** is converting it into a string all by itself.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   670
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   671
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   672
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   673
  >>> pal = raw_input("Enter a number thats a palindrome:")
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   674
  Enter a number thats a palindrome:121
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   675
  '121'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   676
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   677
Observe that **raw_input()** is converting the integer 121 also to a string as 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   678
'121'. Let us look at another example:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   679
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   680
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   681
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   682
  >>> pal = raw_input("Enter a number thats a palindrome:")
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   683
  Enter a number thats a palindrome:121
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   684
  >>> pal + 2
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   685
  Traceback (most recent call last):
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   686
    File "<stdin>", line 1, in <module>
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   687
  TypeError: cannot concatenate 'str' and 'int' objects
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   688
  >>> pal
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   689
  '121'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   690
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   691
Observe here that the variable *pal* is a string and hence integer operations
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   692
cannot be performed on it. Hence the exception is raised.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   693
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   694
**int()** method
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   695
================
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   696
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   697
Generally for computing purposes, the data used is not strings or raw data but 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   698
on integers, floats and similar mathematical data structures. The data obtained
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   699
from **raw_input()** is raw data in the form of strings. In order to obtain integers
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   700
from strings we use the method **int()**. 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   701
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   702
Let us look at an example.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   703
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   704
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   705
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   706
  >>> intpal = int(pal)
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   707
  >>> intpal
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   708
  121
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   709
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   710
In the previous example it was observed that *pal* was a string variable. Here
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   711
using the **int()** method the string *pal* was converted to an integer variable.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   712
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   713
*Try This Yourself:*
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   714
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   715
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   716
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   717
  >>> stringvar = raw_input("Enter a name:")
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   718
  Enter a name:Guido Van Rossum
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   719
  >>> stringvar
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   720
  'Guido Van Rossum'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   721
  >>> numvar = int(stringvar)
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   722