Record Program 1: PROGRAM TO TRAVERSE AND SEARCH PROBLEM DEFINITION: To w rite an interactive menu driven program in Python using functions to traverse and search for the student name from a list of students containing student roll number, name and average. Program: #Program to traverse and search for the student name from a list of students def Traverse(L,nametosearch): for i in L: if i[1]==nametosearch: print("Name Available") print("%5s"%i[0],"%10s"%i[1],"%20s"%i[2]) break else: print("Name not found in the List") print("Program to Traverse and Search Student Detail") n=int(input("Enter the Number of Students")) i=0 K=[] while i<n: sno=i nt(input("Enter the Student Number")) sname=input("Enter the Student Name") avg=int(input("Enter the Average")) K.append([sno,sname,avg]) i+=1 ans='y' while ans=='y': nametosearch=input("Enter the Name to be searched") print("The Given List is") print("%5s"%"Student Number","%10s"%"Student Name","%20s"%"Average") for i in K: print("%5s"%i[0],"%15s"%i[1],"%20s"%i[2]) Traverse(K,nametosearch) ans=input("Do You want to Continue the Search") Output: Program to Traverse and Search Student Detail Enter the Number of Students 5 Enter the Student Number 1 Enter the Student Name Arjun Enter the Average 67 Enter the Student Number 2 Enter the Student Name Ashok Enter the Average 56 Enter the Student Number 3 Enter the Student Name Balaguru Enter the Average 89 Enter the Student Number 4 Enter the Student Name Sekar Enter the Average 98 Enter the Student Number 5 Enter the Student Name Varun Enter the Average 87 Enter the Name to be searched Se kar The Given List is Student Number Student Name Average 1 Arjun 67 2 Ashok 56 3 Balaguru 89 4 Sekar 98 5 Varun 87 Name Available 4 Sekar 98 Do You want to Continue the Search y Enter the Name to be searched Tejas The Given List is Student Number Student Name Average 1 Arjun 67 2 Ashok 56 3 Balaguru 89 4 Sekar 98 5 Varun 87 Name not found in the List Do You want to Continue the Search n Record Program 2: PROGRAM FOR LIST MANIPULATION PROBLEM DEFINITION: To w rite an interactive Python program to do the following using functions: (i) Accept a list of elements and exchange the first half with the second half of the list. (ii) Accept a list of strings and display the number of palindrome words present in it. Program: #Program for List Manipulation def half_and_half(my_list): if len(my_list)%2 == 0: start = 0 else: start = 1 L = len(my_list)//2 for i in range(L): temp = my_list[i] my_list[i] = my_list[i+L+start] my_list[i+L+start] = temp rep='y' print("Program for List Manipulation") while rep=='y': print("1. Exchange of List Elements") print("2. Count the Number of Palindrome") ch=int(input("Enter the Choice")) if ch==1: my_list = list(eval(input("Enter the list of Numbers"))) half_and_half(my_list) print(my_list) else: count=0 listofstr=list(eval(input("Enter the List of Strings"))) print(listofstr) for i in listofstr: if i==i[:: - 1]: count+=1 print("The Number of Palindrome are",count) rep=input("Do you want to Continue") Output: Program for List Manip ulation 1. Exchange of List Elements 2. Count the Number of Palindrome Enter the Choice 1 Enter the list of Numbers [1,2,3,4,5] [4, 5, 3, 1, 2] Do you want to Continue y 1. Exchange of List Elements 2. Count the Number of Palindrome Enter the Choice 1 Enter the list of Numbers [1,2,3,4,5,6] [4, 5, 6, 1, 2, 3] Do you want to Continue y 1. Exchange of List Elements 2. Count the Number of Palindrome Enter the Choice 2 Enter the List of Strings ["mom","god","wow","is","great"] ['mom', 'god', 'wow', 'is', 'g reat'] The Number of Palindrome are 2 Do you want to Continue n Record Program 3: PROGRAM FOR MATRIX MANIPULATION PROBLEM DEFINITION: To Write an interactive Python program using functions to find the sum of boundary, non - boundary, left diagonal and right diagonal elements of a nested List A of size M x N. Program: #Program for Matrix Manipulation def bound_nonbound(L,m,n): sumb=sumnb=0 for i in range(m): for j in range(n): if i==0 or j==0 or i==m - 1 or j==n - 1: sumb+=L[i][j] if not(i==0 or j==0 or i==m - 1 or j==n - 1): sumnb+=L[i][j] return sumb,sumnb def left_right(L,m,n): suml=sumr=0 for i in range(m): for j in range(n): if i==j: suml+=L[i][j] if i+j==m - 1: sumr+=L[i][j] return suml,sumr K=[] print("Program for Matrix Manipulation") m=int(input("Enter the Number of Rows ")) n=int(input("Enter the Number of Columns")) for i in range(m): t=[] for j in range(n): x=int(input("Enter the Number")) t.append(x) K.append(t) print("The Given Matrix is ") for i in range(m): for j in range(n): print(K[i][j],end=" ") print() rep='y' while rep=='y': print("1. Finding the sum of Boundary and Non boundary elements") print("2. Finding the sum of Left and Right Diagonal elements") ch=int(input("Enter the Choice")) if ch==1: b=bound_nonbound(K,m,n) print("Sum of Boundary elements",b[0]) print("Sum of Non Boundary elements",b[1]) if ch==2: if m!=n: print("Sum of diagonal elements not possible") else: l=left_ right(K,m,n) print("Sum of left diagonal elements",l[0]) print("Sum of right diagonal elements",l[1]) rep=input("Do you want to Continue") Output: Program for Matrix Manipulation Enter the Number of Rows 3 Enter the Number of Columns 4 Enter the Number 1 Enter the Number 2 Enter the Number 3 Enter the Number 4 Enter the Number 5 Enter the Number 6 Enter the Number 7 Enter the Number 8 Enter the Number 9 Enter the Number 1 Enter the Number 2 Enter the Number 3 The Giv en Matrix is 1 2 3 4 5 6 7 8 9 1 2 3 1. Finding the sum of Boundary and Non boundary elements 2. Finding the sum of Left and Right Diagonal elements Enter the Choice 1 Sum of Boundary elements 38 Sum of Non Boundary elements 13 Do you want to Continue y 1. Finding the sum of Boundary and Non boundary elements 2. Finding the sum of Left and Right Diagonal elements Enter the Choice 2 Sum of diagonal elements not possible Do you want to Continue n Record Program 4: PROGRAM USING NESTED DICTIONARY PROBLEM DEFINITION: To Write an interactive menu driven program in python to accept the details of employees such as employee number, name, Basic ,HRA , DA from user and calculate annual salary of every employee with deductions like PF and to calculate the net salary(Basic+HRA +DA – PF)using dictionaries. Program: def calculate(d,n): for i in range(1,n+1): pf=(d[i]['basic']+d[i]['hra']+d[i]['da'])*0.1 net=d[i]['basic']+d[i]['hra']+d[i]['da'] - pf d[i]['pf']=pf d[i]['net']=net d[i][‘annual’]=net*12 d={} print(" \ tProgram using Nested Dictionary") n=int(input("Enter the No. of Employees")) for i in range(1, n+1): d1={} d1['name']=input("Enter the name") d1['basic']=int(input("Enter the basic pay")) d1['hra']=int(input("Enter the hra")) d1['da']=int(input("Enter the da")) d[i]=d1 calculate(d,n) print("The Payment details are") print("Name \ t \ t Basic \ t \ t HRA \ t \ t DA \ t \ t PF \ t \ t Netsalary \ t \ t”Annual Salary") for i in d: for j in d[i]: print(d[i][j]," \ t \ t", end='') print() Output: Program using Nested Dictionary Enter the No. of Employees3 Enter the nameARJUN Enter the basic pay26000 Enter the hra4500 Enter the da2300 Enter the nameBHAVIK Enter the basic pay32000 Enter the hra3400 Enter the da2300 Enter the nameCHINTU Enter the basic pay12000 Enter the hra1200 Enter the da700 The Payment details are Name Basic HRA DA PF Netsalary Annual Salary ARJUN 26000 4500 2300 3280.0 29520.0 354240.0 BHAVIK 32000 3400 2300 3770.0 33930.0 407160 .0 CHINTU 12000 1200 700 1390.0 12510.0 150120.0 Record Program No. 14: PROGRAM USING TEXT FILE - 1 PROBLEM DEFINITION: Write a Python program using functions to create a text file “Replace. txt”, read lines from the text file “Replace.txt” and remove all the lines that contain the character 'a' in the file and write it to another file called “New.txt”. Program: #Program using text file def writetxt(): fout=open("Replace.txt","w") rep='y' while(rep=='y'): s=input("Enter the li ne to store : ") fout.write(s) fout.write(' \ n') rep=input("Continue ") fout.close() def readtxt(): fin=open("Replace.txt") fout=open("New.txt",'w') for line in fin: for i in line: if i=='a' or i== 'A': print("Writing the line on New.txt",line) fout.write(line) break fin.close() fout.close() print("Reading from the file New.txt") fin=open("New.txt") for line in fin: print(lin e.rstrip(' \ n')) #main program print(" \ t \ t \ t Program to read a text file Replace.txt and write on the text file New.txt") print("Creating text file") writetxt() print("Reading from the file Replace.txt") readtxt() Output: Program to read a text file Replace.txt and write on the text file New.txt Creating text file Enter the line to store : This is a text file program Continue y Enter the line to store : help function is useful for beginners Continue y Enter the line to store : try using functio ns Continue y Enter the line to store : which is helpful Continue y Enter the line to store : to learn python programming Continue y Enter the line to store : in an efficient way Continue n Reading from the file Replace.txt Writing the line on New.txt This is a text file program Writing the line on New.txt to learn python programming Writing the line on New.txt in an efficient way Reading from the file New.txt This is a text file program to learn python programming in an efficient way Record Program No. 15 PROGRAM USING TEXT FILE - 2 PROBLEM DEFINITION: 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: #Program using text file def writetxt(): fout=open("Story.txt","w") rep='y' while(rep=='y'): s=input("Enter the line to store : ") fout.write(s) fout.write(' \ n') rep=input("Continue ") fout.c lose() def readtxt(): fin=open("Story.txt") for line in fin: n=line.split() for i in n: if len(i)<4: print(i) fin.close() #main program print(" \ t \ t \ t \ t \ t Program using text file Story.txt") print("Creating text file") writetxt() print("R eading from the file") readtxt() Output: Program using text file Story.txt Creating text file Enter the line to store : An apple a day keeps the doctor away Continue y Enter the line to store : Enjoy your holidays and study Continue n Reading from t he file An a day the and Record Program No. 16 : PROGRAM USING TEXT FILE - 3 PROBLEM DEFINITION: Write a Python program using fu nction to count the number of words starting with the vowel from the file Books.txt. Program: #Program using text file def writetxt(): fout=open("Books.txt","w") rep='y' while(rep=='y'): s=input("Enter the line to store :") fout.write(s) fout.write(' \ n' ) rep=input("Continue ") fout.close() def readtxt(): fin=open("Books.txt") ct=0 for line in fin: n=line.split() for i in n: if i[0]=='a' or i[0]=='e' or i[0]=='i' or i[0]=='o' or i[0]=='u' or \ i[0]==' A' or i[0]=='E' or i[0]=='I' or i[0]=='O' or i[0 ]=='U': print(i) ct+=1 fin.close() print("No. of words starting with vowel= ",ct) #main program print(" \ t \ t \ t \ t \ t Program using text file Books .txt") print("Creating text file") writetxt() print("Reading from the file") readtxt() Output: Progra m using text file Books .txt Creating text file Enter the line to store: A python program to store the details in the text file and print the number of words starting with a vowel Continue n Reading from the file A in and of a No. of words starting wit h vowel= 5 Record Program No. 17 : PROGRAM USING TEXT FILE - 4 PROBLEM DEFINITION: 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: #Program using text file def wetxt(): fout=open(" Exam .txt","w") rep='y' while(rep=='y'): s=input("Enter the line to store :") fout.write(s) fout.write(' \ n') rep=input("Continue ") fout.close() def retxt(): fin=open(" Exam .txt") for line in fin: n=line.split() if n[len(n) - 1]=="health": print(line ) fin.close() #main program print(" \ t \ t \ t \ t \ t Program using text file Exam .txt") print("Creating text file") wetxt() print("Rea ding from the file") retxt() Output: Program using text file Exam .txt Creating text file Enter the line to store: We all need good health Continue y Enter the line to store: We must need energy Continue n Reading from the file We all need good health Record Program N o. 18 : PROGRAM USING BINARY FILE - 1 PROB LEM DEFINITION: Write an interactive menu driven Python program using binary file to perform the following tasks: (i) Write the information of the car like CarNo, Carname, mileage and price onto the file “CARS.DAT” (ii) Read from the file and display al l the “Toyota cars” with their price Program: #Program using b inary file import pickle def writecar(): fout=open("Cars.dat","wb") rep='y' while rep=='y': L =[] CarNo=int(input("Enter the Car No. ")) L .append(CarNo) Carname=input(" Enter the Car Name: ") L .append(Carname) mileage=float(input("Enter mileage: ")) L .append(mileage) price=float(input("Enter the price: ")) L .append(price) pickle.dump( L ,fout) rep=input("Continue ") fout.close() def readcar(): fin=open("Cars.dat","rb") try: while True: list1=pickle.load(fin) for i in range(len(list1)): if list1[1]=="Toyota": print(list1[i] , end=' ') print() except EOFError: fin.close() #main program print(" \ t \ t \ t Program using binary file Cars.dat") print("To write the data on to the binary file") writecar() print("Read from the binary file") readcar() Output: Program using binary file Cars.dat To write the data on to the binary file Enter the Car No. 1 Enter the Car Name: Toyota Enter mileage: 899 Enter the pri ce: 6000000 Continue y Enter the Car No. 2 Enter the Car Name: Maruti Enter mileage: 455 Enter the price: 900000 Continue n Read from the binary file 1 Toyota 899.0 6000000.0 Record Program No. 19 : PROGRAM USING BINARY FILE - 2 PROBLEM DEFINITION: Write a Python program to update the binary file “Mobile.dat” containing the information about mobile like ModelNo, memorycard, details and megapixel. The Modelno whose megapixel to be updated are accepted from the user. Program: #Program using binary file import pickle import sys import os def wmobile(): fout=open("Mobile.dat","wb") rep='y' while rep=='y': L =[] ModelNo=int(input("Model No. ")) L .append(ModelNo) Memorycard=int(input("Size of Memory card: ")) L .append(Memorycard) Details=input("Details about the mobile: ") L .append(Details) Megapixel=int(input("Megapixel: ")) L .append(Megapixel) pickle.dump(L ,fout) rep=input("Continue ") fout.close() def updatemobile(mno,mp): fin=open("Mobile.dat","rb") fout= open("Temp.dat","wb") pos=0 list1=[] try: while True: f=0 list1=pickle.load(fin) for i in range(len(list1)): if list1[0]==mno: list1[3]=mp pickle.dump(list1,fout) except EOFError: fin.close() fout.close() os.remove("Mobile.dat") os.rename("Temp.dat" ,"Mobile.dat") def readmobile(): fin1=open("Mobile.dat","rb") list2=[] try: while True: list2=pickle.load(fin1) for i in range(len(list2)): print(list2[i],end=' ') print() except EOFError: fin1.close() #main program pri nt(" \ t \ t \ t Program using binary file Mobile .dat") print("To write the data on to the binary file") wmobile() print("Updating t he binary file") mn=int(input("En ter the model no to be updated : ")) mp=int(input("Enter the megapixel to be modified : ")) upd atemobile(mn,mp) print(“Reading from the updated binary file”) readmobile() Output: Program using binary file Mobile .dat To write the data on to the binary file Model No. 1 Size of Memory card: 12 Details about the mobile: nice Megapixel: 12 Continu e y Model No. 2 Size of Memory card: 24 Details about the mobile: handy Megapixel: 64 Continue n Updating the binary file E n ter the model no to be updated : 2 Enter the megapixel to be modified : 12 8 Reading from the updated binary file 1 12 nice 12 2 24 handy 128 Record Program No. 20 : PROGRAM USING BINARY FILE - 3 PROBLEM DEFINITION: Write a Python program to do the following: (i) Write the information of the directory details like name, address, areacode and phone number on to the b inary file “Telephone.DAT” (ii) Delete all the records where the areacode is “TP101”. (iii) Read from the file and display the details of all the records and also to count the number of records present in it. Program: #Program using binary file import os import pickle def wridict(): fout=open("Telephone.dat","wb") rep='y' while rep=='y': l=[] name=input("Enter the Name: ") l.append(name ) address=input("Enter the Address: ") l.append(address) areacode=input("Enter area code: ") l.append(areacode) phone=int(input("Enter the phone no: ")) l.append(phone) pickle.dump(l,fout) rep=input("Continue ") fout.close() def deldict(): fin=open("Tele phone.dat","rb") fout=open("Temp.dat","wb") try: while True: list2=pickle.load(fin) if list2[2]!="TP101": pickle.dump(list2,fout) except EOFError: fout.close() fin.close() os.remove("Telephone.dat") os.rename("Temp.dat","Telephone.dat") def readdict():