basic_python/intro.rst
author Santosh G. Vattam <vattam.santosh@gmail.com>
Tue, 15 Sep 2009 17:02:07 +0530
changeset 52 9748190df418
parent 50 0985027f4c8c
child 65 0f25f22a2725
permissions -rw-r--r--
Completed intro.rst. Review pending.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
     1
============
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
     2
Basic Python
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
     3
============
1
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     4
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     5
This document is intended to be handed out at the end of the workshop. It has
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     6
been designed for Engineering students who are Python beginners and have basic
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     7
programming skills. The focus is on basic numerics and plotting using Python.
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     8
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     9
The system requirements:
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    10
  * Python - version 2.5.x or newer.
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
    11
  * IPython
1
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    12
  * Text editor - scite, vim, emacs or whatever you are comfortable with.
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    13
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
    14
Introduction
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
    15
============
1
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    16
31
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    17
The Python programming language was created by a dutch named Guido van Rossum.
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    18
The idea of Python was conceived in December 1989. The name Python has nothing
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    19
to do with the reptilian, but its been named after the 70s comedy series 
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    20
"Monty Python's Flying Circus", since it happens to be Guido's favourite 
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    21
TV series. 
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    22
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    23
Current stable version of Python is 2.6.x, although Python 3.0 is also the stable
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    24
version, it is not backwards compatible with the previous versions and is hence
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    25
not entirely popular at the moment. This material will focus on the 2.6.x series.
1
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    26
  
31
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    27
Python is licensed under the Python Software Foundation License (PSF License) 
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    28
which is GPL compatible Free Software license (excepting license version 1.6 and 2.0)
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    29
It is a no strings attached license, which means the source code is free to modify
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    30
and redistribute.
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    31
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    32
The Python docs define Python as "Python is an interpreted, object-oriented, 
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    33
high-level programming language with dynamic semantics." A more detailed summary
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    34
can be found at http://www.python.org/doc/essays/blurb.html. Python is a language that
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    35
has been designed to help the programmer concentrate on solving the problem at hand
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    36
and not worry about the programming language idiosyncrasies.
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    37
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    38
Python is a highly cross platform compatible language on account of it being an 
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    39
interpreted language. It is highly scalable and hence has been adapted to run on 
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    40
the Nokia 60 series phones. Python has been designed to be readable and easy to use
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    41
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    42
**Resources available for reference**
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    43
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    44
* Web: http://www.python.org
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    45
* Doc: http://www.python.org/doc
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    46
* Free Tutorials:
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    47
    * Official Python Tutorial: http://docs.python.org/tut/tut.html
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    48
    * Byte of Python: http://www.byteofpython.info/
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    49
    * Dive into Python: http://diveintopython.org/
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    50
32
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    51
**Advantages of Python - Why Python??**
31
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    52
32
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    53
* Python has been designed for readability and ease of use. Its been designed in 
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    54
  such a fashion that it imposes readability on the programmer. Python does away
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    55
  with the braces and the semicolons and instead implements code blocks based on 
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    56
  indentation, thus enhancing readability. 
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    57
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    58
* Python is a high level, interpreted, modular and object oriented language.
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    59
  Python performs memory management on its own, thus the programmer need not bother
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    60
  about allocating and deallocating memory to variables. Python provides extensibility
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    61
  by providing modules which can be easily imported similar to headers in C and 
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    62
  packages in Java. Python is object oriented and hence provides all the object oriented
34
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
    63
  characteristics such as inheritance, encapsulation and polymorphism.
32
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    64
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    65
* Python offers a highly powerful interactive programming interface in the form
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    66
  of the 'Interactive Interpreter' which will be discussed in more detail in the 
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    67
  following sections.
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    68
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    69
* Python provides a rich standard library and an extensive set of modules. The 
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    70
  power of Python modules can be seen in this slightly exaggerated cartoon
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    71
  http://xkcd.com/353/
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    72
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    73
* Python interfaces well with most other programming languages such as C, C++ 
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    74
  and FORTRAN.
31
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    75
33
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    76
Although, Python has one setback. Python is not fast as some of the compiled 
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    77
languages like C or C++. Yet, the amount of flexibility and power more than make
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    78
up for this setback.
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    79
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    80
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
    81
The Python Interpreter
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
    82
======================
31
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    83
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
    84
The Interactive Interpreter
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
    85
~~~~~~~~~~~~~~~~~~~~~~~~~~~
34
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
    86
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
    87
Typing *python* at the shell prompt on any standard Unix/Gnu-Linux system and
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
    88
hitting the enter key fires up the Python 'Interactive Interpreter'. The Python
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
    89
interpreter is one of the most integral features of Python. The prompt obtained
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
    90
when the interactive interpreter is similar to what is shown below. The exact
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
    91
appearance might differ based on the version of Python being used. The ``>>>``
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
    92
thing shown is the python prompt. When something is typed at the prompt and the
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
    93
enter key is hit, the python interpreter interprets the command entered and
49
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
    94
performs the appropriate action. All the examples presented in this document are
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
    95
to be tried hands on, on the interactive interpreter.
33
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    96
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    97
::
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    98
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    99
  Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
   100
  [GCC 4.3.2] on linux2
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
   101
  Type "help", "copyright", "credits" or "license" for more information.
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
   102
  >>> 
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
   103
34
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   104
Lets try with an example, type ``print 'Hello, World!'`` at the prompt and hit
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   105
the enter key. 
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   106
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   107
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   108
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   109
  >>> print 'Hello, World!'
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   110
  Hello, World!
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   111
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   112
This example was quite straight forward, and thus we have written our first
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   113
line of Python code. Now let us try typing something arbitrary at the prompt.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   114
For example: 
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   115
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   116
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   117
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   118
  >>> arbit word
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   119
    File "<stdin>", line 1
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   120
      arbit word
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   121
              ^
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   122
  SyntaxError: invalid syntax
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   123
  >>>
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   124
    
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   125
The interpreter gave an error message saying that 'arbit word' was invalid
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   126
syntax which is valid. The interpreter is an amazing tool when learning to
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   127
program in Python. The interpreter provides a help function that provides the
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   128
necessary documentation regarding all Python syntax, constructs, modules and
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   129
objects. Typing *help()* at the prompt gives the following output:
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   130
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   131
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   132
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   133
  >>> help()
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   134
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   135
  Welcome to Python 2.5!  This is the online help utility.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   136
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   137
  If this is your first time using Python, you should definitely check out
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   138
  the tutorial on the Internet at http://www.python.org/doc/tut/.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   139
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   140
  Enter the name of any module, keyword, or topic to get help on writing
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   141
  Python programs and using Python modules.  To quit this help utility and
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   142
  return to the interpreter, just type "quit".
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   143
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   144
  To get a list of available modules, keywords, or topics, type "modules",
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   145
  "keywords", or "topics".  Each module also comes with a one-line summary
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   146
  of what it does; to list the modules whose summaries contain a given word
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   147
  such as "spam", type "modules spam".
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   148
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   149
  help> 
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   150
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   151
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   152
As mentioned in the output, entering the name of any module, keyword or topic
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   153
will provide the documentation and help regarding the same through the online
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   154
help utility. Pressing *Ctrl+d* exits the help prompt and returns to the
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   155
python prompt. 
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   156
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   157
Let us now try a few examples at the python interpreter. 
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   158
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   159
Eg 1:
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   160
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   161
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   162
  >>> print 'Hello, python!'
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   163
  Hello, python!
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   164
  >>>
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   165
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   166
Eg 2:
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   167
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   168
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   169
  >>> print 4321*567890
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   170
  2453852690
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   171
  >>> 
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   172
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   173
Eg 3:
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   174
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   175
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   176
  >>> 4321*567890
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   177
  2453852690L
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   178
  >>>
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   179
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   180
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   181
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   182
  Note: Notice the 'L' at the end of the output. The 'L' signifies that the
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   183
  output of the operation is of type *long*. It was absent in the previous
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   184
  example because we used the print statement. This is because *print* formats
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   185
  the output before displaying.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   186
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   187
Eg 4:
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   188
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   189
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   190
  >>> big = 12345678901234567890 ** 3
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   191
  >>> print big
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   192
  1881676372353657772490265749424677022198701224860897069000
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   193
  >>> 
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   194
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   195
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   196
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   197
  This example is to show that unlike in C or C++ there is no limit on the
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   198
  value of an integer.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   199
49
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   200
Try this on the interactive interpreter:
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   201
``import this``
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   202
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   203
*Hint: The output gives an idea of Power of Python*
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   204
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   205
*ipython* - An enhanced interactive Python interpreter
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   206
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   207
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   208
The power and the importance of the interactive interpreter was the highlight
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   209
of the previous section. This section provides insight into the enhanced
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   210
interpreter with more advanced set of features called **ipython**. Entering
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   211
*ipython* at the shell prompt fires up the interactive interpreter. 
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   212
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   213
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   214
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   215
  $ ipython
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   216
  Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   217
  Type "copyright", "credits" or "license" for more information.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   218
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   219
  IPython 0.8.4 -- An enhanced Interactive Python.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   220
  ?         -> Introduction and overview of IPython's features.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   221
  %quickref -> Quick reference.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   222
  help      -> Python's own help system.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   223
  object?   -> Details about 'object'. ?object also works, ?? prints more.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   224
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   225
  In [1]: 
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   226
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   227
This is the output obtained upon firing ipython. The exact appearance may
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   228
change based on the Python version installed. The following are some of the
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   229
various features provided by **ipython**:
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   230
  
40
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   231
    Suggestions - ipython provides suggestions of the possible methods and
34
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   232
    operations available for the given python object.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   233
40
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   234
Eg 5:
34
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   235
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   236
::
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   237
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   238
  In [4]: a = 6
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   239
  
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   240
  In [5]: a.
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   241
  a.__abs__           a.__divmod__        a.__index__         a.__neg__          a.__rand__          a.__rmod__          a.__rxor__
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   242
  a.__add__           a.__doc__           a.__init__          a.__new__          a.__rdiv__          a.__rmul__          a.__setattr__
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   243
  a.__and__           a.__float__         a.__int__           a.__nonzero__      a.__rdivmod__       a.__ror__           a.__str__
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   244
  a.__class__         a.__floordiv__      a.__invert__        a.__oct__          a.__reduce__        a.__rpow__          a.__sub__
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   245
  a.__cmp__           a.__getattribute__  a.__long__          a.__or__           a.__reduce_ex__     a.__rrshift__       a.__truediv__
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   246
  a.__coerce__        a.__getnewargs__    a.__lshift__        a.__pos__          a.__repr__          a.__rshift__        a.__xor__
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   247
  a.__delattr__       a.__hash__          a.__mod__           a.__pow__          a.__rfloordiv__     a.__rsub__          
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   248
  a.__div__           a.__hex__           a.__mul__           a.__radd__         a.__rlshift__       a.__rtruediv__      
34
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   249
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   250
In this example, we initialized 'a' (a variable - a concept that will be
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   251
discussed in the subsequent sections.) to 6. In the next line when the *tab* key
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   252
is pressed after typing '*a.*' ipython displays the set of all possible methods
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   253
that are applicable on the object 'a' (an integer in this context). Ipython
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   254
provides many such datatype specific features which will be presented in the
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   255
further sections as and when the datatypes are introduced.
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   256
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   257
Editing and running a python file
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   258
=================================
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   259
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   260
The previous sections focused on the use of the interpreter to run python code.
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   261
While the interpeter is an excellent tool to test simple solutions and
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   262
experiment with small code snippets, its main disadvantage is that everything
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   263
written in the interpreter is lost once its quit. Most of the times a program is 
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   264
used by people other than the author. So the programs have to be available in 
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   265
some form suitable for distribution, and hence they are written in files. This 
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   266
section will focus on editing and running python files. Start by opening a text 
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   267
editor ( it is recommended you choose one from the list at the top of this page ).
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   268
In the editor type down python code and save the file with an extension **.py** 
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   269
(python files have an extension of .py). Once done with the editing, save the 
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   270
file and exit the editor. 
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   271
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   272
Let us look at a simple example of calculating the gcd of 2 numbers using Python:
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   273
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   274
**Creating the first python script(file)**
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   275
::
34
7a243a6d8625 Added interpreter examples and the ipython section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 33
diff changeset
   276
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   277
  $ emacs gcd.py
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   278
    def gcd(x,y):
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   279
      if x % y == 0:
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   280
        return y
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   281
      return gcd(y, x%y)
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   282
  
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   283
    print gcd(72, 92)
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   284
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   285
To run the script, open the shell prompt, navigate to the directory that 
49
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   286
contains the python file and run ``python <filename.py>`` at the prompt ( in this 
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   287
case filename is gcd.py )
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   288
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   289
**Running the python script**
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   290
::
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   291
  
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   292
  $ python gcd.py
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   293
  4
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   294
  $ 
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   295
40
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   296
Another method to run a python script would be to include the line
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   297
49
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   298
``#! /usr/bin/python``
40
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   299
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   300
at the beginning of the python file and then make the file executable by 
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   301
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   302
$ chmod a+x *filename.py*
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   303
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   304
Once this is done, the script can be run as a standalone program as follows:
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   305
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   306
$ ./*filename.py*
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   307
39
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   308
Basic Datatypes and operators in Python
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   309
=======================================
932a7a863120 Added Editing and running a python file section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 34
diff changeset
   310
40
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   311
Python provides the following set of basic datatypes.
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   312
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   313
  * Numbers: int, float, long, complex
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   314
  * Strings
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   315
  * Boolean
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   316
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   317
Numbers
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   318
~~~~~~~
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   319
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   320
Numbers were introduced in the examples presented in the interactive interpreter
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   321
section. Numbers include types as mentioned earlier viz., int (integers), float 
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   322
(floating point numbers), long (large integers), complex (complex numbers with 
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   323
real and imaginary parts). Python is not a strongly typed language, which means 
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   324
the type of a variable need not mentioned during its initialization. Let us look
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   325
at a few examples.
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   326
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   327
Eg 6:
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   328
::
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   329
  
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   330
  >>> a = 1 #here a is an integer variable
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   331
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   332
Eg 7:
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   333
::
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   334
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   335
  >>> lng = 122333444455555666666777777788888888999999999 #here lng is a variable of type long
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   336
  >>> lng
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   337
  122333444455555666666777777788888888999999999L #notice the trailing 'L'
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   338
  >>> print lng
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   339
  122333444455555666666777777788888888999999999 #notice the absence of the trailing 'L'
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   340
  >>> lng+1
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   341
  122333444455555666666777777788888889000000000L
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   342
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   343
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   344
Long numbers are the same as integers in almost all aspects. They can be used in
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   345
operations just like integers and along with integers without any distinction.
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   346
The only distinction comes during type checking (which is not a healthy practice).
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   347
Long numbers are tucked with a trailing 'L' just to signify that they are long.
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   348
Notice that in the example just lng at the prompt displays the value of the variable
49
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   349
with the 'L' whereas ``print lng`` displays without the 'L'. This is because print 
40
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   350
formats the output before printing. Also in the example, notice that adding an 
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   351
integer to a long does not give any errors and the result is as expected. So for
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   352
all practical purposes longs can be treated as ints.
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   353
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   354
Eg 8:
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   355
::
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   356
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   357
  >>> fl = 3.14159 #fl is a float variable
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   358
  >>> e = 1.234e-4 #e is also a float variable, specified in the exponential form
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   359
  >>> a = 1
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   360
  >>> b = 2
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   361
  >>> a/b #integer division
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   362
  0
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   363
  >>> a/fl #floating point division
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   364
  0.31831015504887655
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   365
  >>> e/fl
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   366
  3.9279473133031364e-05
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   367
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   368
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   369
Floating point numbers, simply called floats are real numbers with a decimal point.
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   370
The example above shows the initialization of a float variable. Shown also in this
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   371
example is the difference between integer division and floating point division.
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   372
'a' and 'b' here are integer variables and hence the division gives 0 as the quotient.
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   373
When either of the operands is a float, the operation is a floating point division,
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   374
and the result is also a float as illustrated.
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   375
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   376
Eg 9:
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   377
::
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   378
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   379
  >>> cplx = 3 + 4j #cplx is a complex variable
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   380
  >>> cplx
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   381
  (3+4j)
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   382
  >>> print cplx.real #prints the real part of the complex number
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   383
  3.0
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   384
  >>> print cplx.imag #prints the imaginary part of the complex number
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   385
  4.0
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   386
  >>> print cplx*fl  #multiplies the real and imag parts of the complex number with the multiplier
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   387
  (9.42477+12.56636j)
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   388
  >>> abs(cplx) #returns the absolute value of the complex number
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   389
  5.0
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   390
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   391
Python provides a datatype for complex numbers. Complex numbers are initialized 
ccdbbc094579 Added content of basic datatypes.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 39
diff changeset
   392
as shown in the example above. The *real* and *imag* operators return the real and
41
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   393
imaginary parts of the complex number as shown. The *abs()* returns the absolute
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   394
value of the complex number.
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   395
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   396
Variables
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   397
~~~~~~~~~
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   398
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   399
Variables are just names that represent a value. Variables have already been 
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   400
introduced in the various examples from the previous sections. Certain rules about
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   401
using variables:
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   402
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   403
  * Variables have to be initialized or assigned a value before being used.
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   404
  * Variable names can consist of letters, digits and underscores(_).
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   405
  * Variable names cannot begin with digits, but can contain digits in them.
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   406
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   407
In reference to the previous section examples, 'a', 'b', 'lng', 'fl', 'e' and 'cplx'
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   408
are all variables of various datatypes.
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   409
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   410
::
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   411
  
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   412
  Note: Python is not a strongly typed language and hence an integer variable can at a
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   413
  later stage be used as a float variable as well.
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   414
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   415
Strings
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   416
~~~~~~~
b81255d096ab Added Variables section.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 40
diff changeset
   417
49
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   418
Strings are one of the essential data structures of any programming language.
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   419
The ``print "Hello, World!"`` program was introduced in the earlier section, and
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   420
the *"Hello, World!"* in the print statement is a string. A string is basically 
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   421
a set of characters. Strings can be represented in various ways shown below:
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   422
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   423
::
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   424
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   425
  s = 'this is a string'              # a string variable can be represented using single quotes
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   426
  s = 'This one has "quotes" inside!' # The string can have quotes inside it as shown
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   427
  s = "I have 'single-quotes' inside!"
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   428
  l = "A string spanning many lines\
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   429
  one more line\
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   430
  yet another"                        # a string can span more than a single line.
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   431
  t = """A triple quoted string does  # another way of representing multiline strings.
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   432
  not need to be escaped at the end and
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   433
  "can have nested quotes" etc."""
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   434
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   435
Try the following on the interpreter:
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   436
``s = 'this is a string with 'quotes' of similar kind'``
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   437
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   438
**Exercise: How to use single quotes within single quotes in a string as shown 
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   439
in the above example without getting an error?**
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   440
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   441
String operations
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   442
-----------------
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   443
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   444
A few basic string operations are presented here. 
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   445
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   446
**String concatenation**
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   447
String concatenation is done by simple addition of two strings.
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   448
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   449
::
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   450
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   451
  >>> x = 'Hello'
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   452
  >>> y = ' Python'
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   453
  >>> print x+y
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   454
  Hello Python
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   455
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   456
*Try this yourself:*
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   457
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   458
::
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   459
  
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   460
  >>> somenum = 13
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   461
  >>> print x+somenum
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   462
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   463
The problem with the above example is that here a string variable and an integer
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   464
variable are trying to be concantenated. To obtain the desired result from the 
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   465
above example the str(), repr() and the `` can be used.
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   466
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   467
**str()** simply converts a value to a string in a reasonable form.
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   468
**repr()** creates a string that is a representation of the value.
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   469
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   470
The difference can be seen in the example shown below:
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   471
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   472
::
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   473
  
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   474
  >>> str(1000000000000000000000000000000000000000000000000L)
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   475
  '1000000000000000000000000000000000000000000000000'
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   476
  >>> repr(1000000000000000000000000000000000000000000000000L)
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   477
  '1000000000000000000000000000000000000000000000000L'
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   478
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   479
It can be observed that the 'L' in the long value shown was omitted by str(), 
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   480
whereas repr() converted that into a string too. An alternative way of using 
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   481
repr(value) is ```value```. 
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   482
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   483
A few more examples:
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   484
::
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   485
  
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   486
  >>> x = "Let's go \nto Pycon"
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   487
  >>> print x
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   488
  Let's go 
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   489
  to Pycon
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   490
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   491
In the above example, notice that the '\n'(newline) character is formatted and 
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   492
the string is printed on two lines. The strings discussed until now were normal 
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   493
strings. Other than these there are two other types of strings namely, raw strings
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   494
and unicode strings.
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   495
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   496
**Raw strings** are strings which are unformatted, that is the backslashes(\) are 
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   497
not parsed and are left as it is in the string. Raw strings are represented with
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   498
an 'r' at the start of a string. 
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   499
Let us look at an example
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   500
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   501
::
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   502
  
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   503
  >>> x = r"Let's go \nto Pycon"
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   504
  >>> print x
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   505
  Let's go \nto Pycon
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   506
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   507
Note: The '\n' is not being parsed into a new line and is left as it is.
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   508
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   509
*Try this yourself:*
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   510
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   511
::
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   512
  
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   513
  >>> x = r"Let's go to Pycon\"
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   514
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   515
**Unicode strings** are strings where the characters are Unicode characters as 
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   516
opposed to ASCII characters. Unicode strings are represented with a 'u' at the 
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   517
start of the string.
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   518
Let us look at an example:
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   519
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   520
::
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   521
  
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   522
  >>> x = u"Let's go to Pycon!"
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   523
  >>> print x
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   524
  Let's go to Pycon!
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   525
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   526
Boolean
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   527
~~~~~~~
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   528
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   529
Python also provides special Boolean datatype. A boolean variable can assume a 
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   530
value of either *True* or *False* (Note the capitalizations). 
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   531
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   532
Let us look at examples:
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   533
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   534
::
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   535
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   536
  >>> t = True
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   537
  >>> f = not t
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   538
  >>> print f
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   539
  False
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   540
  >>> f or t
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   541
  True
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   542
  >>> f and t
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   543
  False
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   544
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   545
The **while** loop
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   546
~~~~~~~~~~~~~~~~~~
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   547
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   548
The Python **while** loop is similar to the C/C++ while loop. The syntax is as
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   549
follows:
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   550
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   551
::
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   552
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   553
  statement 0
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   554
  while condition:
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   555
    statement 1 #while block
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   556
    statement 2 #while block
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   557
  statement 3 #outside the while block.
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   558
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   559
Let us look at an example:
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   560
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   561
::
50
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   562
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   563
    >>> x = 1  
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   564
    >>> while x <= 5:
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   565
    ...   print x
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   566
    ...   x += 1
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   567
    ... 
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   568
    1
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   569
    2
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   570
    3
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   571
    4
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   572
    5
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   573
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   574
The **if** conditional
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   575
~~~~~~~~~~~~~~~~~~~~~~
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   576
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   577
The Python **if** block provides the conditional execution of statements. 
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   578
If the condition evaluates as true the block of statements defined under the if 
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   579
block are executed.
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   580
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   581
If the first block is not executed on account of the condition not being satisfied,
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   582
the set of statements in the **else** block are executed.
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   583
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   584
The **elif** block provides the functionality of evaluation of multiple conditions
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   585
as shown in the example.
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   586
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   587
The syntax is as follows:
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   588
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   589
::
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   590
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   591
  if condition :
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   592
      statement_1
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   593
      statement_2
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   594
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   595
  elif condition:
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   596
      statement_3
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   597
      statement_4
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   598
  else:
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   599
      statement_5
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   600
      statement_6
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   601
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   602
Let us look at an example:
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   603
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   604
::
49
5b5c728cd47f Added Strings and Boolean sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 41
diff changeset
   605
  
50
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   606
   >>> n = raw_input("Input a number:")
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   607
   >>> if n < 0:
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   608
         print n," is negative"
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   609
         elif n > 0:
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   610
         print n," is positive"
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   611
         else:
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   612
         print n, " is 0"
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   613
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   614
**raw_input()**
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   615
~~~~~~~~~~~~~~~
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   616
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   617
In the previous example we saw the call to the raw_input() subroutine. 
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   618
The **raw_input()** method is used to take user inputs through the console.
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   619
Unlike **input()** which assumes the data entered by the user as a standard python
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   620
expression, **raw_input()** treats all the input data as raw data and converts
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   621
everything into a string. To illustrate this let us look at an example.
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   622
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   623
::
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   624
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   625
  >>> input("Enter a number thats a palindrome:")
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   626
  Enter a number thats a palindrome:121
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   627
  121
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   628
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   629
  >>> input("Enter your name:")
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   630
  Enter your name:PythonFreak
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   631
  Traceback (most recent call last):
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   632
    File "<stdin>", line 1, in <module>
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   633
    File "<string>", line 1, in <module>
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   634
  NameError: name 'PythonFreak' is not defined
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   635
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   636
As shown above the **input()** assumes that the data entered is a valid Python
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   637
expression. In the first call it prompts for an integer input and when entered
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   638
it accepts the integer as an integer, whereas in the second call, when the string
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   639
is entered without the quotes, **input()** assumes that the entered data is a valid
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   640
Python expression and hence it raises and exception saying PythonFreak is not 
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   641
defined.
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   642
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   643
::
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   644
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   645
  >>> input("Enter your name:")
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   646
  Enter your name:'PythonFreak'
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   647
  'PythonFreak'
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   648
  >>> 
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   649
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   650
Here the name is accepted because its entered as a string (within quotes). But
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   651
its unreasonable to go on using quotes each time a string is entered. Hence the
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   652
alternative is to use **raw_input()**.
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   653
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   654
Let us now look at how **raw_input()** operates with an example.
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   655
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   656
::
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   657
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   658
  >>> raw_input("Enter your name:")
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   659
  Enter your name:PythonFreak
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   660
  'PythonFreak'
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   661
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   662
Observe that the **raw_input()** is converting it into a string all by itself.
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   663
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   664
::
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   665
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   666
  >>> pal = raw_input("Enter a number thats a palindrome:")
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   667
  Enter a number thats a palindrome:121
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   668
  '121'
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   669
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   670
Observe that **raw_input()** is converting the integer 121 also to a string as 
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   671
'121'. Let us look at another example:
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   672
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   673
::
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   674
  
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   675
  >>> pal = raw_input("Enter a number thats a palindrome:")
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   676
  Enter a number thats a palindrome:121
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   677
  >>> pal + 2
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   678
  Traceback (most recent call last):
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   679
    File "<stdin>", line 1, in <module>
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   680
  TypeError: cannot concatenate 'str' and 'int' objects
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   681
  >>> pal
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   682
  '121'
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   683
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   684
Observe here that the variable *pal* is a string and hence integer operations
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   685
cannot be performed on it. Hence the exception is raised.
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   686
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   687
**int()** method
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   688
~~~~~~~~~~~~~~~~
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   689
52
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   690
Generally for computing purposes, the data used is not strings or raw data but 
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   691
on integers, floats and similar mathematical data structures. The data obtained
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   692
from **raw_input()** is raw data in the form of strings. In order to obtain integers
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   693
from strings we use the method **int()**. 
50
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   694
0985027f4c8c Added while loop, if conditional, raw_input() sections.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 49
diff changeset
   695
Let us look at an example.
52
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   696
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   697
::
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   698
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   699
  >>> intpal = int(pal)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   700
  >>> intpal
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   701
  121
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   702
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   703
In the previous example it was observed that *pal* was a string variable. Here
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   704
using the **int()** method the string *pal* was converted to an integer variable.
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   705
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   706
*Try This Yourself:*
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   707
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   708
::
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   709
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   710
  >>> stringvar = raw_input("Enter a name:")
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   711
  Enter a name:Guido Van Rossum
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   712
  >>> stringvar
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   713
  'Guido Van Rossum'
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   714
  >>> numvar = int(stringvar)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   715
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   716
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   717
Functions in Python: **def**
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   718
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   719
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   720
*Functions* allow us to enclose a set of statements and call the function again
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   721
and again instead of repeating the group of statements everytime. Functions also
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   722
allow us to isolate a piece of code from all the other code and provides the
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   723
convenience of not polluting the global variables.
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   724
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   725
*Function* in python is defined with the keyword **def** followed by the name
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   726
of the function, in turn followed by a pair of parenthesis which encloses the
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   727
list of parameters to the function. The definition line ends with a ':'. The
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   728
definition line is followed by the body of the function intended by one block.
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   729
The *Function* must return a value::
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   730
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   731
  def factorial(n):
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   732
    fact = 1
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   733
    for i in range(2, n):
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   734
      fact *= i
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   735
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   736
    return fact
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   737
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   738
The code snippet above defines a function with the name factorial, takes the
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   739
number for which the factorial must be computed, computes the factorial and
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   740
returns the value.
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   741
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   742
A *Function* once defined can be used or called anywhere else in the program. We
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   743
call a fucntion with its name followed by a pair of parenthesis which encloses
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   744
the arguments to the function.
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   745
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   746
The value that function returns can be assigned to a variable. Let's call the
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   747
above function and store the factorial in a variable::
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   748
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   749
  fact5 = factorial(5)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   750
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   751
The value of fact5 will now be 120, which is the factorial of 5. Note that we
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   752
passed 5 as the argument to the function.
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   753
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   754
It may be necessary to document what the function does, for each of the function
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   755
to help the person who reads our code to understand it better. In order to do
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   756
this Python allows the first line of the function body to be a string. This
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   757
string is called as *Documentation String* or *docstring*. *docstrings* prove
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   758
to be very handy since there are number of tools which can pull out all the
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   759
docstrings from Python functions and generate the documentation automatically
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   760
from it. *docstrings* for functions can be written as follows::
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   761
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   762
  def factorial(n):
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   763
    'Returns the factorial for the number n.'
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   764
    fact = 1
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   765
    for i in range(2, n):
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   766
      fact *= i
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   767
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   768
    return fact
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   769
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   770
An important point to note at this point is that, a function can return any
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   771
Python value or a Python object, which also includes a *Tuple*. A *Tuple* is
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   772
just a collection of values and those values themselves can be of any other
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   773
valid Python datatypes, including *Lists*, *Tuples*, *Dictionaries* among other
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   774
things. So effectively, if a function can return a tuple, it can return any
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   775
number of values through a tuple
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   776
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   777
Let us write a small function to swap two values::
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   778
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   779
  def swap(a, b):
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   780
    return b, a
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   781
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   782
  c, d = swap(a, b)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   783
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   784
Function scope
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   785
---------------
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   786
The variables used inside the function are confined to the function's scope
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   787
and doesn't pollute the variables of the same name outside the scope of the
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   788
function. Also the arguments passed to the function are passed by-value if
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   789
it is of basic Python data type::
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   790
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   791
  def cant_change(n):
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   792
    n = 10
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   793
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   794
  n = 5
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   795
  cant_change(n)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   796
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   797
Upon running this code, what do you think would have happened to value of n
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   798
which was assigned 5 before the function call? If you have already tried out
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   799
that snippet on the interpreter you already know that the value of n is not
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   800
changed. This is true of any immutable types of Python like *Numbers*, *Strings*
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   801
and *Tuples*. But when you pass mutable objects like *Lists* and *Dictionaries*
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   802
the values are manipulated even outside the function::
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   803
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   804
  >>> def can_change(n):
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   805
  ...   n[1] = James
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   806
  ...
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   807
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   808
  >>> name = ['Mr.', 'Steve', 'Gosling']
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   809
  >>> can_change(name)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   810
  >>> name
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   811
  ['Mr.', 'James', 'Gosling']
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   812
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   813
If nothing is returned by the function explicitly, Python takes care to return
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   814
None when the funnction is called.
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   815
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   816
Default Arguments
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   817
-----------------
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   818
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   819
There may be situations where we need to allow the functions to take the
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   820
arguments optionally. Python allows us to define function this way by providing
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   821
a facility called *Default Arguments*. For example, we need to write a function
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   822
that returns a list of fibonacci numbers. Since our function cannot generate an
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   823
infinite list of fibonacci numbers, we need to specify the number of elements
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   824
that the fibonacci sequence must contain. Suppose, additionally, we want to the
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   825
function to return 10 numbers in the sequence if no option is specified we can
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   826
define the function as follows::
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   827
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   828
  def fib(n=10):
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   829
    fib_list = [0, 1]
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   830
    for i in range(n - 2):
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   831
      next = fib_list[-2] + fib_list[-1]
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   832
      fib_list.append(next)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   833
    return fib_list
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   834
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   835
When we call this function, we can optionally specify the value for the
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   836
parameter n, during the call as an argument. Calling with no argument and
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   837
argument with n=5 returns the following fibonacci sequences::
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   838
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   839
  fib()
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   840
  [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   841
  fib(5)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   842
  [0, 1, 1, 2, 3]
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   843
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   844
Keyword Arguments
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   845
-----------------
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   846
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   847
When a function takes a large number of arguments, it may be difficult to
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   848
remember the order of the parameters in the function definition or it may
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   849
be necessary to pass values to only certain parameters since others take
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   850
the default value. In either of these cases, Python provides the facility
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   851
of passing arguments by specifying the name of the parameter as defined in
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   852
the function definition. This is known as *Keyword Arguments*. 
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   853
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   854
In a function call, *Keyword arguments* can be used for each argument, in the
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   855
following fashion::
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   856
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   857
  argument_name=argument_value
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   858
  Also denoted as: keyword=argument
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   859
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   860
  def wish(name='World', greetings='Hello'):
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   861
    print "%s, %s!" % (greetings, name)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   862
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   863
This function can be called in one of the following ways. It is important to
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   864
note that no restriction is imposed in the order in which *Keyword arguments*
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   865
can be specified. Also note, that we have combined *Keyword arguments* with
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   866
*Default arguments* in this example, however it is not necessary::
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   867
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   868
  wish(name='Guido', greetings='Hey')
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   869
  wish(greetings='Hey', name='Guido')
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   870
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   871
Calling functions by specifying arguments in the order of parameters specified
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   872
in the function definition is called as *Positional arguments*, as opposed to
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   873
*Keyword arguments*. It is possible to use both *Positional arguments* and 
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   874
*Keyword arguments* in a single function call. But Python doesn't allow us to
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   875
bungle up both of them. The arguments to the function, in the call, must always
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   876
start with *Positional arguments* which is in turn followed by *Keyword
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   877
arguments*::
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   878
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   879
  def my_func(x, y, z, u, v, w):
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   880
    # initialize variables.
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   881
    ...
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   882
    # do some stuff 
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   883
    ...
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   884
    # return the value
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   885
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   886
It is valid to call the above functions in the following ways::
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   887
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   888
  my_func(10, 20, 30, u=1.0, v=2.0, w=3.0)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   889
  my_func(10, 20, 30, 1.0, 2.0, w=3.0)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   890
  my_func(10, 20, z=30, u=1.0, v=2.0, w=3.0)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   891
  my_func(x=10, y=20, z=30, u=1.0, v=2.0, w=3.0)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   892
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   893
Following lists some of the invalid calls::
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   894
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   895
  my_func(10, 20, z=30, 1.0, 2.0, 3.0)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   896
  my_func(x=10, 20, z=30, 1.0, 2.0, 3.0)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   897
  my_func(x=10, y=20, z=30, u=1.0, v=2.0, 3.0)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   898
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   899
Parameter Packing and Unpacking
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   900
-------------------------------
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   901
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   902
The positional arguments passed to a function can be collected in a tuple
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   903
parameter and keyword arguments can be collected in a dictionary. Since keyword
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   904
arguments must always be the last set of arguments passed to a function, the
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   905
keyword dictionary parameter must be the last parameter. The function definition
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   906
must include a list explicit parameters, followed by tuple paramter collecting
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   907
parameter, whose name is preceded by a *****, for collecting positional
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   908
parameters, in turn followed by the dictionary collecting parameter, whose name
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   909
is preceded by a ****** ::
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   910
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   911
  def print_report(title, *args, **name):
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   912
    """Structure of *args*
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   913
    (age, email-id)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   914
    Structure of *name*
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   915
    {
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   916
        'first': First Name
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   917
        'middle': Middle Name
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   918
        'last': Last Name
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   919
    }
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   920
    """
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   921
    
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   922
    print "Title: %s" % (title)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   923
    print "Full name: %(first)s %(middle)s %(last)s" % name
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   924
    print "Age: %d\nEmail-ID: %s" % args
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   925
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   926
The above function can be called as. Note, the order of keyword parameters can
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   927
be interchanged::
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   928
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   929
  >>> print_report('Employee Report', 29, 'johny@example.com', first='Johny',
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   930
                   last='Charles', middle='Douglas')
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   931
  Title: Employee Report
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   932
  Full name: Johny Douglas Charles
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   933
  Age: 29
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   934
  Email-ID: johny@example.com
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   935
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   936
The reverse of this can also be achieved by using a very identical syntax while
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   937
calling the function. A tuple or a dictionary can be passed as arguments in
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   938
place of a list of *Positional arguments* or *Keyword arguments* respectively
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   939
using ***** or ****** ::
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   940
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   941
  def print_report(title, age, email, first, middle, last):
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   942
    print "Title: %s" % (title)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   943
    print "Full name: %s %s %s" % (first, middle, last)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   944
    print "Age: %d\nEmail-ID: %s" % (age, email)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   945
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   946
  >>> args = (29, 'johny@example.com')
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   947
  >>> name = {
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   948
          'first': 'Johny',
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   949
          'middle': 'Charles',
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   950
          'last': 'Douglas'
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   951
          }
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   952
  >>> print_report('Employee Report', *args, **name)
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   953
  Title: Employee Report
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   954
  Full name: Johny Charles Douglas
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   955
  Age: 29
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   956
  Email-ID: johny@example.com
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   957
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   958
Nested Functions and Scopes
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   959
---------------------------
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   960
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   961
Python allows nesting one function inside another. This style of programming
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   962
turns out to be extremely flexible and powerful features when we use *Python
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   963
decorators*. We will not talk about decorators is beyond the scope of this
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   964
course. If you are interested in knowing more about *decorator programming* in
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   965
Python you are suggested to read:
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   966
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   967
| http://avinashv.net/2008/04/python-decorators-syntactic-sugar/
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   968
| http://personalpages.tds.net/~kent37/kk/00001.html
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   969
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   970
However, the following is an example for nested functions in Python::
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   971
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   972
  def outer():
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   973
    print "Outer..."
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   974
    def inner():
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   975
      print "Inner..."
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   976
    print "Outer..."
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   977
    inner()
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   978
  
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   979
  >>> outer()
9748190df418 Completed intro.rst. Review pending.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 50
diff changeset
   980