basic_python/intro.rst
author Santosh G. Vattam <vattam.santosh@gmail.com>
Sat, 22 Aug 2009 03:01:23 +0530
changeset 33 b850a2b9fc21
parent 32 628d035d559a
child 34 7a243a6d8625
permissions -rw-r--r--
Added interpreter content.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     1
=====================
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     2
Basic Python Workshop
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     3
=====================
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.
dccb80025455 Added Basic Python Handout rst document.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    11
  * IPython 
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
31
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    14
1. Introduction
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
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
628d035d559a Added Advantages of Python section
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 31
diff changeset
    63
  characterstics such as inheritence, encapsulation and polymorphism.
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
31
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    81
1.1 The Python Interpreter
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    82
--------------------------
01ea4544ad81 Added content to introduction.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 3
diff changeset
    83
33
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    84
Typing python at the shell prompt on any standard Unix/Gnu-Linux system fires up
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    85
the Python 'Interactive Interpreter'. The Python interpreter is one of the most 
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    86
integral features of Python. The prompt obtained when the interactive interpreter
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    87
is similar to what is shown below. The exact appearance might differ based on the
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    88
version of Python being used. The ``>>>`` thing shown is the python prompt. 
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    89
When something is typed at the prompt and the enter key is hit, the python interpreter
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    90
interprets the command entered and performs the appropriate action.
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    91
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    92
::
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    93
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    94
  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
    95
  [GCC 4.3.2] on linux2
b850a2b9fc21 Added interpreter content.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 32
diff changeset
    96
  Type "help", "copyright", "credits" or "license" for more information.
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