Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Roll Number: 26665404

Name: Rishabh singh

Date: 19-01-2024

Class: 12

Section: A

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

1 SET - 2
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

2 SET - 2
f=open('stud.dat','rb+')

rno=int(input("Enter the roll no. of the student to be updated: "))

marks=int(input("Enter the updated marks of the student: "))

f.seek(0,0)

m=0

try:

while True:

pos=f.tell()

#load() of pickle module use to read the binary file

S=pickle.load(f)

if S["RollNo"] == rno:

f.seek(pos)

S["Marks"]=marks

pickle.dump(S,f)

m=m+1

except EOFError:

f.close()

if m==0:

print("Student not Found")

else:

3 SET - 2
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

4 SET - 2
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}

5 SET - 2
{'RollNo': 2, 'Name': 'Aman', 'Marks': 18}

{'RollNo': 3, 'Name': 'Ankit', 'Marks': 25}

Press 1: For Add Student

Press 2: For Update Student

Press 3: For Exit

Enter Your Choice: 3

Practical - 2:
(a) Display the details of all employees in descending order of their salary.

Select * from Emp Order by Salary Desc;

(b) Delete the column “Gender” from the table.

Alter Table Emp drop column Gender;

(c) Display the city with number of employees belongs to that city.

Select City, count(City) as 'Number of Employees' from Emp Group by City;

(d) Increase the salary by 10% of all employees.

Update Emp Set Salary = Salary * 1.1;

6 SET - 2

You might also like