basic-python.txt
author amit@shrike.aero.iitb.ac.in
Tue, 20 Apr 2010 13:39:41 +0530
changeset 93 bdee3ead116d
parent 78 099a2cc6c7d2
child 98 8e02b76cf068
permissions -rw-r--r--
Some more additions to basic python ended discussion on string started operators

*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]='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 .  
 
Now lets see some of the ways in which you can modify strings and other methods related to strings .

Type :

a = 'Hello world' 

To check if a particular string starts with a particular substring you can check that with startswith method

a.startswith('Hell')

Depending on whether the string starts with that substring the function returns true or false

same is the case a.endwith('ld')

a.upper()
 returns another string that is all the letters of given string capitalized

similarly a.lower returns all small letters .

Earlier we showed you how to see documentations of functions . You can see the documentation of the lower function by doing a.lower?

You can use a.join to joing a list of strings to one string using a given string as connector . 

for example 

type :
', '.join(['a','b','c'])

In this case strings are joined over , and space

Python supports formatting values into strings. Although this can include very complicated expressions, the most basic usage is to insert values into a string with the %s placeholder. %d can be used for formatting things like integers and %f for floats


Their are many other string formatting options you can look at http://docs.python.org/library/stdtypes.html for more information on other options available for string formatting.


Operators ---- Probably can be a different chapter .

We will start the discussion on operators first with arithmetic operators .

% can be used for remainder for example

864675 % 10 gives remainder 5 


you can use 2 *'s for power operation 

for example 4 ** 3 gives the result 64

One thing one should notice is the type of result depends on the types of input for example :

17 / 2 both the values being integer gives the integer result 2

however the result when one or two of the operators are float is float for example:

17/2.0 
8.5
17.0/2.0 
8.5