PROGRAM-1
Date:
[Link] a menu driven program using functions to perform bubble sort and
insertion sort
Code:
def bubble(l):
n=len(l)
for i in range(n-1):
for j in range(n-i-1):
if l[j]>l[j+1]:
l[j],l[j+1]=l[j+1],l[j]
print(l)
def insertion(l):
for i in l:
j=[Link](i)
while j>0:
if l[j]<l[j-1]:
l[j-1],l[j]=l[j],l[j-1]
else:
break
j=j-1
print(l)
ans="yes"
while ans=="yes":
l=[]
n=int(input("enter limit:"))
for i in range(n):
a=int(input("enter the element"))
[Link](a)
print('[Link] sort,[Link] sort')
ch=int(input("enter choice"))
if ch==1:
bubble(l)
elif ch==2:
insertion(l)
else:
print("invalid choice")
ans=input("Do you want to continue, yes/no")
Output:
PROGRAM-2
Date:
[Link] a menu driven program using functions to perform
(i).linear search
(ii).binary search
Code:
def linear(l,el): #function to perform linear search
for i in range(len(l)):
if l[i]==el:
print("found at",i+1,"position")
break
else :
print("not found")
def binary(l,el): #function to perform binary search
[Link]()
low=0
high=len(l)-1
while low <=high:
mid=(low+high)//2
if el==l[mid]:
print("found")
break
elif el<l[mid]:
high=mid-1
elif el>l[mid]:
low=mid+1
else :
print("not found")
ans='yes'
while ans=='yes':
l=[]
n=int(input("enter limit"))
for i in range(n):
a=int(input("enter element"))
[Link](a)
el=int(input("enter element to be searched"))
print("[Link] search, [Link] search")
ch=int(input("enter choice"))
if ch==1:
linear(l,el)
elif ch==2:
binary(l,el)
else:
print("invalid choice")
ans=input("do you want to continue?, yes/no")
Output:
PROGRAM-3
Data:
[Link] a program to display the unique and duplicated items in a list
Code:
def sort(l):#function to sort
u=[]
d=[]
for i in range(len(l)):
a=l[i]
c=-1
for j in l:
if j==a:
c+=1
if c>0 and a not in u:
[Link](a)
elif c<1:
[Link](a)
print(u)
print(d)
ans="yes"
while ans=="yes":
n=int(input("enter limit"))
L=[]
for i in range(n):#creating list
x=int(input("enter element:"))
[Link](x)
sort(L)
ans=input("Do you want to continue, yes/no:")
Output:
PROGRAM-4
Date:
[Link] a program to accept a dictionary D and then display the elements in
opposite mapping
Code:
def oppo(d):#function to do opposite mapping
l=[]
for i in d:
[Link](i)
for i in l:
d[d[i]]=i
del d[i]
print(d)
D={}
ans="yes"
while ans=="yes":
x=int(input("enter limit:"))
for i in range(x):#creating dictionary
k=input("enter key:")
v=input("enter value:")
D[k]=v
oppo(D)
ans=input("Do you want to continue, yes/no:")
Output:
PROGRAM-5
Date:
[Link] a program to exchange first half of the elements in a list with
second
Code:
def swap(l):#function to swap first half with second half
m=len(l)
n=m//2
if m%2==0:
for i in range(0,m-1):
if n<m:
l[i],l[n]=l[n],l[i]
n=n+1
print(l)
else:
for i in range(0,n):
if n<m:
l[i],l[n+1]=l[n+1],l[i]
n=n+1
print(l)
ans="yes"
while ans=="yes":
n=int(input("enter the limit:"))
L=[]
for i in range(n):#list creation
a=int(input("enter the element"))
[Link](a)
swap(L)
ans=input("Do you want to continue:")
Output:
PROGRAM-6
Date:
[Link] a program generating a random number b/w 1-6 simulating a dice
roll
Code:
import random
def roll():#Function to roll dice
print("__Rolling Dice__")
a=[Link](1,6)
print("Dice landed on",a)
ans="yes"
while ans=="yes":
roll()#Function call
ans=input("do u want to continue, yes/no:")
Output:
PROGRAM-7
Date:
[Link] a program to accept name and phone number of employees,display
the content in dictionary and search a number based on name
Code:
def search(d):#search employee
nm=input("enter name to be searched:")
for i in d:
if i==nm:
print(d[i])
break
else:
print('not found')
d={}
n=int(input("enter limit:"))
for i in range(n):
nm=input("enter name:")
ph=int(input("enter number:"))
d[nm]=ph
ans='yes'
while ans=="yes":
print('1-display,2-search')
ch=int(input("enter choice(1/2):"))
if ch==1:
print(d)#displaying dictionary
elif ch==2:
search(d)
else:
print("not found")
ans=input("do you want to continue(yes/no):")
Output:
PROGRAM-8
Date:
8. Write a menu driven program (using functions) to i) Write the given
below text to a file known as "[Link]" "Neither apple nor pine are in
pineapple. Boxing rings are square. Writers write, but fingers don't fing.
Overlook and oversee are opposites. A house can burn up as it burns
down. An alarm goes off by going on." ii) Read the entire contents from
[Link] iii) Display the last line of the file [Link] iv) Display the words
which are having less than 4 letters v)To count and display the total
number of alphabet in [Link]
Code:
def write_content():#writing into file
f=open("[Link]","w")
content="Neither apple nor pine are in pineapple. Boxing rings are
square. Writers write, but fingers don't fing. Overlook and oversee are
opposites. A house can burn up as it burns down. An alarm goes off by
going on."
[Link](content)
print("successfull")
[Link]()
def display_content():
f=open("[Link]","r")
content=[Link]()
print(content)
[Link]()
def display_lastline():#Display last line from file
f=open("[Link]")
content=[Link]().split(".")
print(content[-2])
[Link]()
def less4letter():
f=open("[Link]")
content=[Link]().replace('.','').replace(',','').split()
for i in content:
if len(i)<4:
print(i)
def no_of_alpha():
f=open("[Link]")
content=[Link]()
c=0
for i in content:
if (65<=ord(i)<=90) or (97<=ord(i)<=122):
c += 1
print(c)
ans="yes"
while ans=="yes":
print('1-write content \n2-display content\n3-display last line\n4-character
less than 4 letter\n5-number of alphabet')
ch=int(input("enter choice:"))
if ch==1:
write_content()
elif ch==2:
display_content()
elif ch==3:
display_lastline()
elif ch==4:
less4letter()
elif ch==5:
no_of_alpha()
else:
print("wrong choice")
ans=input("do u want to continue (yes/no):")
Output:
PROGRAM-9
Date:
9. Write a menu driven program(using functions) to i)To write the given
below text to a file known as "[Link]" "India is the fastest growing
[Link] is looking for more investments around the globe. The
whole world is looking at India as a great [Link] of the Indians can
foresee the heights that India is capable of reaching.’’ ii) Replace every
occurrence of 'the' with 'these' iii) Display the frequency of each word in the
file. iv) Display the lines starting with 'I' or 'T' v) Copy the contents of
[Link] to another file [Link]
Code:
def write_content():#write content
f=open("[Link]","w")
content="India is the fastest growing [Link] is looking for more
investments around the globe. The whole world is looking at India as a
great [Link] of the Indians can foresee the heights that India is
capable of reaching."
[Link](content)
print(content)
[Link]()
def replace_the():
f=open("[Link]","r+")
content=[Link]().replace('the','these').replace("THE",'these').replace("The","
these")
print("replaced data:",content)
[Link](0)
[Link](content)
[Link]()
def frequency():#no of words
f=open("[Link]","r")
content=[Link]().replace('.','').split()
word_count={}
for i in content:
if i in word_count:
word_count[i] +=1
else:
word_count[i]=1
print(word_count)
def dislay_line():
f=open("[Link]","r")
content=[Link]().split('.')
for i in content:
if len(i)>0:
if [Link]()[0] in 'ITit':
print(i)
def new_file():
f=open("[Link]","r")
content=[Link]()
f1=open("[Link]","w")
[Link](content)
print(content)
ans="yes"
while ans=="yes":
print('1-write content \n2-replace the with these\n3-no of times a word
occurs\n4-display lines starting with I or T\n5-create new file')
ch=int(input("enter choice:"))
if ch==1:
write_content()
elif ch==2:
replace_the()
elif ch==3:
frequency()
elif ch==4:
dislay_line()
elif ch==5:
new_file()
else:
print("wrong choice")
ans=input("do u want to continue (yes/no):")
Output:
PROGRAM-10
Date:
10. Write a program to write, read, search, modify and delete the contents
of a binary file [Link](using list or dictionary)
Code:
import pickle
def write_binary(): # write into file
f=open("[Link]","wb")
ans="yes"
while ans=="yes":
rollno=int(input("enter rollno:"))
name=input("enter name:")
mark=int(input("enter mark:"))
l={"rollno":rollno,"name":name,"mark":mark}
[Link](l,f)
ans=input("Do you want to continue:")
print("File created")
[Link]()
def read_binary():
f=open("[Link]","rb")
try:
while True:
x=[Link](f)
print(x)
except EOFError:
pass
[Link]()
def search_binary(): #search a student
f=open("[Link]","rb")
a=int(input("enter roll no:"))
try:
while True:
x=[Link](f)
if x['rollno']==a:
print(x)
else:
continue
except EOFError:
pass
[Link]()
def modify_binary():
f=open("[Link]","rb+")
rollno=int(input("enter the rollno:"))
mark=input("enter new mark")
try:
while True:
pos=[Link]()
x=[Link](f)
if x['rollno']==rollno:
x['mark']=mark
else:
continue
print(x)
[Link](pos)
[Link](x,f)
print("modified")
except EOFError:
pass
[Link]()
def modify_binary():
try:
with open("[Link]", "rb") as f:
records = []
while True:
[Link]([Link](f))
except EOFError:
pass
rollno = int(input("Enter rollno to modify: "))
new_mark = int(input("Enter new mark: "))
modified = False
for record in records:
if record["rollno"] == rollno:
record["mark"] = new_mark
modified = True
if modified:
with open("[Link]", "wb") as f:
for record in records:
[Link](record, f)
print("Record modified successfully.\n")
else:
print("Record not found.\n")
def delete_binary():
try:
with open("[Link]", "rb") as f:
records = []
while True:
[Link]([Link](f))
except EOFError:
pass
rollno = int(input("Enter rollno to delete: "))
new_records = [r for r in records if r["rollno"] != rollno]
if len(new_records) != len(records):
with open("[Link]", "wb") as f:
for record in new_records:
[Link](record, f)
print("Record deleted successfully.\n")
else:
print("Record not found.\n")
ans="yes"
while ans=="yes":
print("1-write,2-read,3-search,4-modify,5-delete")
ch=int(input("enter choice:"))
if ch==1:
write_binary()
elif ch==2:
read_binary()
elif ch==3:
search_binary()
elif ch==4:
modify_binary()
elif ch==5:
delete_binary()
else:
print("wrong choice")
ans=input("Do you want to continue:")
Output:
PROGRAM-11
Date:
11. Write a program to write ,read,search ,modify the contents of a csv
file '[Link](using list or dictionary)
Code:
import csv
filename = "[Link]"
def write_csv(): #write into file
with open(filename, "w", newline="") as f:
w = [Link](f)
ans = "yes"
while [Link]() == "yes":
empid = input("Enter Employee ID: ")
name = input("Enter Employee Name: ")
salary = input("Enter Employee Salary: ")
[Link]([empid, name, salary])
ans = input("Do you want to add another record? (yes/no): ")
print("File created successfully.")
def read_csv():#read from file
try:
with open(filename, "r") as f:
r = [Link](f)
for row in r:
print(row)
except EOFError:
pass
def search_csv():
empid = input("Enter Employee ID to search: ")
found = False
try:
with open(filename, "r") as f:
r = [Link](f)
for row in r:
if row[0] == empid:
print("Record found:", row)
found = True
break
if not found:
print("Record not found.")
except FileNotFoundError:
print("File not found.")
def modify_csv():
records = []
try:
with open(filename, "r") as f:
r = [Link](f)
records = list(r)
except FileNotFoundError:
print("File not found.")
return
empid = input("Enter Employee ID to modify: ")
modified = False
for i in range(1, len(records)):
if records[i][0] == empid:
print("Old record:", records[i])
name = input("Enter new name: ")
salary = input("Enter new salary: ")
records[i] = [empid, name, salary]
modified = True
break
if modified:
with open(filename, "w", newline="") as f:
w = [Link](f)
[Link](records)
print("Record modified successfully.")
else:
print("Record not found.")
ans = "yes"
while [Link]() == "yes":
print("1 - Write")
print("2 - Read")
print("3 - Search")
print("4 - Modify")
choice = int(input("Enter your choice: "))
if choice == 1:
write_csv()
elif choice == 2:
read_csv()
elif choice == 3:
search_csv()
elif choice == 4:
modify_csv()
else:
print("Invalid choice.\n")
ans = input("Do you want to continue? (yes/no): ")
Output:
PROGRAM-12
Date:
12. Write a menu driven program to perform push, pop and display a
stack – BOOK (Bookno, Bookname, Authorname are the details)using
functions.
Code:
def push_stack():#input values into stack
ans="yes"
while ans=="yes":
bno=int(input("enter book number:"))
bname=input("enter book name:")
aname=input("enter author name:")
[Link]([bno,bname,aname])
ans=input("Do u want to add another record:")
def pop_stack():#delete elements from stack
if BOOK==[]:
print("underflow")
else:
print("record deleted:",[Link]())
def display_stack():
for i in range(len(BOOK)-1,-1,-1):
print(BOOK[i])
ans="yes"
BOOK=[]
while ans=="yes":
ch=int(input("1-Push,2-Pop,3-Display:"))
if ch==1:
push_stack()
elif ch==2:
pop_stack()
elif ch==3:
display_stack()
else:
print("error")
ans=input("Do u want to continue:")
Output:
PROGRAM-13
Date:
13. Write a program to push a line of text into a stack from the input
terminal. Display the string in the reverse order, but each character
appearing twice.(Eg:- if the string is a b c d e, the output should be ee dd
cc bb aa)
Code:
def display():# display elements
x = [Link]()
for i in x:
print([Link]()*2,end=" ")
ans = "yes"
while [Link]() == "yes":
stack = []
x = input("Enter string: ")
for i in x: # stack creation
if i!=" ":
[Link](i)
display()
ans = input("\nDo you want to continue? ")
Output:
PROGRAM-14
Date:
[Link] a program to check whether a given text is palindrome or
not using stack.
Code:
def check_pali():#checking if string is palindrome
rev_stack = []
x = [Link]()
for i in stack:
rev_stack.append([Link]())
if rev_stack == stack:
print("Palindrome")
else:
print("Not palindrome")
ans = "yes"
while [Link]() == "yes":
stack = []
x = input("Enter string: ")
for i in x:
[Link](i)
check_pali()#function call
ans = input("Do you want to continue? ")
Output:
PROGRAM-15
Date:
15. Write a program to find the minimum element from a stack.
Code:
def min_element():#finding minimum element
x=[Link]()
n=len(x)
for i in range(n-1):
for j in range(n-i-1):
if x[j]<x[j+1]:
x[j],x[j+1]=x[j+1],x[j]
print("min element:",[Link]())#output
ans = "yes"
while [Link]() == "yes":
stack = []
n=int(input("enter limit: "))
for i in range(n):
a=int(input("enter input: "))
[Link](a)
min_element()
ans = input("Do you want to continue? ")
Output:
PROGRAM-16
Date:
[Link] a menu drive program to connect all database and store record of
coaches and display the records
Code:
import [Link] as m
mydb=[Link](host='localhost',user='root',password='123',database='nih
aal')
c=[Link]()
def input_data():#entering values
id1=int(input("enter coach id:"))
name=input("enter coach name:")
age=int(input("enter age: "))
sport=input("enter sport: ")
date=input("enter date(yyyy-mm-dd):")
pay=input("enter salary:")
sex=input("enter sex:")
value=[id1,name,age,sport,date,pay,sex]
try:
query='insert into club values(%s,%s,%s,%s,%s,%s,%s)'
[Link](query,value)
[Link]()
except:
[Link]()
def display():#display values
try:
[Link]("select * from club")
x=[Link]()
for i in x:
print(i)
except:
print("error")
ans="yes"
while ans=="yes":
print('1-enter value into table')
print('2-display all data')
ch=int(input("enter choice(1,2):"))
if ch==1:
input_data()
elif ch==2:
display()
else:
print("invalid choice")
ans=input("do u want to continue(yes/no):")
[Link]()
[Link]()
Output:
PROGRAM-17
Date:
[Link] the table CLUB. Write a menu driven program to connect with
database and search for coach/coaches based on
(i)coach_id
(ii)sports
and display record. If coach not found, `display appropriate message
Code:
import [Link] as m
mydb=[Link](host="localhost",user="root",password="123",database="
nihaal")
c=[Link]()
ans="yes"
while ans=="yes":
print("1-search using coach_id")
print("2-seach using sports")
ch=int(input("enter choice"))
if ch==1:#searching using coach id
try:
id1=int(input("enter id:"))
[Link]("select * from club where coach_id={}".format(id1))
x=[Link]()
for i in x:
print(i)
if len(x)==0:
print("no record found")
except:
print("error")
elif ch==2:#searching using sport
try:
sport=input("enter sport")
[Link]("select * from club where sport='{}'".format(sport))
x=[Link]()
for i in x:
print(i)
if len(x)==0:
print("no record found")
except:
print("invalid choice")
ans=input("Do you want to continue(yes/no):")
[Link]()
[Link]()
Output:
PROGRAM-18
Date:
18. Consider the table CLUB. Write a program to connect with the
database and update the pay of a particular coach based on coach _id.
Code:
import [Link] as m
mydb=[Link](host="localhost",user="root",password="123",database="
12a2025")
c=[Link]()
ans="yes"
while ans=="yes":
try:
id1=int(input("enter id:"))
pay=int(input("enter new pay:"))
[Link]("update club set pay={} where
coach_id={}".format(pay,id1))
[Link]()
[Link]("select * from club")
x=[Link]()
for i in x:
print(i)
except:
print("error")
ans=input("Do u want to continue:")
[Link]()
[Link]()
Output:
PROGRAM-19
Date:
19. Consider the table CLUB. Write a menu driven program to connect with
the database and i) delete the details of coaches based on coach_id ii)
delete details of coaches whose names start with “k”.
Code:
import [Link] as m
mydb=[Link](host="localhost",user="root",password="123",database="
12a2025")
c=[Link]()
ans="yes"
def del_coach():#deleting coach
try:
id1=int(input("enter id:"))
[Link]("delete from club where coach_id={}".format(id1))
[Link]()
except:
print("error")
def del_k():
try:
[Link]("delete from club where coach_name like 'K%'")
[Link]()
except:
[Link]()
def display():#display table
try:
[Link]("select * from club")
x=[Link]()
for i in x:
print(i)
except:
pass
while ans=="yes":
ch=int(input("1-Delete based on id,2-Delete user starting with k,3-
Display"))
if ch==1:
del_coach()
elif ch==2:
del_k()
elif ch==3:
display()
else:
print("error")
ans=input("Do u want to continue:")
[Link]()
[Link]()
Output: