Computer Science
Anant has been asked to display all the students who have scored less than 40 for Remedial Classes. Write a user-defined function to display all those students who have scored less than 40 from the binary file "Student.dat".
Answer
Let the file "Student.dat" include the following sample data:
Radhika 80
Shaurya 35
Sonia 38
Anirudh 45
import pickle
def display_students(file_name):
file = open(file_name, 'rb')
students = pickle.load(file)
for student in students:
name = student[0]
score = student[1]
if score < 40:
print(name)
file.close()
display_students('student.dat')
Output
Shaurya
Sonia
Related Questions
Write the output of the following code with justification if the contents of the file "ABC.txt" are:
"Welcome to Python Programming!"
f1 = open("ABC.txt", "r") size = len(f1.read()) print(size) data = f1.read(5) print(data)
Give the output of the following snippet:
import pickle list1, list2 = [2, 3, 4, 5, 6, 7, 8, 9, 10], [] for i in list1: if (i % 2 == 0 and i % 4 == 0): list2.append(i) f = open("bin.dat", "wb") pickle.dump(list2, f) f.close() f = open("bin.dat", "rb") data = pickle.load(f) f.close() for i in data: print(i)
Following is the structure of each record in a data file named "PRODUCT.DAT".
{"prod_code": value, "prod_desc": value, "stock": value}
The values for prodcode and proddesc are strings and the value for stock is an integer.
Write a function in Python to update the file with a new value of stock. The stock and the product_code, whose stock is to be updated, are to be inputted during the execution of the function.
Given a binary file "STUDENT.DAT", containing records of the following type:
[S_Admno, S_Name, Percentage]
Where these three values are:
S_Admno — Admission Number of student (string)
S_Name — Name of student (string)
Percentage — Marks percentage of student (float)Write a function in Python that would read contents of the file "STUDENT.DAT" and display the details of those students whose percentage is above 75.