Computer Science Practical File
Computer Science Practical File
Computer Science Practical File
Index
3|Page
13. Search a record using
CSV File.
4|Page
20. Find the min, max,
sum, count and
average.
Acknowledgement
I would like to convey my sincere thanks to Mr. Ravi
Kumar Sharma, my Computer Science teacher for his
valuable suggestions and guidance through the course
of this project. He has been a source of inspiration for
me during the completion of this project. He helped
understand and remember the important details of the
practical that I would have otherwise lost. The credit of
my project’s success surely goes to him.
Student’s Signature
5|Page
Date:
School: Ambuja Vidya Niketan
Subject Teacher: Mr. Ravi Kumar Sharma
Certificate
This is to certify that Mas. Vijay Dilip Barhate off class
12th (Science) of Ambuja Vidya Niketan Upparwahi
has completed the practical under my supervision. He
has taken proper care and shown utmost sincerity while
making it. I certify that practical is up to my
expectation and as per guidelines issued by CBSE.
School Stamp
Q.1 Create a Binary File using Pickle.
Input
import pickle
d={}
f=open("Students.dat","ab")
ans='y'
while ans=='y':
rno=int(input("Enter the Roll number of the Student:"))
name=input("Enter the name of the Student:")
marks=float(input("Enter the Marks obtained by Student:"))
d["Roll No"]=rno
d["Name"]=name
d["Marks"]=marks
pickle.dump(d,f)
print("Successfully added a new Record")
ans=input("Do you want to add more Records? press 'y' for
adding more records ......=")
f.close()
7|Page
Output
8|Page
Q.2 Write a program to display a Binary File
content using Pickle.
Input
import pickle
f=open("Students.dat","rb")
d={}
print("Student Name","Roll Number","Marks \n",sep="\t")
try:
while True:
d=pickle.load(f)
print(d["Name"],str(d["RollNo"]),str(d["Marks"])+"\n",sep="\
t"+"\t"+" ")
except EOFError:
f.close()
Output
9|Page
Q.3 Write a Program to delete a record using
Pickle.
Input
import os
import pickle
fin=open("Students.dat","rb")
fout=open("Temp.dat","wb")
d={}
found=False
roll=int(input("Enter the Roll No:"))
print("The Record of Roll No ",roll," will be deleted")
print("Student Name","Roll Number","Marks \n",sep="\t")
try:
while True:
d=pickle.load(fin)
if d["Roll No"]!=roll:
pickle.dump(d,fout)
found=True
10 | P a g e
print(d["Name"],str(d["Roll No"]),str(d["Marks"])+"\
n",sep="\t"+"\t"+" ")
except EOFError:
if found==False:
print("No Record was found with Conditions applied")
fin.close()
fout.close()
else:
fin.close()
fout.close()
os.remove("Students_1.dat")
os.rename("Temp.dat","Students_1.dat")
print("Mission Successful")
Output
11 | P a g e
Q.4 Create a Binary File with roll number, name,
marks. Input a roll number and update the marks.
Input
import os
import pickle
r=int(input("Enter the roll no for which you have to update the
record -"))
f=open("Students.dat","rb")
f1=open("Temp.dat","wb")
d={}
try:
while True:
d=pickle.load(f)
if d["Roll No"]==r:
print("Plz enter new marks for roll no -",r)
m=float(input("Enter the new Marks obtained by the
student -"))
12 | P a g e
d["Marks"]=m
pickle.dump(d,f1)
else:
pickle.dump(d,f1)
except EOFError:
f.close()
f1.close()
os.remove("Students.dat")
os.rename("Temp.dat","Students.dat")
print("Record Updated Successfully")
Output
13 | P a g e
Q.5 Create a Binary File with name and roll
number. Search for a given roll number and
display the name, if not found display appropriate
message.
Input
import pickle
f=open("Student.dat","rb")
14 | P a g e
d={}
print("*********************__Searching_with_Roll_No__***
*******************")
w=int(input("Enter the ROLL NUMBER of the Student you want
to search record of -"))
wf=False
try:
print("Searching Records . ............. . .")
while True:
d=pickle.load(f)
if d["Roll No"]==w:
wf=True
print("Student Name")
print(d["Name"])
except EOFError:
if wf==False:
print("No Record was found")
else:
print("Record found Successfully☝")
f.close()
Output
15 | P a g e
Student.dat file record (for reference)
16 | P a g e
Input
import os
import pickle
f=open("Students.dat","rb")
f1=open("Temp.dat","wb")
d={}
try:
while True:
d=pickle.load(f)
pickle.dump(d,f1)
except EOFError:
f.close()
f1.close()
c=input("Enter the New Name for the File:")
os.remove("Students_1.dat")
os.rename("Temp.dat",c)
print("Record Updated Successfully")
Output
Previous File
17 | P a g e
Copied File
18 | P a g e
Input
f=open("LICENSE_.txt","r")
c=0
p=["t","T","w","W"]
s=f.readlines()
for i in s:
if i[0] in p:
c=c+1
else:
c=c
print("The number of lines starting with the letter",p,"are=",c)
Text File
Output
19 | P a g e
Q.8 Read a text file line by line and display each
word separated by ‘#’.
20 | P a g e
Input
f=open("LICENSE_2.txt","r")
m=f.readlines()
for i in m:
i=i.split()
for j in i:
print(j,end="#")
Text File
Output
21 | P a g e
Q.9 Read a Text File and display the number of
vowels/ consonants/ uppercase/ lowercase
characters in the File.
Input
22 | P a g e
f=open("LICENSE_.txt","r")
s=f.read()
l=0
u=0
v=0
c=0
for i in s:
if i.islower():
l=l+1
elif i.isupper():
u=u+1
i=i.lower()
if i in ['a','e','i','o','u']:
v=v+1
elif i in
['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z']:
c=c+1
Output
23 | P a g e
Text File (for reference)
24 | P a g e
Input
f=open("LICENSE_.txt","r")
g=f.readlines()
f1=open("LICENSEE.txt","w")
l=[]
for i in g:
if "a" in i:
f1.write(i)
else:
l.append(i)
f2=open("LICENSE_.txt","w")
f2.writelines(l)
f.close()
f2.close()
f1.close()
print("All the lines containing character a have been moved to
Another File.")
Output
New File
25 | P a g e
Original File
26 | P a g e
Input
import csv
f=open("Practical.csv","w",newline='')
d=csv.writer(f)
d.writerow(["User ID","Password"])
ans='y'
while ans=='y':
user=input("Enter User ID:")
pas=input("Enter Password:")
a=[user,pas]
d.writerow(a)
print("Successfully added a new Record")
ans=input("Do you want to add more Records? press 'y' for
adding more records ......=")
f.close()
CSV File
27 | P a g e
Searching the password for given user id
import csv
f=open("Practical.csv","r")
d=csv.reader(f)
w=input("Enter UserID:")
for i in d:
if i[0]==w:
print(i[1])
f.close()
Output
Input
28 | P a g e
import os
import csv
r=input("Enter the roll no for which you have to update the record
-")
f=open("Students_2.csv","r")
f1=open("Temp.csv","w",newline='\n')
d=csv.reader(f)
wt=csv.writer(f1)
for i in d:
if i[0]==r:
print("Plz enter new marks for roll no -",r)
m=float(input("Enter the new Marks obtained by the student
-"))
i[2]=m
wt.writerow(i)
f.close()
f1.close()
os.remove("Students_2.csv")
os.rename("Temp.csv","Students_2.csv")
print("Record Updated Successfully")
File Content
29 | P a g e
Output
30 | P a g e
Input
import csv
r=input("Enter the roll no to be searched in the record -")
f=open("Students_2.csv","r")
d=csv.reader(f)
for i in d:
if i[0]==r:
print("searching for Roll Number -",r," in the Record")
a=i[0]
b=i[1]
c=i[2]
print("Rollno","Name","Marks",sep='\t')
print(a,b,c,sep='\t')
f.close()
print("Record found Successfully")
Output
import random
def random_no_generater():
a=random.randint(1,6)
print("The Number on the Dice is -",a)
def main():
n=input("Enter y to start the generating a random number -")
while n=='y':
random_no_generater()
print("\n")
print("press y to again roll the dice.")
n=input("Do you want to roll the dice -")
main()
Output
32 | P a g e
Input
def Push(stk,x):
stk.append(x)
top=len(stk)-1
def Pop(stk):
if stk==[]:
return "UNDERFLOW!"
else:
t=stk.pop()
top=len(stk)-1
print("Removed Value or Item from the list -",t)
def Display(stk):
if stk==[]:
print("UNDERFLOW! Stack is Empty")
else:
top=len(stk)-1
print("Displayinig the Data-")
for i in range(top,-1,-1):
print(stk[i])
33 | P a g e
SList=[]
choice=0
while choice!=4:
print("********************__STACK_OPERATIONS__****
********************")
print("1. Push")
print("2. Pop")
print("3. Display")
print("4. Exit")
choice=int(input("Enter your choice(1 to 4):-"))
if choice==1:
x=int(input("Enter the Value to Push:-"))
Push(SList,x)
elif choice==2:
Pop(SList)
elif choice==3:
Display(SList)
elif choice==4:
exit
else:
print("Invalid Choice")
break
Output
34 | P a g e
Database Management
35 | P a g e
Q.16 Alter table to add new attributes/modify data
type/drop attribute.
36 | P a g e
Modify Datatype
Drop Attribute
37 | P a g e
Query - ALTER TABLE students DROP XYZ;
38 | P a g e
Query - update students set s_name='Riya Sepurwar' where
roll_no=8;
Descending Order
40 | P a g e
Query - select * from students order by roll_no desc;
41 | P a g e
Query - DELETE FROM students WHERE roll_no=12;
42 | P a g e
Query – select
min(marks),max(marks),sum(marks),count(marks),avg(marks) from
students;
Thank You
43 | P a g e