Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

ASSESSED HOMEWORK – 3

M VIGNESH XII A
10/08/22
1. Create a binary file with name and roll number. Search for a given roll number and
display the name, if not found display appropriate message.

SOURCE CODE :
import pickle
f=open('binary.dat', 'wb')
li=[]
n=int(input('Enter number of sublists : '))
for i in range(n) :
for j in range(1) :
name=input('Enter the name :')
roll_no=int(input('Enter the roll number :'))
li.append([name,roll_no])
pickle.dump(li,f)
f.close()
f=open('binary.dat', 'rb')
li=pickle.load(f)
print("%10s"%"Name","%10s"%"Roll no")
for i in li:
print("%10s"%i[0],"%10s"%i[1])
f.close()
ele=int(input('Enter roll no to check : '))
for i in li :
if ele==i[1] :
print('Roll no is found and name of the roll no is', i[0])
break
else :
print('Roll no not found in binary file')

2. Create a binary file with roll number, name and marks. Input a roll number and
update the marks.
SOURCE CODE :
import pickle
f=open('stud.dat', 'wb')
li=[]
n=int(input('Enter number of sublists : '))
for i in range(n) :
for j in range(1) :
name=input('Enter the name :')
roll_no=int(input('Enter the roll number :'))
mark=int(input('Enter the mark :'))
li.append([name,roll_no, mark])
pickle.dump(li,f)
f.close()
f=open('stud.dat', 'rb')
li=pickle.load(f)
print("%10s"%"Name","%10s"%"Roll no","%10s"%"Mark")
for i in li:
print("%10s"%i[0],"%10s"%i[1],"%10s"%i[2],)
f.close()

f=open("stud.dat",'wb')
flag=False
roll=int(input("Enter the roll no to be update : "))
for i in range(len(li)):
if li[i][1]==roll:
s=int(input("Enter new mark :"))
li[i][2]=s
flag=True
print("Record updated")
if not flag:
print("No Record Found")
pickle.dump(li,f)
f.close()
f=open("stud.dat",'rb')
li=[]
li=pickle.load(f)
print("%10s"%"Name","%10s"%"Roll no","%10s"%"Mark",)
for s in li:
print("%10s"%s[0],"%10s"%s[1],"%10s"%s[2],)
f.close()

3. Create a binary file with roll number, name and marks. Input a roll number and delete
the marks.
Source Code :
import pickle
f=open('stud.dat', 'wb')
li=[]
n=int(input('Enter number of sublists : '))
for i in range(n) :
for j in range(1) :
name=input('Enter the name :')
roll_no=int(input('Enter the roll number :'))
mark=int(input('Enter the mark :'))
li.append([name,roll_no, mark])
pickle.dump(li,f)
f.close()
f=open('stud.dat', 'rb')
li=pickle.load(f)
print("%10s"%"Name","%10s"%"Roll no","%10s"%"Mark")
for i in li:
print("%10s"%i[0],"%10s"%i[1],"%10s"%i[2],)
f.close()

f=open("stud.dat",'wb')
roll=int(input("Enter the roll no to delete :"))
li2=[]
for i in range(len(li)):
if li[i][1]!=roll:
li2.append(li[i])
pickle.dump(li2,f)
f.close()
f=open("stud.dat",'rb')
li2=[]
li2=pickle.load(f)
print("Student Records")
print("%10s"%"Name","%10s"%"Roll no","%10s"%"Mark",)
for s in li2:
print("%10s"%s[0],"%10s"%s[1],"%10s"%s[2],)
f.close()

4. Create a binary file with empid, name and salary and display the number of records
present in the file.
SOURCE CODE:
import pickle
f=open('binary.dat', 'wb')
li=[]
n=int(input('Enter number of sublists : '))
for i in range(n) :
for j in range(1) :
empid=int(input('Enter the empid : '))
name=input("Enter the name : ")
salary=int(input("enter salary: "))
li.append([empid,name,salary])
pickle.dump(li,f)
f.close()
f=open('binary.dat', 'rb')
li=pickle.load(f)
print("%10s"%"empid","%10s"%"name","%10s"%"salary")
for i in li:
print("%10s"%i[0],"%10s"%i[1],"%10s"%i[2])
f.close()

5. Create a binary file with empid, name and salary. Calculate the average salary of the
employee whose salary is greater than 75,000.
SOURCE CODE:
import pickle
f=open('binary.dat', 'wb')
li=[]
n=int(input('Enter number of sublists : '))
for i in range(n) :
for j in range(1) :
empid=int(input('Enter the empid : '))
name=input("Enter the name : ")
salary=int(input("enter salary: "))
li.append([empid,name,salary])
pickle.dump(li,f)
f.close()
f=open('binary.dat', 'rb')
li=pickle.load(f)
print("%10s"%"empid","%10s"%"name","%10s"%"salary")
for i in li:
print("%10s"%i[0],"%10s"%i[1],"%10s"%i[2])
sumsal=0
count=0
for i in li :
if i[2]>7500 :
count+=1
sumsal+=i[2]
avg=sumsal/count
print('average salary of employee whose salary is greater than 75,000 is ', avg)

You might also like