Practical Programs: Name - Yakesh Balaji Raja.P
Practical Programs: Name - Yakesh Balaji Raja.P
Practical Programs
Ex.no-1
Aim:
To write an menu driven program to apply linear search and binary search for a list
of string using functions.
Program:
def binary():
r='false'
a.sort()
if str(a[i]) == str(val):
r='true'
for j in range(((len(a)//2)-1),len(a)):
if str(a[j]) == str(val):
r='true'
if r == 'true':
elif r=='false':
r1='false'
for i in a:
if str(i) == str(val) :
r1='true'
if r1=='true':
elif r1=='false':
a=l.split(',')
choise=input("Which type of search you want to use \n(a) Binary or (b) Linear:")
if choise == 'a':
binary()
linear()
Output:
Ex.no-2
Aim:
Program:
def bubble_sort(List):
for i in range(len(List)):
for j in range(len(List)-i-1):
List=[]
details=list(details.split(","))
List.append(details)
if cont == "y":
print(List)
continue
else:
bubble_sort(List)
print(List)
break
Output:
Ex.no-3
Aim:
To write a python program which has the function to sort the detail of the product
in ascending order of their price using insertion sort. The nested list contain the details of
the product such as Pid, Pname, Qty and Price.
Program:
def insertion_sort(List):
for i in List:
j=List.index(i)
while j>0:
else:
break
j=j-1
repeat=1
List=[]
details=list(details.split(","))
List.append(details)
if cont == "y":
continue
else:
insertion_sort(List)
break
Output:
Ex.no-4
Aim:
To Write a python program using functions to create a text file “Story.txt”, read lines
from the text file “Story.txt” and display those words whose length is less than 4 characters.
Program:
with open("story.txt") as f:
d=f.readlines()
a=''.join(d)
b=a.split('\n')
for i in b:
c=i.split(' ')
for j in range(len(c)):
if len(c[j]) < 4:
print(c[j])
Output:
Ex.no-5
Aim:
To write a python program using function to count the number of words starting
with the vowel in the file “Book.txt”.
Program:
def find():
count=0
a=['a','e','i','o','u']
for i in d:
if i[0] in a:
count+=1
print(‘Number of words starting with vowel is:’,count)
with open('Books.txt') as f:
d=f.readlines()
find()
Output:
Ex.no-6
Aim:
To write a python program which has the function to read from the file “Exam.txt”
and display all the lines which ends with the word “health”.
Program:
def count():
for i in d:
a=''.join(i).split(' ')
if a[-1] == 'healthy':
print(i)
with open('Exam.txt') as f:
d=''.join(f.readlines()).split('\n')
count()
Output:
Ex.no-7
Aim:
To write the car details, and read display the Toyota cars with price.
Program:
import pickle
def read():
with open("Cars.dat",'rb') as p:
d=pickle.load(p)
for i in d:
if i[1] == 'Toyato':
while 1>0:
a=[]
b=no,name,mil,price
a.append(b)
pickle.dump(a,f)
choise=str(input('Do you want to continue or not(y/n):'))
if choise == 'y':
continue
read()
Output:
Ex.no-8
Aim:
To update the megapixel in the binary file by accepting the model number from
user.
Program:
import pickle
def show():
with open('mobile.dat','rb') as f:
dp=pickle.load(f)
print('Mobile records',dp)
global d
d=[]
d.append(dp)
def update():
with open('mobile.dat','wb') as k:
ans='false'
for i in range(len(d[0])):
if str(d[0][i][0]) == str(number):
d[0][i][2]=mp
ans='true'
print('Record updated')
if ans == 'false':
pickle.dump(d,k)
while 1>0:
choise=str(input('What do you want to do:\n(a)View records\n(b)Update
record\n>>>'))
if choise == 'a':
show()
update()
Output:
Ex.no-9
Aim:
Program:
import pickle
def write():
d=[]
with open('telephone.dat','wb') as f:
d.append([name,address,areacode,ph_no])
pickle.dump(d,f)
def delete():
d=[]
with open('telephone.dat','rb') as k:
d=pickle.load(k)
with open('telephone.dat','wb') as h:
found=False
val ='TP101'
d1=[]
for i in range(len(d)):
if d[i][2] != val:
d1.append(d[i])
pickle.dump(d1,h)
def read():
d=[]
with open('telephone.dat','rb') as k:
ans='y'
while True:
try:
d=pickle.load(k)
except EOFError:
break
for i in d:
print(i)
l=len(d)
while 1>0:
if choise == 'a':
write()
delete()
read()
Output:
Ex.no-10
Aim:
To write the details of students in csv file and display the students whose average is
above 85.
Program:
import csv
r=True
def func():
with open("students.csv",'w',newline='') as f:
a=csv.writer(f)
a.writerow(val)
with open("students.csv") as f:
d=csv.reader(f)
for j in d:
v.append(j)
v=[]
while r is True:
avg=input("Average:")
val=[rno,name,marks,avg]
func()
if choise == 'y':
continue
else:
break
Output:
Ex.no-11
Aim:
To write csv file and display the name and salary of the manager who belong to sales
department.
Program:
import csv
r=True
def func():
with open("emp.csv",'w',newline='') as f:
a=csv.writer(f)
a.writerow(val)
with open("emp.csv") as f:
d=csv.reader(f)
for j in d:
if j[3] == 'sales':
v.append([j[1],j[4]])
v=[]
while r is True:
val=[eno,name,des,dep,sal]
func()
if choise == 'y':
continue
else:
Output:
Ex.no-12
Display sum of boundary, non-boundary, right and left diagonal of a nested list
Aim:
To write a python program to find the sum of the boundary, non-boundary, left and
right diagonal of a nested list A of size MxN.
Program:
def boundary():
Sum=0
for j in l[0]:
Sum=Sum+int(j)
for k in l[-1]:
Sum=Sum+int(k)
for m in l[1:-1] :
Sum=Sum+int(m[0]) +int(m[-1])
return Sum
def non_boundary():
Sum=0
for i in l[1:-1]:
for j in i[1:-1]:
Sum=Sum+int(j)
return Sum
def right_diagonal():
Sum=0
ans='y'
for i in l:
if len(i) != no:
ans='n'
break
if ans=='y':
p=-1
for i in l:
Sum=Sum+int(i[p])
p=p-1
return Sum
def left_diagonal():
Sum=0
ans='y'
for i in l:
if len(i) != no:
ans='n'
break
if ans =='y':
p=0
for i in l:
Sum=Sum+int(i[p])
p=p+1
if p > len(i):
break
return Sum
l=[]
for i in range(no):
l.append(a.split(','))
b=boundary()
nb=non_boundary()
rd=right_diagonal()
ld=left_diagonal()
print('Sum of Boundary is',b)
Output:
Ex.no-13
Aim:
To swap the elements in the list and to count the number of palindrome in the list.
Program:
def swap():
c=[]
b=len(a)
for j in range(b//2,b):
c.append(a[j])
for i in range(b//2):
c.append(a[i])
def pal():
count=0
for i in a:
if i == i[::-1]:
count+=1
a=l.split(',')
while 1>0:
if choice == 'a':
swap()
pal()
Output:
Ex.no-14
Stack
Aim:
To write a python program to do stack operations from the stack customers which
contains customer id,name,phone no.
Program:
def dopush():
cid=str(input('Cutomer ID:'))
name=str(input('Name:'))
no=str(input('Phone no:'))
b=cid,name,no
a.append(b)
print(a)
def dopop():
if a == []:
print("Stack is empty")
else:
a.pop()
def doshow():
if a == []:
else:
for i in a:
print('\nCustomer ID:',i[0])
print('Name:',i[1])
print('Phone no:',i[2])
a=[]
while 1>0:
if choise == 'a':
dopush()
dopop()
doshow()
Output:
Ex.no-15
Queue
Aim:
To write a python program to add, delete, view from a queue Pasanger name, ticket-
no.
Program:
def InsQueue():
name=str(input('\nName:'))
ticketno=str(input('Ticket no:'))
b=name,ticketno
a.append(b)
def DelQueue():
if a == []:
print('Queue is empty')
else:
a.pop(0)
def DispQueue():
if a == []:
print('Queue is empty')
else:
for i in a:
print('Name :',i[0])
print('Ticket no:',i[1])
a=[]
while 1>0:
if choise == 'a':
InsQueue()
DelQueue()
DispQueue()
Output: