Listofpractical 22 23
Listofpractical 22 23
Class=XII-A
ROLL NO.=12144
Session=2022-23
PRACTICAL FILE
COMPUTER
SCIENCE
PRACTICAL FILE- COMPUTER SCIENCE (083)
LIST OF PRACTICALS (2022-23) CLASS-XII
Programming Language : Python
S.
No.
NAME OF PRACTICAL
10. Read a text file and display the number of vowels/ consonants/
uppercase/ lowercase characters in the file.
def Count():
f=open('abc.txt')
cv=cc=cu=cl=0
s=f.read()
for i in s:
if i.isupper():
cu+=1
else:
cl+=1
if i in 'aeiouAEIOU':
cv+=1
else:
cc+=1
print('no of vowels:',cv)
print('no of consonants:',cc)
print('no of uppercase char:',cu)
print('no of lowercase char:',cl)
f.close()
Count()
11. Write a function in PYTHON to count and display the number
of words starting with alphabet ‘A’ or ‘a’ present in a text file
“LINES.TXT”.
Example:
If the file “LINES.TXT” contains the following lines,
“A boy is playing there. There is a playground. An aeroplane is
in the sky.Are you getting it?”
The function should display the output as 5.
def count():
c=0
with open("lines.txt") as f:
for ch in f:
ch=ch.split()
for word in ch:
if word[0]=='A' or word[0]=='a':
c+=1
print (c)
count()
def fact(no):
f=1
for i in range(no,0,-1):
f=f*i
return f
def sin(x,n):
s=0
f=1
p=0
k=-1
for i in range(3,n+1):
f=fact(i)
s=s+(x**i)/f
k=-k
return s
n1=int(input("enter first no:"))
n2=int(input("enter second no:"))
ans=sin(n1,n2)
print(ans)
14. Write a python code to find the sum of all elements of a list
those which are divisible by 7.
def sum(l):
s=0
for i in l:
if i%7==0:
s+=i
print('sum: ',s)
l=[]
x=int(input('”enter length of list:”))
for i in range(x):
n=int(input(“enter number:”))
l.append(n)
sum(l)
def wordchar():
f=open("fun.txt")
for ch in f:
ch=ch.split()
for word in ch:
if word[-1] in ".,:;":
word = word[:-1]
if len(word)==4:
print(word)
f.close()
wordchar()
16. Write a definition for function BUMPER() in PYTHON to read
each object of a binary file GIFTS.DAT, find and display details
of those gifts, which have remarks as “ON DISCOUNT”.
Assume that the file GIFTS.DAT is created with the help of lists
of following type:
(ID, Gift, Remarks, Price)
def BUMPER():
import pickle
f=open('GIFTS.dat','rb')
try:
while True:
record=pickle.load(f)
if record[2]=='ON DISCOUNT':
for i in record:
print(i,end=' ')
except:
pass
f.close()
BUMPER()
17. Create a binary file with name and roll number. Search for a
given roll number and display the name, if not found display.
def bin():
import pickle
f=open('abc.dat','ab')
for i in range(5):
name=input('enter name:')
r_no=int(input('enter roll no:”))
record={r_no:name}
pickle.dump(record,f)
f.close()
def search():
import pickle
r_no = int(input(“enter roll no to search:”))
f=open('abc.dat','rb')
flag=False
try:
while True:
record=pickle.load(f)
if r_no in record:
print(record[r_no])
flag=True
except:
pass
f.close()
if not flag:
print('not found')
bin()
search()
18. Create a binary file with roll number, name and marks. Input a
roll number and update the marks.
def create():
import pickle
f=open('ndata.dat','wb')
for i in range(3):
name=input('enter name:')
r_no=int(input('enter roll no:'))
marks=int(input('enter marks: '))
record=[r_no,name,marks]
pickle.dump(record,f)
f.close()
def modify():
import pickle,os
r_no = int(input('rnter roll no to search: '))
f=open('ndata.dat','rb')
t=open('temp.dat','wb')
flag=False
try:
while True:
record=pickle.load(f)
if record[0]==r_no:
nm=int(input('enter marks: '))
record[2]=nm
flag=True
pickle.dump(record,t)
except:
pass
f.close()
t.close()
if not flag:
print('not found')
os.remove('ndata.dat')
os.rename('temp.dat','ndata.dat')
create();modify()
19. Following is the structure of each record in a data file named
”PRODUCT.DAT”.
{"prod_code":value, "prod_desc":value, "stock":value}
The values for prod_code and prod_desc are strings, and the
value for stock is an integer. Write a function in PYTHON to
update the file with a new value of stock. The stock and the
product_code, whose stock is to be updated, are to be input
during the execution of the function.
def update_pro():
import pickle,os
temp=open("file.tmp",'wb')
with open("PRODUCT.DAT",'rb') as f:
pc=input("enter product code: ")
st=int(input(eEnter stock: "))
found=0
while(1):
try:
rec=pickle.load(f)
if rec['prod_code']==pc:
rec['stock']=st
found=1
pickle.dump(rec,temp)
except:
break
temp.close()
if found==0:
print("record not found")
else:
os.remove("product.dat")
os.rename("file.tmp","product.dat")
print("record updated")
20. Write a program to perform read and write operation with .csv
file.
def func():
import csv
f=open('data.csv','w',newline='')
wtr=csv.writer(f)
for i in range(5):
user_id=input('enter ID: ')
pwd=input('enter PWD: ')
wtr.writerow([user_id,pwd])
f.close()
def read():
import csv
f=open('data.csv')
rdr=csv.reader(f)
for i in rdr:
print(i[0],i[1],sep='=>')
f.close()
func()
read()
21. Write a menu based program to perform the operation on stack
in python.
stack=[]
def push():
n=int(input(“enter number:”))
stack.append(n)
print('value added successfully')
def isEmpty():
return len(stack)==0
def pop():
if isEmpty():
print('underflow-stack is empty')
return
stack.pop()
def display():
for i in range(-1,-1-len(stack),-1):
print(stack[i])
def mainmenu():
import sys
while True:
print('1.push')
print('2.pop')
print('3.display')
print('4.exit')
choice=input('enter choice[1-4]: ')
if choice.isdigit():
choice=int(choice)
else:
print('invalid choice')
continue
if choice==1:
push()
elif choice==2:
pop()
elif choice==3:
display()
elif choice==4:
sys.exit()
else:
print('invalid choice')
mainmenu()
22. Write a program to connect Python with MySQL using database
connectivity and perform the
following operations on data in database: Fetch, Update and delete the
data.
def Fetch()
import mysql.connector as sql
con=sql.connect(host='localhost',user='root',passwd='1234',db='ncjps')
cur=con.cursor()
query='select * from employee'
cur.execute(query)
data=cur.fetchall()
for row in data:
print(row)
con.close()
def Update():
import mysql.connector as sql
con=sql.connect(host='localhost',user='root',passwd='1234',db='ncjps')
cur=con.cursor()
ns=int(input('Enter New Salary: '))
empno=int(input('Enter Employee Number: '))
query='update employee set salary={} where empno ={}'.format(ns,empno)
cur.execute(query)
con.commit()
con.close()
def Delete():
import mysql.connector as sql
con=sql.connect(host='localhost',user='root',passwd='1234',db='ncjps')
cur=con.cursor()
empno=int(input('Enter Employee Number: '))
query='delete from employee where empno={}'.format(ns,empno)
cur.execute(query)
con.commit()
con.close()
Fetch()
Update()
Delete()
23. Consider the following tables STORE and SUPPLIERS and answer (a)
and (b) parts of this question:
Table: - Store
ItemNo. Iteam Scode Qty Rate LastBuy
2005 Sharpener Classic 23 60 8 31-Jun-09
2003 Ball Pen 0.25 22 50 25 01-Feb-10
2002 Gel Pen Premium 21 150 12 24-Feb-10
2006 Gel Pen Classic 21 250 20 11-Mar-09
2001 Eraser Small 22 220 6 19-Jan-09
2004 Eraser Big 22 110 8 02-Dec-09
2009 Ball Pen 0.5 21 180 18 03-Nov-09
Table:- Supplier
Scode Sname
21 Premium Stationers
23 Soft Plastics
22 Tetra Supply
A.Write SQL commands for the following statements:
(i) To display details of all the items in the Store table in ascending
order of LastBuy.
(ii) To display ItemNo and Item name of those items from Store
table, whose Rate is more than 15 Rupees.
(iii) To display the details of those items whose Supplier code (Scode)
is 22 or Quantity in Store (Qty) is more than 110 from the table
Store.
SELECT * FROM STORE A,SUPPLIER B WHERE A.Scode=22 AND Qty>110;
23
22
21
Item Sname
Gel Pen Classic Premium Stationers
MAX(LastBuy)
24-Feb-10
Table : CUSTOMER
C_ID CustomerName City I_ID
01 N Roy Delhi LC03
06 H Singh Mumbai PC03
12 R Pandey Delhi PC06
15 C Sharma Delhi LC03
16 K Agarwal Banglore PC01
IV. To increase the Price of all Items by 1000 in the table Item.
TABLE : RECIPIENT
SenderCity
Kolkata
New Delhi
Mumbai
VI. SELECT A.SenderName A, B.RecName FROM Sender A,
Recipient B WHERE A.SenderID=B. SenderID AND
B.RecCity=’Mumbai’;
SenderName RecName
R.Jain H. Singh
S Jha P.K. Swamy
RecName RecAddressS
Mahajan 116, A Vihar
S Tripati 13, B1 D, Mayur Vihar
VIII. SELECT RecID, RecName FROM Recipient WHERE
SenderID = ‘MU02’ OR SenderID = ‘ND50’;
RecID RecName
ND08 S Mahajan
ND48 S Tripat
26. Consider the following tables FURNITURE and ARRIVALS: write SQL
commands for the questions (a) to (f) and give outputs for SQL queries (i)
to (iv) :
TABLE : FURNITURE
NO ITEMNAME TYPE DATEOFSTOCK PRICE DISCOUNT
1 White lotus Double Bed 23/02/02 30000 25
2 Pink feather Baby cot 20/01/02 7000 20
3 Dolphin Baby cot 19/02/02 9500 20
4 Decent Office Table 01/01/02 25000 30
5 Comfort zone Double Bed 12/01/02 25000 25
6 Donald Baby cot 24/02/02 6500 15
7 Royal Finish Office Table 20/02/02 18000 30
8 Royal tiger Sofa 22/02/02 31000 30
9 Econo sitting Sofa 13/12/01 9500 25
10 Eating Paradise Dining 19/02/02 11500 25
Table
TABLE : ARRIVALS
NO ITEMNAME TYPE DATEOFSTOCK PRICE DISCOUNT
11 Wood Comfort Double Bed 23/03/03 25000 25
12 Old Fox Sofa 20/02/03 17000 20
13 Micky Baby cot 21/02/03 7500 15
18.3333
27. Study the following tables DOCTOR and SALARY and write SQL
commands for the questions (i) to (iv) and give outputs for SQL queries
(v) to (vi) :
TABLE : DOCTOR
ID NAME DEPT SEX EXPERIENCE
101 John ENT M 12
104 Smith ORTHOPEDIC M 5
107 George CARDIOLOGY M 10
114 Lara SKIN F 3
109 K George MEDICINE F 9
105 Johnson ORTHOPEDIC M 10
117 Lucy ENT F 3
111 Bill MEDICINE F 12
130 Morphy ORTHOPEDIC M 15
TABLE : SALARY
ID BASIC ALLOWANCE CONSULTATION
101 12000 1000 300
104 23000 2300 500
107 32000 4000 500
114 12000 5200 100
109 42000 1700 200
105 18900 1690 300
130 21700 2600 300
A.Write SQL commands for the following statements:
(i) Display NAME of all doctors who are in “MEDICINE” having more
than 10 year experience from the table DOCTOR.
(iv) Display the highest consultation fee among all male doctor