Manu Impro Cs File Python
Manu Impro Cs File Python
if num > 1:
if (num % i) == 0:
break
else:
else:
3.Write a UDF in python, it will take two arguments list(sequence of elements) and
finding element . Function search and return 1 if element is present in the given list
otherwise return -1 . Using Binary search.
def fun(L):
num=int(input(“Enter number to search”))
for i in L:
if i==num:
print(num,1)
else:
print(-1)
n=int(input(“enter how many numbers you want to add ”)
L=[]
for i in range(0,n):
s=int(input(“Enter number”))
l.append(s)
fun(L)
4.Write a UDF in python,it will take search me and my words in the text file
DAIRY.TXT.
def fun():
f=open("DAIRY.TXT","r")
s=f.read()
a=0
l=s.split()
for i in l:
if i=="me" or i=="my" or i=="MY" or i=="ME":
a=a+1
print("the numbers of such words are",a)
fun()
5. Write the program in python to read contents of a text file “Places.TXT”and
display all those elements lines on screen which are either starting with “P” or
“S”.
def fun1():
f=open("PLACES.TXT","")
l=f.readlines()
for i in l:
if i[0] in ("P","p","s","S"):
print(i)
f.close()
fun1()
6. Write a User defined function that will read each character in file “IMP.TXT”
should count and display the number of “E”and “U” words present in text file.
def EUcounter():
f=open("IMP.TXT","r")
s=f.read()
a=s.count("E")
b=s.count("e")
c=s.count("u")
d=s.count("U")
print("The numbers of E are",a+b)
print("The numbers of U are",c+d)
f.close()
EUcounter()
7.Write the function in python to count the number of line ending with a
vowelfrom the text file “Story.TXT”.
def fun():
f=open("STORY.TXT","r")
l=f.readlines()
print(l)
a=0
for i in l:
if i[-2] in ("a","e","i","o","u"):
a=a+1
print("The numbers of lines are",a)
f .close()
fun()
8. Write the function copy that will copy all the words starting with uppercase
alphabets from file “FAIPS.TXT” to “DPS.txt”
def copy():
f=open("FAIPS.TXT","r")
p=[]
s=f.read()
l=s.split()
for i in l:
if i[0].isupper==True:
p.append(i)
print(p)
f.close()
w=open("DPS.TXT","w")
w.writelines(p)
w.close()
copy()
def reading():
f=open(“BOOK.DAT”,”wb”)
try:
while True:
rec=pickle.load(f)
if rec[0]==num:
print(rec)
except EOFError:
10. Write a menu-driven program to perform all the basic operations using dictionary
on student binary file such as inserting, reading, updating, searching and deleting a
record
def insertRec():
rec = {'Rollno':rollno,'Name':name,'Marks':marks}
f = open('d:/student.dat','ab')
pickle.dump(rec,f)
f.close()
def readRec():
f = open('d:/student.dat','rb')
while True:
try:
rec = pickle.load(f)
print('Roll Num:',rec['Rollno'])
print('Name:',rec['Name'])
print('Marks:',rec['Marks'])
except EOFError:
break
f.close()
def searchRollNo(r):
f = open('d:/student.dat','rb')
flag = False
while True:
try:
rec = pickle.load(f)
if rec['Rollno'] == r:
print('Roll Num:',rec['Rollno'])
print('Name:',rec['Name'])
print('Marks:',rec['Marks'])
flag = True
except EOFError:
break
if flag == False:
def updateMarks(r,m):
f = open('d:/student.dat','rb')
reclst = []
while True:
try:
rec = pickle.load(f)
reclst.append(rec)
except EOFError:
break
f.close()
if reclst[i]['Rollno']==r:
reclst[i]['Marks'] = m
f = open('d:/student.dat','wb')
for x in reclst:
pickle.dump(x,f)
f.close()
def deleteRec(r):
f = open('d:/student.dat','rb')
reclst = []
while True:
try:
rec = pickle.load(f)
reclst.append(rec)
except EOFError:
break
f.close()
f = open('d:/student.dat','wb')
for x in reclst:
if x['Rollno']==r:
continue
pickle.dump(x,f)
f.close()
while 1==1:
if choice==0:
break
if choice == 1:
insertRec()
if choice == 2:
readRec()
if choice == 3:
searchRollNo(r)
if choice == 4:
r = int(input('Enter a rollno:'))
updateMarks(r,m)
if choice == 5:
r = int(input('Enter a rollno:'))
deleteRec(r)
11.Write a function in PYTHON to search for a BookNo from a binary file “BOOK.DAT”,
assuming the binary file is containing the records of the following type:
{"BookNo":value, "Book_name":value}. Assume that BookNo is an integer.
import pickle as p
def fun():
f=open("BOOK.DAT", "rb")
while True:
try:
rec=p.load(f)
for i in rec:
if i[0]==No:
print(i)
except:
break
f.close()
fun()
(b) Write all the records in one single line and go to csv.
import csv
def writing():
f=open("Student.csv","w")
obj=csv.writer(f)
obj.writerow("line")
record=[]
record.append(rec)
if choice=="No":
break
obj.writerows(record)
f.close()
def writing2():
f=open("Student.csv","w")
obj=csv.writer(f)
obj.writerow("admn","name")
record=[]
record.append(rec)
if choice=="No":
break
obj.writerows(record)
f.close()
def reading():
f=open("student.csv","r")
l=csv.reader(f)
for i in l:
print(i)
f.close()
if choice==1:
writing()
if choice==2:
writing1():
if choice==3:
reading()
13. Create a CSV file by entering user-id and password, read and search the password
for given userid
import csv
al=["id", "password"]
myfilel=csv.writer (myfile)
myfilel.writerow(al)
myfilel.writerows (a)
myfile.seek(0)
myfile2=csv.reader (myfile)
for i in myfile2:
if i[0]==b:
myfile.close()
14. Write a function in python for Push (list) and Pop(List) for performing Push
and Pop operations with a stack.
def push():
s=[]
s.append(a)
def pop():
s.pop()
pop()
15. Write a program to create a stack called student, to perform the basic operations
on stack using list. The list contains two data values called roll number and name of
student. Notice that the data manipulation operations are performed as per the rules
of a stack. Write the following functions :
• PUSH ( ) - To push the data values (roll no. and
name) into the stack.
• POP() - To remove the data values from the stack.
• SHOW() - To display the data values from the stack.
Push ()
Pop()
Show()
def push():
s=[]
s.append(a)
def pop():
s.pop()
pop()
ABC Infotech Pvt. Ltd. needs to store, retrieve and delete
the records of its employees. Develop an interface that
provides front-end interaction through Python, and
stores
and updates records using MySQL.
16.
(a) Write a UDF to create a table ‘EMPLOYEE’ through
MySQL- Python Connectivity.
(b) Write a UDF to add/insert employee data into the
‘EMPLOYEE’ table through MySQL-Python
Connectivity.
(c) Write a UDF to fetch and display all the records from
EMPLOYEE table .
(d) Write a UDF to update the records of employees by
increasing salary by 1000 of all those employees who
are getting less than 80000.
(e) Write a UDF to delete the record on the basis of
Employee eno.
Write a menu driven program to perform all operations.
def createTable():
db=con.connect(host='localhost',user="root",password="",database="emp")
cur=db.cursor()
db.commit()
db.close()
def insert():
db=con.connect(host='localhost',user="root",password="",database="emp")
cur=db.cursor()
db.commit()
db.close()
def display():
db=con.connect(host='localhost',user="root",password="",database="emp")
cur=db.cursor()
r=cur.fetchall()
for i in r:
print(i)
print()
db.commit()
db.close()
def update():
db=con.connect(host='localhost',user="root",password="",database="emp")
cur=db.cursor()
db.commit()
db.close()
def delete():
db=con.connect(host='localhost',user="root",password="",database="emp")
cur=db.cursor()
db.commit()
db.close()