Archive for the ‘Python’ Category

Chapter 8 – Python Dictionary Datatypes

 
Step 1
 
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
 
#!/usr/bin/python
#Author : Jijo K Jose
 
print "\n————————————\nChapter 8 – Dictionaries\n————————————\n"
 
name = {'fname':'Jijo','mname':'K','lname':'Jose'}
 
print "\n", type(name)
 
print "\nDictonary Values : ", name
 
print "\nname['mname'] : " + name['mname']
 
print "\nAll keys -keys() : ", name.keys()
 
print "\nAll values -values() : ", name.values()
 
del name['mname']
print "\nDelete value del name[‘key’] : ", name
 
print "\nIterate through for loop items… -name.iteritems()\n"
for k,v in name.iteritems():
print k + "—->" + v
 
name['fname']= ['Jijo','Jibin','Jijan']
name['mname']= 'K'
name['lname']= 'Jose'
print "\nAdd more value to same key -name['fname']='value' : ", name
 
print "\n————————————\n"
 
#END
 

Read more »

Chapter 7 – Python List Datatypes

 
Step 1
 
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
 
#!/usr/bin/python
#Author : Jijo K Jose
 
print "\n————————————\nChapter 7 – List\n————————————\n"
 
l1 = [1,2,3,4]
l2 = [5,6,7,8]
 
print "List Manipulations L1 : " ,l1
print "List Manipulations L2 : " ,l2
 
l1.reverse()
print "\nList reverse : ", l1

l1.append(l2)

print "\nList append (Create multi diamension list)  : ", l1 
 
print "\nElement [4][2] is : ", l1[4][2]
 
l1 = [1,2,3,4];
l2 = [5,6,7,8];
 
l1.extend(l2)
print "\nList extend (Create single diamension list)  : ", l1
 
l1.pop()
print "\nRemove last element from list -pop() : ",l1
 
l1.pop(2)
print "\nRemove 2nd element from list -pop(2) : ",l1
 
l1.insert(2,3)
print "\nInsert element into list -insert(,i,val) : ",l1
 
l3=range(5,27,2)
print "\nCreate a range of integers – range(init,end,step) : ",l3
 
print "\n————————————\n"
 
#END
 

Read more »

Chapter 5 – Python String Operations Part 2

 
Step 1
 
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
 
#!/usr/bin/python
#Author : Jijo K Jose
 
print "\n————————————\nChapter 5 – String 2\n————————————\n"
 
import string
 
a = raw_input("Enter any string : ")
print "%s contains %d character\n" %(a,len(a))
 
b = string.upper(a)
c = a.lower()
d = a.capitalize()
e = string.capwords(a)
 
print "%s in different forms are :"
print b;
print c;
print d;
print e;
 
print "\nSplit function result\n———————————————"
f = a.split()
print f
 
print "\nJoin function result\n———————————————"
g = string.join(f)
print g
 
print "\n————————————\n"
 
#END

 

Read more »