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.

*Script


*Hello and welcome to this tutorial on Basic Python using Python.

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 . 

In numbers Python supports three kinds of data types ,

floats,integers and complex numbers

An integer can be defined as follows :
a=13

This make a an integer variable with value 13 .

You can also type 9 around 20 times 

a=99999999999999999999999 . as you can see Python does not have a limit on how long an integer has to be . Isn't that great . 

Now will try a float.

let's type 
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 . 

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 .
Type:
c = 3+4j
As you can see ,the notation for complex numbers is similar to the one used in electric engineering. 
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 :
abs in parenthesis c . 

Do get the imaginary part of c you can do :

c.imag

and similarly for real part do :

c.real

Python also has Boolean as a built-in type .

Try it out just type ..
 t=True , note that T in true is capitalized .    
  
You can apply different Boolean operations on t now for example :


f=not t , this saves the value of not t that is False in f. 

We can apply other operators like or and and ,

f or t gives us the value True while 
f and t gives us the value false.

You can use parenthesis for precedence , 

Lets write some piece of code to check this out .

a=False
b=True
c=True

To check how precedence changes with parenthesis . We will try two expressions and their evaluation.

do
(a and b) or c 
 
This expression gives the value True

where as the expression a and (b or c) gives the value False .

Now we will have a look at strings 

type 
w="hello"

w is now a string variable with the value "hello"

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 . 

Also functions like len work with strings just like the way they did with lists

Now lets try changing a character in the string in the same way we change lists .

type :
w[0]='Capital H'  

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 .