Saturday, January 29, 2011

Python concepts

# not to exit just after the execution of the prgram

>>raw_input("presss")
-------------------------------------------------------------

#Add a string to a number
>>num=12 this won't work
>>print "tashi is" + num

-------------------------------------------------------------

tips:
>>num =str(12) this works
>>print "tashi is "+ num

--------------------------------------------------------------
using indexing

>>family = ['mom','dad','bro','sis','dog']
>>family[2]
# it will print bro

---------------------------------------------------------------
using method

>>say=['hey','now','brown','cow']
>>say
['hey','now','brown','car']

# so while i want find the index of brown
>>say.index['brown']
>>2

#now i want to insert 'hoss' in the place of 'brown'
>>say.insert[2,'hoss']
>>say
'hey','now',hoss','brown','cow'

#delete the value
>>say.pop(1)
>>now
'hey','hoss',brown,'cow'

#delete by content
>>say.delete('brown')
>>say
'hey','hoss','cow'


#reverse the values
>>say.reverse()
>>say
'cow','hoss','hey'

----------------------------------------------------------------
soring and tubles


>>new=(32,54,22,7,98,1)

>>new.sort()
>>new
[1,7,22,32,54,98]

# to sort a string

>>sorted['Eassyhoss']
['E','a','o','s','s','y']

-----------------------------------------------------------------

add variable in string

>>bucky="hey there %s,hows your %s"
>>verb=('betty','foot')
>>print bucky % verb
>>hey there betty , hows your foot

#to find in string
example="hey now bessie nice chops"
>>example
>>'heu now bessie noce chops'
>>example.find('bessie')
>>8 # i.e the starting index of bessie


# to replace a string
>>truth="i love old woman"
>>truth
>>'i love old woman'
>>truth.replace('woman','man')
>>'i love old man'

--------------------------------------------------------------------
Dictionary

>>>book={'Dad':'Bob','Mom':'Lisa',Bro:'joe'}
>>>book
{'Dad':'Bob','Mom':'Lisa','Bro':'Joe'}
>>>book['Dad']
'Bob'
>>>book['mom']
'Lisa'

-----------------------------------------------------------------------
if statement

>>>fish="tuna"
>>>if fish=="bass":
print "this is a bass alright"
>>>elif fish == "salman":
print "i am not salman"
>>>elif fish=="ruma":
print "i am fish"
>>>else:
print "I dont knnow what this is:"
-----------------------------------------------------------------------
nested if statement
thing = "animal"
animal= "cat"

if thing == "animal":
if animal == "cat":
print "this is animal"
else:
print "I dont know what this animal is "

else:
print "I dont know what this thing is"

-------------------------------------------------------------------------
testing

>>>9<7
>>>False

>>>9==9
>>>true

>>>9!=3
>>>Truth

---------------------------------------------------------------------------
And and or

>>>if example > 3 and example < 10:
print "number is between 3 and 10"


>>>if example is > 3 or example < 4:
print "this works"

----------------------------------------------------------------------------