Computer science finalized
Computer science finalized
d=[10,14,15,21]
print("Original list",d)
r=len(d)
Alter(d,r)
'''
OUTPUT
Original list [10, 14, 15, 21]
LIst after Alteration [5, 0, 5, 0]
'''
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.
e.g. if the content of array is
0 1 2 3
10 14 11 21
The changed array content will be:
0 1 2 3
21 10 14 11
sol:
def Convert ( T, N):
for i in range(N):
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)
OUTPUT:
original list [10, 14, 11, 21]
LIst after conversion [21, 10, 14, 11]
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
'''
def showEmployee(name,salary=9000):
print("employee name",name)
print("salary of employee",salary)
'''
OUTPUT
enter employee namejohn miller
enter employee's salary6700
employee name john miller
salary of employee 6700
enter employee namesamantha
employee name samantha
salary of employee 9000
'''
Q4
Write a program using function which accept two integers as an argument and
return its sum.Call this function and print the results in main( )
def fun(a,b):
return a+b
'''
OUTPUT
enter no1: 34
enter no2: 43
sum of 2 nos is 77
'''
Q5
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.
import pickle
def itemadd ():
f=open("items.dat","wb")
n=int(input("enter how many records"))
for i in range(n):
r=int(input('enter id'))
n=input("enter gift name")
p=float(input("enter cost"))
v=[r,n,p]
pickle.dump(v,f)
print("record added")
f.close()
#Sol:
import pickle
def SHOWINFO():
f=open("items.dat","rb")
while True:
try:
g=pickle.load(f)
print(g)
except:
break
f.close()
SHOWINFO()#function calling
'''
output
[1, 'pencil', 45.0]
[2, 'pen', 120.0]
'''
Q7
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
#sol
import pickle
def COSTLY():
f=open("items.dat","rb")
while True:
try:
g=pickle.load(f)
if(g[2]>50):
print(g)
except:
break
f.close()
if(f==0):
print("record not found")
writecsv()
searchcsv()
'''
output
india
us
malaysia
'''
Q9
write a python function to find transfer only those records from the file product.csv to another
file "pro1.csv" whose quantity is more than 150. also include the first row with headings
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---------------------------------------------
import csv
def writecsv():
f=open("product.csv","w")
r=csv.writer(f,lineterminator='\n')
r.writerow(['pid','pname','cost','qty'])
r.writerow(['p1','brush','50','200'])
r.writerow(['p2','toothbrush','12','150'])
r.writerow(['p3','comb','40','300'])
r.writerow(['p5','pen','10','250'])
def searchcsv():
f=open("product.csv","r")
f1=open("pro1.csv","w")
r=csv.reader(f)
w=csv.writer(f1,lineterminator='\n')
g=next(r)
w.writerow(g)
for i in r:
if i[3]>'150':
w.writerow(i)
def readcsv():
f=open("pro1.csv","r")
r=csv.reader(f)
for i in r:
print(i)
writecsv()
searchcsv()
readcsv()
'''
output
['pid', 'pname', 'cost', 'qty']
['p1', 'brush', '50', '200']
['p3', 'comb', '40', '300']
['p5', 'pen', '10', '250']
'''
Q10
write a python function to search and display the record of that product from the file
PRODUCT.CSV which has maximum cost.
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---------------------------------------------
import csv
def writecsv():
f=open("product.csv","w")
r=csv.writer(f,lineterminator='\n')
r.writerow(['pid','pname','cost','qty'])
r.writerow(['p1','brush','50','200'])
r.writerow(['p2','toothbrush','120','150'])
r.writerow(['p3','comb','40','300'])
r.writerow(['p5','pen','10','250'])
def searchcsv():
f=open("product.csv","r")
r=csv.reader(f)
next(r)
m=-1
for i in r:
if (int(i[2])>m):
m=int(i[2])
d=i
print(d)
writecsv()
searchcsv()
'''
output
['p2', 'toothbrush', '120', '150']
'''
Q11
#WAP to find no of lines starting with F in firewall.txt
f=open(r"C:\Users\hp\Desktop\cs\networking\firewall.txt")
c=0
for i in f.readline():
if(i=='F'):
c=c+1
print(c)
‘’’
Output
1
‘’’
Q12
#WAP to find how many 'f' and 's' present in a text file
f=open(r"C:\Users\user\Desktop\cs\networking\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)
'''
output
18 41
'''
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
#WAP to find how many 'firewall' or 'to' present in a file firewall.txt
f=open(r"C:\Users\user\Desktop\cs\networking\firewall.txt")
t=f.read()
c=0
for i in t.split():
if(i=='firewall')or (i=='is'):
c=c+1
print(c)
'''
output
9
'''
Q15 A linear stack called "List" contain the following information:
a. Roll Number of student
b. Name of student
Write push(List) and POP(List) methods in python to add and remove from the stack.
#Ans.
List=[]
def add(List):
rno=int(input("Enter roll number"))
name=input("Enter name")
item=[rno,name]
List.append(item)
def pop(List):
if len(List)>0:
List.pop()
else:
print("Stack is empty")
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])
TABLE GARMENT
GCODE DESCRIPTION PRICE FCODE
READYDATE
10023 PENCIL SKIRT 1150 F 03 19-DEC-08
10001 FORMAL SHIRT 1250 F 01 12-JAN-08
10012 INFORMAL SHIRT 1550 F 02 06-JUN-08
10024 BABY TOP 750 F 03 07-APR-07
10090 TULIP SKIRT 850 F 02 31-MAR-07
10019 EVENING GOWN 850 F 03 06-JUN-08
10009 INFORMAL PANT 1500 F 02 20-OCT-08
10007 FORMAL PANT 1350 F 01 09-MAR-08
10020 FROCK 850 F 04 09-SEP-07
10089 SLACKS 750 F 03 20-OCT-08
TABLE FABRIC
FCODE TYPE
F 04 POLYSTER
F 02 COTTON
F 03 SILK
F01 TERELENE
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)
Q19.
Write a MySQL-Python connectivity code 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()