Set - 1
Set - 1
Name: Bhavishya
Class: 12
Section: A
SET - 01
Practical - 1:
Create a binary file with name and roll number of students. Search for a given roll number
and display the name of student, if not found display appropriate message
#First we import the pickle module to read and write the binary file in Python
import pickle
S={}
while True:
1 SET - 1
#Input Choice from the user
if choice == 1:
#Use open() for create and open the binary file in append mode
f=open('stud2.dat','ab')
S['RollNo']=rno
S['Name']=name
#dump() of pickle module used to write the content in the binary file
pickle.dump(S,f)
f.close()
f=open('stud2.dat','rb')
K={}
m=0
try:
while True:
K=pickle.load(f)
if K["RollNo"] == rno:
print(K)
m=m+1
except EOFError:
2 SET - 1
f.close()
if m==0:
f=open('stud2.dat','rb')
try:
while True:
S=pickle.load(f)
print(S)
except EOFError:
f.close()
else:
break
Output:
Press 1: For Add Student
3 SET - 1
Enter the Name of the student: Rohit
4 SET - 1
Practical - 2:
(a) Display the details of class XII students in descending order of their marks.
(c) Display the city with number of students belongs to that city.
City;
5 SET - 1