Cs2study



COMPUTER SCIENCE WITH PYTHONPROGRAM FILENAME:CLASS: SECTION:INDEXS.NOTOPICT.SIGN1Write 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.2Write 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.3Create 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 90004Write a program using function which accept two integers as an argument and return its sum. Call this function and print the results in main( )5Write 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.6Write 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 list7Write 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 list8A csv file counties.csv contains data in the following order:country,capital,codesample of counties.csv is given below:india,newdelhi,iius,washington,uumalaysia,ualaumpur,mmfrance,paris,ffwrite a python function to read the file counties.csv and display the names of all thosecountries whose no of characters in the capital are more than 6.9write 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,quantityp1,brush,50,200p2,toothbrush,120,150p3,comb,40,300p4,sheets,100,500p5,pen,10,25010write 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 headingssample of product.csv is given below:pid,pname,cost,quantityp1,brush,50,200p2,toothbrush,120,150p3,comb,40,300p4,sheets,100,500p5,pen,10,25011WAP to find no of lines starting with F in firewall.txt12WAP to find how many 'f' and 's' present in a text file 13Write 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.14WAP to find how many 'firewall' or 'to' present in a file firewall.txt15A linear stack called "List" contain the following information:?? ??? ?? ? a.?Roll Number of student?? ??? ?? ? b.?Name of studentWrite add(List) and pop(List) methods in python to add and remove from the stack.16A linear stack called "List" contain the following information: a. Roll of student b. Name of the studentWrite enqueue() and dequeue() methods in python to add and remove from the queue.17Consider the tables GARMENT and FABRIC, Write SQL commands for the statements (i) to (iv) 18Write SQL commands for (a) to (f) on the basis of Teacher relation 19Write 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”20Write 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”Q1Write 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.#soldef Alter ( A, N): for i in range(N): 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)'''OUTPUTOriginal 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 is012310141121The changed array content will be:012321101411sol: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]Q3Create a function showEmployee() in such a way that it should accept employeename, and it’s salary and display both, and if the salary is missing infunction call it should show it as 9000'''def showEmployee(name,salary=9000): print("employee name",name) print("salary of employee",salary)n=input("enter employee name")#s=eval(input("enter employee's salary"))#showEmployee(n,s)showEmployee(n)'''OUTPUTenter employee namejohn millerenter employee's salary6700employee name john millersalary of employee 6700enter employee namesamanthaemployee name samanthasalary of employee 9000'''Q4Write a program using function which accept two integers as an argument andreturn its sum.Call this function and print the results in main( )def fun(a,b): return a+ba=int(input("enter no1: "))b=int(input("enter no2: "))print("sum of 2 nos is",fun(a,b))'''OUTPUTenter no1: 34enter no2: 43sum 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 pickledef 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()itemadd() #function calling'''outputenter how many records2enter id1enter gift namepencilenter cost45record addedenter id2enter gift namepenenter cost120record added'''Q6Write a definition for function SHOWINFO() to read each record of a binary fileITEMS.DAT, (items.dat- id,gift,cost).Assume that info is stored in the form of list#Sol:import pickledef 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 fileITEMS.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#solimport pickledef 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'''output[2, 'pen', 120.0]'''Q8A csv file counties.csv contains data in the following order:country,capital,codesample of counties.csv is given below:india,newdelhi,iius,washington,uumalaysia,ualaumpur,mmfrance,paris,ffwrite a python function to read the file counties.csv and display the names of all thosecountries whose no of characters in the capital are more than 6.import csvdef writecsv(): f=open("counties.csv","w") r=csv.writer(f,lineterminator='\n') r.writerow(['country','capital','code']) r.writerow(['india','newdelhi','ii']) r.writerow(['us','washington','uu']) r.writerow(['malysia','kualaumpur','mm']) r.writerow(['france','paris','ff'])def searchcsv(): f=open("counties.csv","r") r=csv.reader(f) f=0 for i in r: if (len(i[1])>6): print(i[0],i[1]) f+=1 if(f==0): print("record not found")writecsv()searchcsv()'''outputindiausmalaysia'''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,quantityp1,brush,50,200p2,toothbrush,120,150p3,comb,40,300p4,sheets,100,500p5,pen,10,250#solution---------------------------------------------import csvdef 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']'''Q10write 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,quantityp1,brush,50,200p2,toothbrush,120,150p3,comb,40,300p4,sheets,100,500p5,pen,10,250#solution---------------------------------------------import csvdef 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.txtf=open(r"C:\Users\hp\Desktop\cs\networking\firewall.txt")c=0for i in f.readline(): if(i=='F'): c=c+1print(c)‘’’Output1‘’’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=0d=0for i in t: if(i=='f'): c=c+1 elif(i=='s'): d=d+1print(c,d)'''output18 41'''Q13Write a program that reads character from the keyboard one by one. All lower casecharacters get store inside the file LOWER, all upper case characters get stored insidethe 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.txtf=open(r"C:\Users\user\Desktop\cs\networking\firewall.txt")t=f.read()c=0for i in t.split(): if(i=='firewall')or (i=='is'): c=c+1print(c)'''output9'''Q15 A linear stack called "List" contain the following information: a. Roll Number of student b. Name of studentWrite add(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])#Call add and pop function to verify the codeadd(List)add(List)disp(List)pop(List)disp(List)'''outputEnter roll number1Enter namereenaEnter roll number2Enter nameteena[2, 'teena'] ---top[1, 'reena'][1, 'reena'] ---top'''Q16 A linear stack called "List" contain the following information: a. age of student b. Name of the studentWrite enqueue() and dequeue() methods in python to add and remove from the queue.q=[]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==[]): print("queue is empty") else: f=0 r=len(q)-1 print(q[f],"---front") for i in range(1,r): print(q[i]) print(q[r],"---rear")enqueue()enqueue()enqueue()disp()dequeue()disp()‘’’OUTPUTenter age12enter ur namereenaenter age34enter ur nameteenaenter age16enter ur nameheena[12, 'reena'] ---front[34, 'teena'][16, 'heena'] ---rearelement deleted[34, 'teena'] ---front[16, 'heena'] ---rear‘’’Consider the following table GARMENT and FABRIC, Write SQL commands for the statements (i) to (iv) -104776137795TABLE GARMENT-104776148590GCODEDESCRIPTIONPRICE FCODE READYDATE10023PENCIL SKIRT1150F 0319-DEC-0810001FORMAL SHIRT1250F 0112-JAN-0810012INFORMAL SHIRT1550F 0206-JUN-0810024BABY TOP 750F 0307-APR-0710090TULIP SKIRT 850F 0231-MAR-0710019EVENING GOWN 850F 0306-JUN-0810009INFORMAL PANT1500F 0220-OCT-0810007FORMAL PANT1350F 0109-MAR-0810020FROCK 850F 0409-SEP-07-1047751708150010089SLACKS 750F 0320-OCT-081666874145415TABLE FABRIC1666874137160FCODETYPEF 04POLYSTERF 02COTTONF 03SILKF01TERELENE16668752921000(i) To display GCODE and DESCRIPTION of each GARMENT in descending order of GCODE.(ii) To display the details of all the GARMENT, which have READYDATE in between 08-DEC-07 and 16-JUN-08 (inclusive if both the dates).(iii) To display the average PRICE of all the GARMENT, which are made up of fabric with FCODE as F03.(iv) To display fabric wise highest and lowest price of GARMENT from GARMENT table. (Display FCODE of each GARMENT along with highest and lowest Price).Ans .(i) SELECT GCODE, DESCRIPTION FROM GARMENT ORDER BY GCODE DESC;(ii) SELECT * FROM GARMENT WHERE READY DATE BETWEEN ’08-DEC-07’ AND ’16-JUN-08’;(iii) SELECT AVG (PRICE) FROM GARMENT WHERE FCODE = ‘F03’;(iv) SELECT FCODE, MAX (PRICE), MIN (PRICE) FROM GARMENT GROUP BY FCODE;Q18 Write SQL commands for (a) to (f) on the basis of Teacher relation given below: relation TeacherNo.NameAgeDepartmentDate of joinSalarySex1.Jugal34Computer10/01/9712000M2.Sharmila31History24/03/9820000F3.Sandeep32Maths12/12/9630000M4.Sangeeta35History01/07/9940000F5.Rakesh42Maths05/09/9725000M6.Shyam50History27/06/9830000M7.Shiv Om44Computer25/02/9721000M8.Shalakha33Maths31/07/9720000FTo show all information about the teacher of history departmentTo list the names of female teacher who are in Hindi departmentTo list names of all teachers with their date of joining in ascending order.To display student’s Name, Fee, Age for male teacher onlyTo count the number of teachers with Age>23.To inset a new row in the TEACHER table with the following data:9, “Raja”, 26, “Computer”, {13/05/95}, 2300, “M”Ans .(a)SELECT * FROM TeacherWHERE Department = “History”;(b)SELECT Name FROM TeacherWHERE Department = “Hindi” and Sex = “F”;(c)SELECT Name, DateofjoinFROM TeacherORDER BY Dateofjoin;(d)(The given query is wrong as no. information about students and fee etc. is available.The query should actually beTo display teacher’s Name, Salary, Age for male teacher only)SELECT Name, Salary, Age FROM TeacherWHERE Age > 23 AND Sex = ‘M’;(e)SELECT COUNT (*) FROM TeacherWHERE Age > 23;(f)INSERT INTO TeacherVALUES (9, “Raja”, 26, “Computer”, {13/05/95}, 2300, “M”);Q19Write 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 mdb=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 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 mdb=m.connect(host="localhost",user="root",passwd="1234",database="Emgt")c=db.cursor()c.execute("update emp set sal=sal+300 where job=”clerk”)mit() ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download

To fulfill the demand for quickly locating and searching documents.

It is intelligent file search solution for home and business.

Literature Lottery

Related searches