Record Programs 1 To 14
Record Programs 1 To 14
AIM: Write a Python program to search an element in the list and display the frequency of element
present in the list and its location by using user defined function.
SOURCE CODE :
OUTPUT:
PROGRAM 2
AIM : Write a Python program to pass a list to a function, double the odd value, half the even value
and display the changed list.
SOURCE CODE:
OUTPUT :
PROGRAM 3
AIM : Write a Python program to pass a string to a function and count the vowels present in the
string.
SOURCE CODE:
OUTPUT :
PROGRAM 4
AIM : Write a Python program to create a phone directory of 5 friends’ name and phone number.
Create a menu driven program to perform the following :
SOURCE CODE:
OUTPUT :
PROGRAM 5
AIM : Write a random number generator that generates random numbers between 1 and 6 (Simulates
a dice)
SOURCE CODE:
OUTPUT :
PROGRAM 6
AIM : Write a python program to copy all the lines starting with the character ‘A’ in a text file and
write it to another text file.
SOURCE CODE:
PROGRAM 7
AIM : Write a python program to read and display text file content line by line with each word
separated by #
SOURCE CODE:
PROGRAM 8
SOURCE CODE:
PROGRAM 9
AIM :
SOURCE CODE:
file1=open("myfile.txt",'w')
file1.write(str)
file1.close()
file1=open("myfile.txt",'r')
vowels=0
consonants=0
uppercase=0
lowercase=0
str1=file1.read()
for i in str1:
lowercase+=1
uppercase+=1
for j in str1:
j=j.lower()
vowels+=1
else:
if j.isalpha():
consonants+=1
print("lowercase count=",lowercase)
print("uppercase count=",uppercase)
print("vowels count=",vowels)
print("consonants count=",consonants)
PROGRAM 10
AIM :
SOURCE CODE:
import pickle
# write into a file
def write_file():
f=open('student.dat','ab')
roll=int(input('Enter the roll number: '))
name=input('Enter the name: ')
marks=int(input('Enter the marks: '))
data=[roll,name,marks]
pickle.dump(data,f)
f.close()
# Main def
while True:
print('1. Write to the file \n2. Read from the file \n3. Search a record \n4. Exit')
ch=int(input('Enter your choice: '))
if ch==1:
write_file()
elif ch==2:
read_file()
elif ch==3:
search_file()
elif ch==4:
break
else:
print('Wrong option selected.')
OUTPUT:
PROGRAM 11
AIM :
SOURCE CODE:
import pickle
def write_file():
f=open ("stu1.dat",'ab')
d={}
d['rollno']=int(input("enter the roll number:"))
pickle.dump(d,f)
f.close()
def read_file():
f=open("stu1.dat",'rb')
while True:
try:
rec=pickle.load(f)
print(rec)
except EOFError:
break
f.close()
def update_file():
f=open("stu1.dat",'rb+')
found=0
while True:
try:
pos=f.tell()
rec=pickle.load(f)
if d['rollno']==int(m):
d['name']==input("enter the new name:")
f.seek(pos)
pickle.dump(d,f)
found=1
except EOFError:
break
f.close()
if found==0:
#main def
d={}
while True:
print("1. write to the file\n 2. read from the file\n 3. update a record\n 4.
exit")
if ch==1:
write_file()
elif ch==2:
read_file()
elif ch==3:
update_file()
elif ch==4:
break
else:
print("wrong option selected")
PROGRAM 12
AIM :
Write a python program to implement a menu driven program to perform all file
operations in a binary file using dictionary.
SOURCE CODE:
#D ={'eid':23,'name':'Hari','Salary':4500}
import pickle
import OS
def write_b():
f=open("fil1.dat",'ab')
d={}
pickle.dump(d,f)
f.close()
def display():
f=open("fil1.dat",'rb')
while True:
try:
rec= picke.load(f)
print(rec)
except EOFError:
break
f.close()
f=open("fil1.dat",'rb')
found = 0
while True:
try:
rec=pickle.load(f)
if rec['Name']==name:
print(rec)
found=1
except EOFError:
f.close()
break
if found ==0:
def del_b():
f=open("fil1.dat",'rb')
t=open("fil2.dat",'wb')
found = 0
while True:
try:
rec = pickle.load(f)
if rec['EID']!=rn:
pickle.dump(rec,t)
else:
found =1
except EOFError:
break
f.close()
t.close()
if found ==1:
OS.remove('fil1.dat')
OS.remove('fil2.dat')
else:
def update():
f=open("fil1.dat",'rb+')
found = 0
while True:
try:
nos=f.tell()
rec = pickle.load(f)
if rec['EID'] == eid:
rec['Name']=input("Enter the new name")
found =1
print(rec)
f.seek(nos)
pickle.dump(rec,f)
except EOFError:
break
if found ==0:
f.close()
#main
while True:
print("6.Exit")
if ch==1:
write_b()
if ch==2:
display()
if ch==3:
search()
if ch==4:
del_b()
if ch==5:
update()
if ch==6:
break
PROGRAM 13
AIM :
SOURCE CODE:
import csv
def head():
f=open('cd.csv','w',newline='')
csv_w=csv.writer(f)
l=['cid','name','price']
f.close()
def write():
f=open('cd1.csv','a',newline='')
csv_w=csv.writer(f)
cid=int(input('CID: '))
name=input('Name: ')
price=int(input('Price: '))
l=[cid,name,price]
csv_w.writerow(l)
f.close()
def read():
f=open('cd1.csv','r',newline='')
csv_r=csv.reader(f)
print(rec)
f.close()
def search():
f=open('cd1.csv','r',newline='')
csv_r=csv.reader(f)
found=0
if rec[0]==cid:
print(rec)
found+=1
f.close()
if found==0:
while True:
print()
print('1. Write into file \n2. Display Records \n3. Search for a record \n4. Exit')
if ch==1:
write()
elif ch==2:
read()
elif ch==3:
search()
elif ch==4:
break
else:
print('Invalid choice')
PROGRAM 14
AIM :
SOURCE CODE:
import csv
def head():
f=open('cd.csv','w',newline='')
csv_w=csv.writer(f)
l=['cid','name','price']
csv_w.writerow(l)
f.close()
def write():
f=open('cd1.csv','a',newline='')
csv_w=csv.writer(f)
cid=int(input('CID: '))
name=input('Name: ')
price=int(input('Price: '))
l=[cid,name,price]
csv_w.writerow(l)
f.close()
def read():
f=open('cd1.csv','r',newline='')
csv_r=csv.reader(f)
print(rec)
f.close()
def update():
f=open('cd1.csv','r+',newline='')
csv_r=csv.reader(f)
l=[]
if rec[0]==cid:
found+=1
l.append(rec)
if found==1:
f.seek(1)
csv_w=csv.writer(f)
for rec in l:
csv_w.writerow(rec)
else:
f.close()
head()
while True:
print()
print('1. Write into file \n2. Display records \n3. Update a record \n4. Exit')
if ch==1:
write()
elif ch==2:
read()
elif ch==3:
update()
elif ch==4:
break
else:
print('Invalid choice')