basic-python.txt
author Shantanu <shantanu@fossee.in>
Tue, 20 Apr 2010 12:58:15 +0530
changeset 92 fa26bdda8f32
parent 78 099a2cc6c7d2
child 93 bdee3ead116d
permissions -rw-r--r--
Data types covered.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
73
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
     1
*Script
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
     2
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
     3
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
     4
*Hello and welcome to this tutorial on Basic Python using Python.
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
     5
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
     6
This tutorial formally introduces Python as a language . Through this tutorial we will be able to understand Basic Data types like number , Boolean and strings .Some basic operators , simple input/output and basic conditional flow . 
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
     7
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
     8
In numbers Python supports three kinds of data types ,
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
     9
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    10
floats,integers and complex numbers
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    11
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    12
An integer can be defined as follows :
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    13
a=13
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    14
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    15
This make a an integer variable with value 13 .
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    16
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    17
You can also type 9 around 20 times 
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    18
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    19
a=99999999999999999999999 . as you can see Python does not have a limit on how long an integer has to be . Isn't that great . 
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    20
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    21
Now will try a float.
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    22
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    23
let's type 
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    24
p=3.141592  if you type out p now you will notice that it is not absolutely equal to p you typed in . The reason for that is how a computer saves decimal values . 
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    25
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    26
Apart from integer and float, Python has an in-built support for complex numbers. Now we try to assign a complex value to a variable .
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    27
Type:
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    28
c = 3+4j
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    29
As you can see ,the notation for complex numbers is similar to the one used in electric engineering. 
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    30
We will now try some operations on complex numbers . First we will try to get the absolute value of the complex number . For this we will use the abs built in function . For this do :
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    31
abs in parenthesis c . 
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    32
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    33
Do get the imaginary part of c you can do :
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    34
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    35
c.imag
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    36
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    37
and similarly for real part do :
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    38
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    39
c.real
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    40
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    41
Python also has Boolean as a built-in type .
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    42
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    43
Try it out just type ..
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    44
 t=True , note that T in true is capitalized .    
78
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    45
  
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    46
You can apply different Boolean operations on t now for example :
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    47
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    48
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    49
f=not t , this saves the value of not t that is False in f. 
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    50
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    51
We can apply other operators like or and and ,
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    52
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    53
f or t gives us the value True while 
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    54
f and t gives us the value false.
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    55
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    56
You can use parenthesis for precedence , 
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    57
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    58
Lets write some piece of code to check this out .
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    59
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    60
a=False
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    61
b=True
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    62
c=True
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    63
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    64
To check how precedence changes with parenthesis . We will try two expressions and their evaluation.
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    65
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    66
do
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    67
(a and b) or c 
73
9dffd11728d2 Started on day2 . Adding a script on basic python
amit@shrike.aero.iitb.ac.in
parents:
diff changeset
    68
 
78
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    69
This expression gives the value True
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    70
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    71
where as the expression a and (b or c) gives the value False .
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    72
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    73
Now we will have a look at strings 
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    74
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    75
type 
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    76
w="hello"
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    77
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    78
w is now a string variable with the value "hello"
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    79
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    80
printing out w[0] + w[2] + w[-1] gives hlo if you notice the expression for accessing characters of a string is similar to lists . 
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    81
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    82
Also functions like len work with strings just like the way they did with lists
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    83
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    84
Now lets try changing a character in the string in the same way we change lists .
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    85
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    86
type :
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    87
w[0]='Capital H'  
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    88
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    89
oops this gives us a Type Error . Why? Because string are immutable . You can change a string simply by assigning a new element to it . This and some other features specific to string processing make string a different kind of data structure than lists .  
099a2cc6c7d2 Added a bit more to script of basic python . Session one day 2
amit@shrike.aero.iitb.ac.in
parents: 73
diff changeset
    90