Docsity

Python Binary File Operations and SQL Basics for High School, Assignments of Computer science

Toggle sidebar
bg1
1 SET - 2
Roll Number:
Name:
Date:
Class: 12
Section:
Subject: Computer Science Practical (083)
School Name: GSBV, Burari, Delhi โ€“ 84
SET โ€“ 02
Practical - 1:
Create a binary file with roll number, name and marks of some students. Input a roll number
and update the marks of specific student.
#First we import the pickle module to read and write the binary file in Python
import pickle
#Initialize a Empty dictionary
S={}
while True:
#Create a menu for different type of options
pf3
pf4
pf5

Partial preview of the text

Download Python Binary File Operations and SQL Basics for High School and more Assignments Computer science in PDF only on Docsity!

Roll Number:

Name:

Date:

Class: 12

Section:

Subject: Computer Science Practical (083)

School Name: GSBV, Burari, Delhi โ€“ 84

SET โ€“ 02

Practical - 1:

Create a binary file with roll number, name and marks of some students. Input a roll number and update the marks of specific student.

#First we import the pickle module to read and write the binary file in Python

import pickle

#Initialize a Empty dictionary

S={}

while True:

#Create a menu for different type of options

print("Press 1: For Add Student")

print("Press 2: For Update Student")

print("Press 3: For Exit")

#Input Choice from the user

choice = int(input("Enter Your Choice: "))

if choice == 1:

#Use open() for create and open the binary file in append mode f=open('stud.dat','ab') rno=int(input("Enter the roll no. of the student : ")) name=input("Enter the name of the student: ") marks=int(input("Enter the marks of the student: ")) S['RollNo']=rno S['Name']=name S['Marks']=marks #dump() of pickle module used to write the content in the binary file pickle.dump(S,f) #close the file using close() f.close()

elif choice ==2:

#Now open the binary file in read mode

f=open('stud.dat','rb') try: while True: S=pickle.load(f) #Display the details of the student print(S) except EOFError: f.close() else: break

Output:

Press 1: For Add Student

Press 2: For Update Student

Press 3: For Exit

Enter Your Choice: 1

Enter the roll no. of the student : 1

Enter the name of the student: Amit

Enter the marks of the student: 22

Press 1: For Add Student

Press 2: For Update Student

Press 3: For Exit

Enter Your Choice: 1

Enter the roll no. of the student : 2

Enter the name of the student: Aman

Enter the marks of the student: 18

Press 1: For Add Student

Press 2: For Update Student

Press 3: For Exit

Enter Your Choice: 1

Enter the roll no. of the student : 3

Enter the name of the student: Ankit

Enter the marks of the student: 28

Press 1: For Add Student

Press 2: For Update Student

Press 3: For Exit

Enter Your Choice: 2

Enter the roll no. of the student to be updated: 3

Enter the updated marks of the student: 25

{'RollNo': 1, 'Name': 'Amit', 'Marks': 22}