PRACTICAL FILE
COMPUTER SCIENCE
VISHAL BHARTI SR. SEC. SCHOOL
PITAMPURA , SARASWATI VIHAR
A PRACTICAL FILE IS SUBMITTED TO DEPARTMENT
OF COMPUTER SCIENCE FOR THE FULFILLMENT
OF SSCE EXAMINATION FOR THE ACADEMIC
SESSION - 2023-2024 .
SUBMITTED BY: Sahil Chhabra
SUBMITTED TO:Nomita Ma’am
CLASS:________XII-A_____
ROLL NO.:_________________________
ACKNOWLEDGEMENT
I WISH TO EXPRESS MY DEEP
SENSE OF GRATITUDE AND INDEBTEDNESS TO
OUR LEARNED TEACHER NOMITA SHARMA MAAM ,
PGT COMPUTER SCIENCE , VISHAL BHARTI SR.
SEC. SCHOOL FOR HIS VALUABLE HELP ADVICE
AND GUIDANCE IN THE PREPARATION OF THIS
PROJECT.
I AM ALSO GREATLY INDEBTED TO OUR
PRINCIPAL KIRAN DHALL AND THE SCHOOL
AUTHORITIES FOR PROVIDING ME WITH THE
FACILITIES AND REQUISITE LABORATORY
CONDITIONS FOR MAKING THIS PRACTICAL FILE.
I ALSO EXTEND MY THANKS TO A
NUMBER OF TEACHERS, MY CLASSMATES AND
FRIENDS WHO HELPED ME TO COMPLETE THIS
FILE SUCCESSFULLY.
STUDENT’S NAMES: Sahil Chhabra
DATE:__________________
SIGNATURE:_________________
CERTIFICATE
THIS IS TO CERTIFY THAT Sahil Chhabra,
STUDENT OF CLASS 12TH ,VISHAL BHARTI SR. SEC.
SCHOOL HAS COMPLETED – PRACTICAL FILE
DURING THE ACADEMIC SESSION 2023-2024
TOWARDS FULFILLMENT OF CREDIT FOR
COMPUTER SCIENCE PRACTICAL EVALUATION OF
CBSE AND SUBMITTED SATISFACTORY REPORT,
AS COMPILED IN THE FOLLOWING PAGES, UNDER
MY SUPERVISION.
TOTAL NUMBER OF PRATICAL CERTIFIED ARE:
_____
EXAMINER SIGNATURE:________
DATE:____________
HEAD OF DEPARTMENT SIGN:_____________
PRINCIPAL SIGNATURE:_________
CONTENTS
S.N. Practical topics date signaure
A. PYTHON PROGRAMS
1. Read a text file line by line and
display each word separated
by a #
2. Read a text file and display the
number of
vowels/consonants/uppercase/l
owercase characters in the file.
3. Remove all the lines that
contain the character 'a' in a
file and write it to another file.
4. 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.
5. Create a binary file with roll
number, name and marks.
Input a roll number and update
the marks.
6. Write a random number
generator that generates
random numbers between 1
and 6 (simulates a dice).
7. Write a Python program to
implement a stack using list.
8. Create a CSV file by entering
user-id and password, read
and search the password for
given user id.
B. DATABASE MANAGEMENT
PROGRAMS
9. Create a student table and
insert data. Implement the
following SQL commands on
the student table:
o ALTER table to add new
attributes / modify data type /
drop attribute
o UPDATE table to modify data
o ORDER By to display data in
ascending / descending order
o DELETE to remove tuple(s)
o GROUP BY and find the min,
max, sum, count and average
10. Integrate SQL with Python by
importing suitable module.
QUESTION1: Read a text file line by line and display
each word separated by a #.
FILE:
SOURCECODE: f=open("python
.txt",'r')
lines=f.readlines()
for line in lines:
words=line.split()
for word in words:
print(word+"#",end='')
print(" ")
OUTPUT:
QUESTION2: Read a text file and display the number of
vowels/consonants/uppercase/lowercase characters in
the file.
SOURCE CODE:
fileobject=open("python.txt","r")
k=fileobject.read()
print(k)
vowels=0
consonants=0
uppercase_c=0
lowercase_c=0
for ch in k:
if (ch.islower()):
lowercase_c+=1
elif (ch.isupper()):
uppercase_c+=1
ch=ch.lower()
if( ch in ['a','e','i','o','u']):
vowels+=1
else:
consonants+=1
fileobject.close()
print("total no. of vowels are:",vowels)
print("total no. of consonants are:",consonants)
print("total no. of lower case letters are:",lowercase_c)
print("total no. of upper case letters are:",uppercase_c)
OUTPUT:
QUESTION3: Remove all the lines that contain the
character 'a' in a file and write it to another file.
SOURCE CODE:
#open input file in read mode
input_file=open("python.txt",'r')
#open output file in write mode
output_file=open("abc.txt","w")
lines=input_file.readlines()
for i in lines:
if 'a'not in i:
output_file.write(i)
output_file.close()
input_file.close()
f=open("abc.txt",'r')
k=f.read()
print(k)
f.close()
OUTPUT:
QUESTION4: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.
SOURCECODE:
import pickle
s={}
f=open("stud.dat",'wb')
c="y"
while c=="y" or c=="Y":
rno=int(input("enter the roll no. of the student:"))
name=input("enter the name of the student:")
s['rollno']=rno
s['name']=name
pickle.dump(s,f)
c=input("do you want to add more students(y/n):")
f.clos()
f=open('stu.dat','rb')
rno=int(input("enter the rollno. of the student tob be
search:"))
k={}
m=0
try:
while True:
k=pickle.load(f)
if k["rollno"]==rno:
print(k)
m=m+1
except EOFError:
f.close()
if m==0:
print("student not found")
OUTPUT:
QUESTION5: Create a binary file with roll number, name
and marks. Input a roll number and update the marks.
SOURCE CODE:
import pickle
stu_data={}
#writing to the file
no_of_students=int(input("enter no of students"))
file=open("stud_dat","ab")
for i in range (no_of_students):
stu_data["rollno"]=int(input("enter roll no"))
stu_data["name"]=input("enter student name:")
stu_data["marks"]=float(input("ënter student marks:"))
pickle.dump(stu_data,file)
stu_data={}
file.close()
#reading the file-logic
file=open("D:\\stud_u.dat", "rb")
try:
while True:
student_data=pickle.loat(file)
print(student_data)
except EOFError:
file.close()
#searching and updating(i.e. readind and then writing)
found=False
roll_no=int(input("enter the roll no. to search: "))
file=open("D:\\stud_u.dat", "rb+")
try:
while True:
pos=file.tell()
student_data=pickle.loat(file)
if(student_data["RollNo"]==roll_no):
#print(student_data["Name"]
student_data["Marks"]=float(input("Enter marks to
be updated"))
file.seek(pos)
pickle.dump(student_data,file)
found=True
except EOFError:
if(found==False):
print("Roll no not found please try again")
else:
print("Student marks updated successfully. ")
file.close()
#Displaying logic
file=open("D:\\stud_u.dat","rb")
try:
while True:
student_data=pickle.load(file)
print(student_data)
except EOFError:
file.close()
OUTPUT:
QUESTION6: Write a random number generator that
generates random numbers between 1 and 6 (simulates
a dice).
SOURCECODE:
import random
min=1
max=6
roll_again="y"
while roll_again=="y"or"Y":
print("rolling the diced...")
val=random.randint(min,max)
print("you get...:",val)
roll_again=input("roll the dice again?(y/n)...")
OUTPUT:
QUESTION7: Write a Python program to implement a
stack using list.
SOURCECODE:
# Python program to
# demonstrate stack implementation
# using list
mystack = []
# append() function to push
# element in the stack
mystack.append('p')
mystack.append('y')
mystack.append('t')
mystack.append('h')
mystack.append('o')
mystack.append('n')
print('After inserting element into stack :')
print(mystack)
# pop() function to pop
# element from stack in
# LIFO order
print('\nElements popped from stack:')
print(mystack.pop())
print(mystack.pop())
print(mystack.pop())
print(mystack.pop())
print('\nStack after elements are popped:')
print(mystack)
OUTPUT:
QUESTION8: Create a CSV file by entering user-id and
password, read and search the password for given user
id.
SOURCECODE:
import csv
with open("ABC.csv", "w") as obj:
fileobj = csv.writer(obj)
fileobj.writerow(["User Id", "password"])
while(True):
user_id = input("enter id: ")
password = input("enter password: ")
record = [user_id, password]
fileobj.writerow(record)
x = input("press Y/y to continue and N/n to terminate
the program\n")
if x in "Nn":
break
elif x in "Yy":
continue
with open("ABC.csv", "r") as obj2:
fileobj2 = csv.reader(obj2)
given = input("enter the user id to be searched\n")
for i in fileobj2:
next(fileobj2)
# print(i,given)
if i[0] == given:
print(i[1])
break
OUTPUT:
QUESTION9: Create a student table and insert data.
Implement the following SQL commands on the student
table:
o ALTER table to add new attributes / modify data type /
drop attribute
o UPDATE table to modify data
o ORDER By to display data in ascending / descending
order
o DELETE to remove tuple(s)
o GROUP BY and find the min, max, sum, count and
average