answer key
answer key
# Write a user defined function in python that displays the number of lines starting with 'H' in the file para.txt
def display():
f=open("h.txt","r")
l=f.readlines() Hit the nail with the hammer.
c=0
for i in l: My friend has a hamster named Bubba.
if i[0]=="H" or i[0]=="h": his hand reached for the sky.
c=c+1
print("To count number of lines starting with H",c) Put your shirt on the hanger.
f.close()
display()
O/P To count number of lines starting with H 2
2. #Write a function countmy() in python to read the text file "DATA.TXT" and count the number of times "my"
occurs in the file.
def countmy():
f=open("DATA.txt","r")
l=f.read() my first book was me and MY family.
w=l.split()
c=0 it gave my chance to be known to the world.
for i in w:
if i=="my" or i=="MY":
c+=1
print("To count the number of times my",c)
countmy()
O/P To count the number of times my 3
3. Write a Python program to read text file “story.txt” line by line and display each word separated by “#”.
4. Write a method/function BIGWORDS() in Python to read contents from a text file CODE.TXT, to count and display
the occurrence of those words, which are having 7 or more alphabets..
def BIGWORDS():
f=open("re.txt","r")
lines=f.readlines()
count=0
for l in lines:
line=l.split()
for word in line:
if len(word) >=7:
count+=1
print("Number of words =",count)
f.close()
BIGWORDS()
5. Write a user-defined function named Count() that will read the contents of text file named “Report.txt” and count
the number of lines which starts with either “I” or “M”.
def count1():
f=open("helllo.txt","r")
lines=f.readlines()
count=0
for w in lines:
if w[0] == "I" or w[0]=="M":
count+=1
print("Number of lines=",count)
f.close()
count1()
6. cust = [["Danish",80,"Maths"], ["Hazik",79,"CS"], ["Parnik",95,"Bio"], ["Danish",70,"CS"],
["Sindhu",99,"CS"]]
status = []
def push_element():
for i in range(len(cust)):
if (cust[i][1]) > 75 and (cust[i][2]) == "CS":
customer = cust[i][0],cust[i][1]
status.append(customer)
def pop_element():
for i in range(len(status)):
print(status.pop())
if len(status) == 0:
print("Stack Empty")
push_element()
pop_element()
7. TU=[]
def PUSH():
admno=int(input("enter the admission number"))
name=input("enter the name")
rec=[admno,name]
STU.append(rec)
def DISPLAY():
if STU==[]:
print("Empty stack")
else:
for st in STU:
if st[1][0]=="A":
print(st[0],st[1])
print("PUSHING")
for a in range(3):
PUSH()
print("DISPLAYING RECORDS")
DISPLAY()
8.
9. import pickle
def createfile():
while True:
fobj=open("ITEMS.DAT","wb")
id=input("Enter id number")
gift=input("Enter gift")
cost=int(input("enter cost"))
rec=[id,gift,cost]
pickle.dump(rec,fobj)
ch=input("enter choice")
if ch in "Nn":
break
createfile()
def economic():
fobj=open("ITEMS.DAT","rb")
try:
while True:
rec=pickle.load(fobj)
if rec[2]>2500:
print(rec)
except EOFError:
fobj.close()
economic()
10. import pickle
def count():
f=open("EMPLOYEE.dat","rb")
try:
while True:
d=pickle.load(f)
if d[2]>50000:
print(d)
except EOFError:
print("Done")
f.close()
count()
11. import pickle
def Count_Rec() ():
f=open("SCHOOL.dat","rb")
C=0
try:
while True:
d=pickle.load(f)
if d[2]<33:
C=C+1
print(d)
except EOFError:
print("Done")
f.close()
Count_Rec()
12. import pickle
def NewEmp():
F= open("EMP.dat","ab")
EmpNO=int(input("Enter Employee Number: "))
Ename= input("Enter Employee Name: ")
Post= input("Enter Employee Post: ")
Salary=int(input("Enter Employee Salary: "))
emp=[EmpNO,Ename,Post,Salary]
pickle.dump(emp,file)
F.close()
NewEMP()
def SumSalary(Post):
F=open("EMP.dat", "rb")
totsalary=0
try:
while True:
emp= pickle.load(F)
if emp['Post'].lower()==Post.lower():
totsalary+=emp['Salary']
except EOFError:
break
print("Total Salary for post total_salaryn",totsalary)
F.close()
SumSalary(Post)
13. import csv
def ADD():
fobj=open("animal.csv","w",newline='')
wo=csv.writer(fobj)
while True:
name=input("enter animal name")
type=input("enter animal type")
food=input("enter anima food")
rec=[name,type, food]
wo.writerow(rec)
ch=input("enter choice")
if ch=='Nn':
break
ADD()
def search():
fobj=open("animal.csv","r",newline='')
ro=csv.reader(fobj)
for i in ro:
if i[2]=="grass":
print(i)
fobj.close()
search()
14.
15. 16.
17.
21.
22. def EvenOdd(L):
result = []
for num in L:
if num % 2 == 0:
result.append(num + 2)
else:
result.append(num + 1)
return result
L = [35, 12, 16, 69, 26]
result_list = EvenOdd(L)
print("Original List:", L)
print("Modified List:", result_list)
23. def DIVI_LIST(NUM_LST):
D_2=[]
D_5=[]
for i in range(len(NUM_LST)):
if NUM_LST[i] % 2 ==0 :
D_2.append(NUM_LST[i])
if NUM_LST[i] % 5 == 0:
D_5.append(NUM_LST[i])
print(D_2)
print(D_5)
NUM_LST=[2,4,6,10,15,12,20]
DIVI_LIST(NUM_LST)
24.
(i) import pickle # Statement 1
(ii) fout = open("temp.dat", "wb") # Statement 2
(iii) rec = pickle.load(fin) # Statement 3
(iv) pickle.dump(rec, fout) # Statement 4
25.
26.
27.
28.