Jan 2, 2013

Python Application 4 - Replace Text Content


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 22 - Regular Expression 5 \n------------------------------------\n"
import re
in_str = raw_input("Enter a text         : ")
se_str = raw_input("Enter search string  : ")
re_str = raw_input("Enter replace string : ")
count = 10
reg1 = re.compile(se_str)
print "Result : ",reg1.sub(re_str,in_str,count)
print "\n-----------------------------------\n"
#END

Python Application 3 - Check Email Address

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 20 - Regular Expression 4 \n------------------------------------\n"
import re
in_str = raw_input("Enter an email address : ")
se_str = "\w+@\w+\.[com|in|org|net]"
reg1 = re.compile(se_str)
search1 = reg1.search(in_str)

if search1:
 print "\nValid email address"
else:
 print "\nInvalid email address"

print "\n-----------------------------------\n"
#END

Python Application 2 - Extract File Content

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
f1 = open("logfile","r")
for i in f1.readlines():
 j = i.split(' ')
 print j
#END

Python Application 1 - Format File Content

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
input_file = raw_input("\nEnter input file name : ")
output_file = "file_new.txt"
h1 = open(input_file,"r")
print "\n----------------------------"
print "Input File Content"
print "----------------------------\n"
for i in h1:
 print i,

h1.close()
h1 = open(input_file,"r")
h2 = open(output_file,"w")
k=0
for i in h1:
 i2 = i.split(',')
 for j in i2:
  n = len(j)
  if j[n-1] =="\n":
   h2.write(j)
 else:
  h2.write(j)

  h2.write("\n")

h1.close()
h2.close()

print "\n---------------------------"
print "Output File Content - file_new.txt"
print"----------------------------\n"
h1 = open(output_file,"r")
for i in h1:
 print i,
print "\n---------------------------\n"
#END

Chapter 22 – Python Regular Expression Part 5

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 22 - Regular Expression 5 \n------------------------------------\n"
import re
in_str = raw_input("Enter a text         : ")
se_str = raw_input("Enter search string  : ")
re_str = raw_input("Enter replace string : ")
count = 2
reg1 = re.compile(se_str)
print "Result : ",reg1.sub(re_str,in_str,count)

print "\n-----------------------------------\n"
#END

Chapter 21 – Python Regular Expression Part 4

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 21 - Regular Expression 4 \n------------------------------------\n"
import re
in_str = raw_input("Enter an email address : ")
se_str = "\w+@\w+\.[com|in|org|net]"
reg1 = re.compile(se_str)

search1 = reg1.search(in_str)
if search1:
 print "\nValid email address"
else:
 print "\nInvalid email address"
print "\n-----------------------------------\n"

#END

Chapter 20 – Python Regular Expression Part 3


 


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 20 - Regular Expression 3 \n------------------------------------\n"


 


import re


 


in_str = raw_input("Enter the text : ")


 


se_str = raw_input("Enter search string : ")


 


reg1 = re.compile(se_str)


 


find1 = reg1.findall(in_str)


 


print "\nOutput of - .findall() : ", find1 


 


print "\n-----------------------------------\n"


 


iter1 = reg1.finditer(in_str)


 


print "Output of - .finditer() : \n"


 


for i in iter1:


 print i.group() 


 


print "\n-----------------------------------\n"


 


#END



Chapter 19 – Python Regular Expression 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 19 - Regular Expression 2 \n------------------------------------\n"


 


import re


match_str = "jijo k 50 JOSE"


search_str = "[a-z]+.*"


 


print "Input String : %s"%match_str


 


print "\n-----------------------------------\n"


 


reg1 = re.compile(search_str,re.IGNORECASE)


match = reg1.match(match_str)


 


print "Address Location : ",reg1.match(search_str)


print "Search string : ",search_str


print "Matched string : ",match.group()


 


print "\n-----------------------------------\n"


 


search_str = '[a-z]+\s[a-z]\s[0-9]+\s\w[A-Z]'


reg1 = re.compile(search_str)


match = reg1.match(match_str)


 


print "Search string : ",search_str


print "Matched string : ",match.group()


 


print "\n-----------------------------------\n"


 


#END



Chapter 18 - Python Regular Expression Part 1


 


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 18 - Regular Expression 1 \n------------------------------------\n"


 


import re


match_str = raw_input("Enter a text : ")


search_str = raw_input("Enter search string : ")


 


reg1 = re.compile(search_str,re.IGNORECASE)


 


print "Address Location : ",reg1.match(match_str)


 


print "\n-----------------------------------\n"


 


match = reg1.match(match_str)


 


if match:


 print "Match - Found : ",match.group()


else:


 print "No match found !!! "


 


print "\n-----------------------------------\n"


 


search = reg1.search(match_str)


 


if search:


 print "Search Found : ",search.group()


else:


 print "No searched result !!!"


 


print "\n-----------------------------------\n"


 


#END



Chapter 17 - Python High Level File I/O Operations


 


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


 


import shutil


 


print "\n------------------------------------\nChapter 17 - High level File I/O \n------------------------------------\n"


 


s = shutil


 


s.copy("data1","data5")


 


print "File data1 copied to data5 -shutil.copy() \n"


 


s.copy2("data1","data6")


 


print "File data1 copied to data6 without changing timestamp -shutil.copy1() \n"


 


s.move("data5","temp/data7")


 


print "File data5 moved to temp/data5 -shutil.move() \n" 


 


srcdir = "temp"


desdir = "temp2"


 


s.rmtree(desdir)


 


print "Remove the whole directory tree -shutil.rmtree() \n"


 


s.copytree(srcdir,desdir)


 


print "Copy the whole directory tree -shutil.copytree() \n"


 


print "\n----------------------------------------"


 


#END



Chapter 16 - Python Functions


 


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 16 - Functions \n------------------------------------\n"


 


def sum_mul(a,b):


 return a+b,a*b


 


c,d = sum_mul(10,20)


 


print "Sum = %d and Product = %d" %(c,d) 


 


print"----------------------------------------"


 


#END



Chapter 15 - Python Exceptions


 


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 15 - File Writing \n------------------------------------\n"


 


while 1:


 f1=raw_input("Enter the file name : ")


 try:


  h1 = open(f1,"r")


  print "\nFile contents are :\n",h1.read()


  break 


 except:


  print "Problem in opening file : ",f1


 


print"----------------------------------------"


 


#END



Jan 1, 2013

Chapter 14 – Python File Write Operations


 


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 14 - File Writing \n------------------------------------\n"


 


h1 = open("data1","r")


h2 = open("data2","w")


h3 = open("data3","w")


print h1


 


h1 = open("data1","r")


s = h1.readlines()


print "\nReading the Content - readlines() : ",s


 


for i in s:


 h2.write(i)


 


print "File contents are written to data2 using write()"


 


h3.writelines(s)


print "File contents are written to data3 using writelines()"


 


print "\n--------------------------------------\n"


 


h1.close()


h2.close()


 


#END



Chapter 13 - Python File Read Operations


 


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 13 - File Reading \n------------------------------------\n"


 


h1 = open("data1","r")


print h1


 


print "\nReading the first line -readline() : ",h1.readline()


 


h1 = open("data1","r")


print "\nReading the full content - read() : \n",h1.read()


 


h1 = open("data1","r")


print "\nReading the First 25 Character - read(25) : ",h1.read(25)


 


h1 = open("data1","r")


print "\nReading the Content - readlines() : ",h1.readlines()


 


print "\n--------------------------------------\n"


 


#END



Chapter 12 - Python While Loop


 


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 12 - While Loop \n------------------------------------\n"


 


c=1


while(c!=0):


 i = input("Enter an integer : ")


 while(i > 0):


  print i


  i -= 1


 c = input("To quit press 0 : ")


 


print "\n--------------------------------------\n"


 


#END



Chapter 11 - Python For Loop


 


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 11 - For Loop \n------------------------------------\n"


 


for i in range(10,25,2):


  print i


 


print "\n--------------------------------------\n"


 


#END



Chapter 10 - Python Command Line Arguments


 


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 10 - Command Line Parameters \n------------------------------------\n"


 


import sys


 


if(len(sys.argv) < 3):


 print "You should enter 2 argument"


elif(len(sys.argv) >=3):


 print "You entered %d argument " %(len(sys.argv)-1)


 print "The sys.argv values are : ", sys.argv


 print "sys.argv values in separte lines are...."


 for i in sys.argv:


  print i


 


print "\n--------------------------------------\n"


 


#END




Chapter 9 - Python Conditionals


 


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 9 - Conditionals \n------------------------------------\n"


 


a = input("Enter integer A : ")


b = input("Enter integer B : ")


if a == b:


 print "A and B are same"


elif a < b:


 print "B is larger"


else:


 print "A is larger"


 


print "\n--------------------------------------\n"


 


#END



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


 


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


 


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



 


Chapter 4 – Python String Operations Part 1


 


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 4 - String 1\n------------------------------------\n"


 


a = raw_input("Enter any string : ")


print "%s contains %d character" %(a,len(a))


print "\nLast character of %s is : %s" %(a,a[len(a)-1])


print "\nFirst 3 characters are : %s" %(a[0:3])


print "\nEach Characters are..."


for ch in a:


print ch


b = raw_input("\nEnter second string : ")


if a==b:


print "\nStrings are equal"


else:


print "\nStrings are not equal"


 


print "\n------------------------------------\n"


 


#END


 




Chapter 3 – Python Keyboard Inputs


 


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 3 - STDIN Input\n------------------------------------\n"


 


msg = raw_input("Enter Your Name : ")


age = input("Enter your age : ")


print "Your name is :" , msg


print "%s, your age is : %d" %(msg,age)


 


print "\n------------------------------------\n"


 


#END



 


Chapter 2 - Python Math Functions


 


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 2 - Math Function\n------------------------------------\n"


 


print "100 divided by 16 is:", 100 / 16 , "& Remainder is:", 100 % 16


 


#END


 


Chapter 1 - Python Hello World Program


 


Step 1


 


Open any text editor (gedit,pico,vi,nano)


 


python-code-open


 


Enter the following python script and save the file with .py extension.


 


#!/usr/bin/python


#Author : Jijo K Jose


 


a="Hello World"


print 'First program was'


print a + "."


 


#END


 

Introduction to Python Programming Language


 


Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.


 


The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python Web site, http://www.python.org/, and may be freely distributed. The same site also contains distributions of and pointers to many free third party Python modules, programs and tools, and additional documentation.


 


In this tutorial am using linux operating system for compiling python script.


 


Step 1


 


First you need to check, python is already installed in your system by using the following command


 


which python