Computer Practical File-Siddhartha Patwal 12 F
Computer Practical File-Siddhartha Patwal 12 F
BY : SIDDHARTHA PATWAL
XII F
ROLL NO - 30
INDEX
SNO. PROGRAM
1 Write the definition of a function Alter(A, N) in python, which should change all the multiples of 5 in
the list to 5 and rest of the elements as 0.
2 Write a code in python for a function Convert ( T, N) , which repositions all the elements of array by
shifting each of them to next position and shifting first element to last position.
3 Create a function showEmployee() in such a way that it should accept employee
name, and it’s salary and display both, and if the salary is missing in
function call it should show it as 9000.
4 WAP to find no of lines starting with F in firewall.txt.
5 WAP to find how many 'f' and 's' present in a text file.
6 WAP to find how many 'firewall' or 'to' present in a file firewall.txt
INDEX
SNO. PROGRAM
7 Write a definition for function Itemadd () to insert record into the binary file ITEMS.DAT, (items.dat-
id,gift,cost). info should stored in the form of list.
8 Write a definition for function SHOWINFO() to read each record of a binary file
ITEMS.DAT, (items.dat- id,gift,cost).Assume that info is stored in the form of list
9 Write a definition for function COSTLY() to read each record of a binary file
ITEMS.DAT, find and display those items, which are priced less than 50.
(items.dat- id,gift,cost).Assume that info is stored in the form of list.
10 A csv file countries.csv contains data in the following order:
country,capital,code sample of counties.csv is given below:
india,newdelhi,ii ;
us,washington,uu ;
malaysia,ualaumpur,mm ;
france,paris,ff .Write a python function to read the file counties.csv and display the names of all those
countries whose no of characters in the capital are more than 6.
INDEX
SNO. PROGRAM
11 Write a python function to search and display the total cost of all products
from the file PRODUCT.CSV. Sample of product.csv is given below: pid,pname,cost,quantity;
p1,brush,50,200;
p2,toothbrush,120,150;
p3,comb,40,300;
p4,sheets,100,500;
p5,pen,10,250
12 Write a python function readcsv() to display the following information into product.csv.
pid,pname,cost,quantity;
p1,brush,50,200;
p2,toothbrush,120,150;
p3,comb,40,300;
p4,sheets,100,500;
p5,pen,10,250;
INDEX
SNO. PROGRAM
13 Write a program that reads character from the keyboard one by one. All lower case characters get store
inside the file LOWER, all upper case characters get stored inside the file UPPER and all other
characters get stored inside OTHERS.
14 Write the definition of a member function PUSH() in python to add a new book in a dynamic stack of
BOOKS considering the following code is already included in the Program: ISBN, TITLE.
15 :Write a function in python to delete a node containing book’s information, from a dynamically allocated
stack of books implemented with the help of the following structure: BNo,BName.
16 A linear stack called "List" contain the following information:
a. age of student;
b. Name of the student. Write enqueue() and dequeue() methods in python to add and remove from the
queue.
SNO. PROGRAM
17 Write SQL commands for (b) to (e) and write the outputs for (f) on the basis of table GRADUATE.
18 Write SQL commands for (a) to (f) and write the outputs for (g) on the basis of table HOSPITAL.
19 Write a MySQL-Python connectivity code display ename, empno, designation, sal of those employees
whose salary is more than 3000 from the table emp. Name of the database is “Emgt”.
20 Write a MySQL-Python connectivity code to increase the salary (sal) by 300 of those employees whose
designation (job) is clerk from the table emp. Name of the database is “Emgt”.
Q1 : Write the definition of a function Alter(A, N) in python, which should change all the
multiples of 5 in the list to 5 and rest of the elements as 0.
SOLUTION OUTPUT
def Alter ( A, N): Original list [10, 14, 15, 21]
for i in range(N): LIst after Alteration [5, 0, 5, 0]
if(A[i]%5==0):
A[i]=5
else:
A[i]=0
print("LIst after Alteration", A)
d=[10,14,15,21]
print("Original list",d)
r=len(d)
Alter(d,r)
Q2 : Write a code in python for a function void Convert ( T, N) , which repositions all the
elements of array by shifting each of them to next position and shifting last element to first
position.
SOLUTION OUTPUT
def Convert ( T, N): original list [10, 14, 11, 21]
for i in range(N): LIst after conversion [21, 10, 14, 11]
t=T[N-1]
T[N-1]=T[i]
T[i]=t
print("LIst after conversion", T)
d=[10,14,11,21]
print("original list",d)
r=len(d)
Convert(d,r)
Q3: Create a function showEmployee() in such a way that it should accept employee name, and
it’s salary and display both, and if the salary is missing in
function call it should show it as 9000.
SOLUTION OUTPUT
def showEmployee(name,salary=9000): enter employee namejohn miller
print("employee name",name) enter employee's salary6700
print("salary of employee",salary) employee name john miller
n=input("enter employee name") salary of employee 6700
#s=eval(input("enter employee's salary")) enter employee namesamantha
#showEmployee(n,s) employee name samantha
showEmployee(n) salary of employee 9000
Q4: WAP to find no of lines starting with F in firewall.txt
SOLUTION OUTPUT
f=open(r"C:\Users\hp\Desktop\cs\networking\ 1
firewall.txt")
c=0
for i in f.readline():
if(i=='F'):
c=c+1
print(c)
Q5: WAP to find how many 'f' and 's' present in a text file
SOLUTION OUTPUT
f=open(r"C:\Users\user\Desktop\cs\networking\ 18 41
firewall.txt")
t=f.read()
c=0
d=0
for i in t:
if(i=='f'):
c=c+1
elif(i=='s'):
d=d+1
print(c,d)
Q6: WAP to find how many 'firewall' or 'to' present in a file firewall.txt
SOLUTION OUTPUT
f=open(r"C:\Users\user\Desktop\cs\networking\ 9
firewall.txt")
t=f.read()
c=0
for i in t.split():
if(i=='firewall')or (i=='is'):
c=c+1
print(c)
Q7: Write a definition for function Itemadd () to insert record into the binary file
ITEMS.DAT, (items.dat- id,gift,cost). info should stored in the form of list.
SOLUTION OUTPUT
import pickle enter how many records2
def itemadd (): enter id1
f=open("items.dat","wb") enter gift namepencil
n=int(input("enter how many records"))
enter cost45
for i in range(n):
record added
r=int(input('enter id'))
enter id2
n=input("enter gift name")
enter gift namepen
p=float(input("enter cost"))
v=[r,n,p] enter cost120
pickle.dump(v,f) record added
print("record added")
f.close()
itemadd() #function calling
Q8: Write a definition for function SHOWINFO() to read each record of a binary file
ITEMS.DAT, (items.dat- id,gift,cost).Assume that info is stored in the form of list
SOLUTION OUTPUT
import pickle [1, 'pencil', 45.0]
def SHOWINFO(): [2, 'pen', 120.0]
f=open("items.dat","rb")
while True:
try:
g=pickle.load(f)
print(g)
except:
break
f.close()
SHOWINFO()#function calling
Q9: Write a definition for function COSTLY() to read each record of a binary file
ITEMS.DAT, find and display those items, which are priced less than 50.
(items.dat- id,gift,cost).Assume that info is stored in the form of list.
SOLUTION OUTPUT
import pickle [2, 'pen', 120.0]
def COSTLY():
f=open("items.dat","rb")
while True:
try:
g=pickle.load(f)
if(g[2]>50):
print(g)
except:
break
f.close()
COSTLY() #function calling
Q10: A csv file countries.csv contains data in the following order:
country,capital,code sample of counties.csv is given below: india,newdelhi,ii ;
us,washington,uu ; malaysia,ualaumpur,mm ; france,paris,ff .Write a python function to read
the file counties.csv and display the names of all those countries whose no of characters in the
capital are more than 6.
SOLUTION
Import csv def searchcsv():
f=open("countries.csv","r")
def writecsv():
r=csv.reader(f)
f=open("countRiesrcsv","w") f=0
r=csv.writer(f,lineterminator='\n') for i in r:
r.writerow(['france','paris','ff']) OUTPUT
india
writecsv()
us
searchcsv() malaysia
Q11: Write a python function to search and display the total cost of all products
from the file PRODUCT.CSV. Sample of product.csv is given below: pid,pname,cost,quantity;
p1,brush,50,200; p2,toothbrush,120,150; p3,comb,40,300; p4,sheets,100,500;p5,pen,10,250
SOLUTION OUTPUT
import csv Total cost is 92500
def searchcsv():
f=open("product.csv","r")
r=csv.reader(f)
next(r)
s=0
for i in r:
s=s+int(i[2])*int(i[3])
print("total cost is",s)
Q12: Write a python function readcsv () to display the following information into
product.csv. Assume that following info is already present in the file. pid,pname,cost,quantity;
p1,brush,50,200; p2,toothbrush,120,150; p3,comb,40,300;
p4,sheets,100,500; p5,pen,10,250;
SOLUTION
import csv pid,pname,cost,quantity
def readcsv(): p1,brush,50,200
f=open("product.csv","r") p2,toothbrush,120,150
r=csv.reader(f) p3,comb,40,300
for i in r: p4,sheets,100,500
print(i) p5,pen,10,250
f.close()
readcsv()
Q13: Write a program that reads character from the keyboard one by one. All lower case
characters get store inside the file LOWER, all upper case characters get stored inside the file
UPPER and all other characters get stored inside OTHERS.
f=open(r"C:\Users\user\Desktop\cs\networking\firewall.txt")
f1=open("lower.txt","a")
f2=open("upper.txt","a")
f3=open("others.txt","a")
r=f.read()
for i in r:
if(i>='a' and i<='z'):
f1.write(i)
elif(i>='A' and i<='Z'):
f2.write(i)
else:
f3.write(i)
f.close()
f1.close()
f2.close()
f3.close()
Q14: Write the definition of a member function PUSH() in python to add a new book in a
dynamic stack of BOOKS considering the following code is already included in the Program:
ISBN, TITLE.
SOLUTION OUTPUT
s=[]
enter ISBN no 2
def push():
a=int(input("enter ISBN no")) enter title Encyclopedia
t=input("enter title")
[2, ' Encyclopedia’] ---top
l=[a,t]
s.append(l)
def disp(s):
if(s==[]):
print("list is empty")
else:
top=len(s)-1
print(s[top],"---top")
for i in range(top-1,-1,-1):
print(s[i])
push()
disp(s)
Q15: Write a function in python to delete a node containing book’s information, from a
dynamically allocated stack of books implemented with the help of the following structure:
BNo,BName.
SOLUTION OUTPUT
s=[]
stack is empty
def pop():
if(s==[]): list is empty
print("stack is empty")
else:
Book_no,Book_title=s.pop()
print("element deleted")
def disp(s):
if(s==[]):
print("list is empty")
else:
top=len(s)-1
print(s[top],"---top")
for i in range(top-1,-1,-1):
print(s[i])
pop()
disp(s)
Q16: A linear stack called "List" contain the following information: a. age of student;
b. Name of the student. Write enqueue() and dequeue() methods in python to add and remove
from the queue.
def enqueue():
age=int(input("enter age"))
name=input("enter ur name")
l=[age,name]
q.append(l)
def dequeue():
if(q==[]):
print("queue is empty")
else:
age,name=q.pop(0)
print("element deleted")
def disp():
if(q==[]): OUTPUT
print("queue is empty") enter age12
else:
enter ur namereena
f=0
enter age34
r=len(q)-1
print(q[f],"---front")
enter ur nameteena
for i in range(1,r): enter age16
print(q[i]) enter ur nameheena
print(q[r],"---rear") [12, 'reena'] ---front
[34, 'teena']
enqueue()
enqueue() [16, 'heena'] ---rear
enqueue() element deleted
disp() [34, 'teena'] ---front
dequeue()
[16, 'heena'] ---rear
disp()
Q17: Write SQL commands for (b) to (e) and write the outputs for (f) on the basis of table
GRADUATE.
f) What will be the output of the following query:
SELECT NAME, ADVISOR FROM GRADUATE, GUIDE WHERE SUBJECT = MAINAREA
Q18: Write SQL commands for (a) to (f) and write the outputs for (g) on the basis of table
HOSPITAL.
Q19: Write a MySQL-Python connectivity code display ename, empno, designation, sal of those
employees whose salary is more than 3000 from the table emp. Name of the database is
“Emgt”.
import mysql.connector as m
db=m.connect(host="localhost",user="root",passwd="1234",database="Emgt")
c=db.cursor()
c.execute("select * from emp where sal>3000")
r=c.fetchall()
for i in r:
print(i)
Q20: Write a MySQL-Python connectivity code to increase the salary (sal) by 300 of those
employees whose designation (job) is clerk from the table emp. Name of the database is
“Emgt”.
import mysql.connector as m
db=m.connect(host="localhost",user="root",passwd="1234",database="Emgt")
c=db.cursor()
c.execute("update emp set sal=sal+300 where job=”clerk”)
db.commit()