0% found this document useful (0 votes)
3 views10 pages

Binary File Notes

The document contains Python code for managing student records using three different data structures: lists, tuples, and dictionaries. Each section includes functions to create and update student details, with the data being serialized using the pickle module. The update functionality allows for modifying a student's marks based on their roll number, with appropriate error handling for file operations.

Uploaded by

Prajan Arun
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Binary File Notes

The document contains Python code for managing student records using three different data structures: lists, tuples, and dictionaries. Each section includes functions to create and update student details, with the data being serialized using the pickle module. The update functionality allows for modifying a student's marks based on their roll number, with appropriate error handling for file operations.

Uploaded by

Prajan Arun
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Update using List:

SOURCE CODE:
import pickle
def create():
f = open("[Link]","wb")
L = []
while True:
roll_no = input("Enter roll number:")
name = input("Enter Name:")
mark = int(input("Enter Marks:"))
[Link]([roll_no,name,mark])
ch = input("Do you want to add another student detail(y/n):")
if ch == "n":
break
[Link](L,f)
[Link]()

create()
def update():
f = open("[Link]","rb+")
roll_no = input('Enter roll number to check: ')
mark = int(input('Mark to change: '))

while True:
try:
pos = [Link]() # store position
data = [Link](f)
for i in data:
if i[0] == roll_no:
i[2] = mark

[Link](pos) # go back
[Link](data, f) # overwrite
print("Mark updated Successfully and Details are:",data)

except EOFError:
break

[Link]()
update()
Output:
Update using Tuple:
import pickle
def create():
f = open("[Link]","wb")
L = ()
while True:
roll_no = input("Enter roll number:")
name = input("Enter Name:")
mark = int(input("Enter Marks:"))
L=L+((roll_no,name,mark),)
ch = input("Do you want to add another student detail(y/n):")
if ch == "n":
break
[Link](L,f)
[Link]()

create()
def update():
f = open("[Link]","rb+")
roll_no = input('Enter roll number to check: ')
mark = int(input('Mark to change: '))
try:
data = [Link](f) # load entire tuple

L = list(data) # convert tuple → list (to modify)

for i in range(len(L)):
if L[i][0] == roll_no:
temp = list(L[i]) # tuple → list
temp[2] = mark # update mark
L[i] = tuple(temp) # list → tuple (store back)

data = tuple(L) # convert back to tuple

[Link](0)
[Link](data, f) # overwrite file

print("Mark updated successfully.")


print("Updated Data:", data)

except EOFError:
print("File is empty")

[Link]()
update()
OUTPUT:
Update using Dictionary:
import pickle

def create():
f = open("[Link]","wb")
D = {}

while True:
roll_no = input("Enter roll number: ")
name = input("Enter Name: ")
mark = int(input("Enter Marks: "))

D[roll_no] = (name, mark) # dictionary entry

ch = input("Do you want to add another student detail(y/n): ")


if ch == "n":
break

[Link](D, f)
[Link]()

create()

def update():
f = open("[Link]","rb+")

roll_no = input('Enter roll number to check: ')


mark = int(input('Mark to change: '))

try:
data = [Link](f) # load dictionary

if roll_no in data:
name = data[roll_no][0] # keep name same
data[roll_no] = (name, mark) # update mark

[Link](0)
[Link](data, f)

print("Mark updated successfully.")


print("Updated Data:", data)
else:
print("Roll number not found")

except EOFError:
print("File is empty")

[Link]()

update()
output:

Binary Questions:
1.

2.

3.
4.

5.

6.

7.

8.
9.

10.

You might also like