Important Python Programs
Important Python Programs
Write a menu drive program to perform the following operations into a binary file shoes.dat.
1. Add record
2. Display records
3. Search record
4. Exit
import pickle
l=[]
found=0
def add_rec():
f=open("shoes.dat","ab")
s_id=int(input("Enter Shoes ID:"))
nm=input("Enter Name:")
br=input("Enter Brand:")
ty=input("Enter Type:")
pr=int(input("Enter Price:"))
l=[s_id,nm,br,ty,pr]
pickle.dump(l,f)
print("Record Added....")
f.close()
def dis_rec():
f=open("shoes.dat","rb")
while True:
try:
l=pickle.load(f)
print(l)
except EOFError:
f.close()
break
def search_record():
f=open("shoes.dat","rb")
s_id=int(input("Enter Shoe ID:"))
while True:
try:
l=pickle.load(f)
if l[0]==s_id:
found=1
print("Record Found...",l)
else:
found=0
except EOFError:
f.close()
break
if found==0:
print("Record Not Found...")
while True:
print(''' 1. Add Record 2. Display Record 3. Search Record 4. Exit
''')
ch=int(input("Enter your choice:"))
if ch==1:
add_rec()
elif ch==2:
dis_rec()
elif ch==3:
search_record()
elif ch==4:
break
else:
print("Invalid Choice")
Write a menu drive program to python to implement stack using a list and perform
following functions:
1. Push
2. Pop
3. Display
4. Exit
def PUSH():
Stack.append(n)
def POP():
if Stack==[]:
print(Stack.pop())
def PEEK():
if Stack==[]:
else:
print(Stack[-1])
def Display():
for i in range(len(Stack)-1,-1,-1):
print(Stack[i],end=", ")
Stack=[]
while True:
ch=int(input("Hey user enter\n1 for PUSH\n2 for POP\n3 for PEEK\n4 for Display\n"))
if ch==1:
PUSH()
elif ch==2:
POP()
elif ch==3:
PEEK()
elif ch==4:
Display()
else:
1. Add record
2. Display records
3. Search record (By Team)
4. Exit
The structure of file content is: [team_id, team, wins, lost, titles]
import csv
def add_record():
f=open("telephone.csv",'a',newline='')
wo=csv.writer(f)
s_id=int(input("Enter Subscriber ID:"))
name=input("Enter name of subscriber:")
brand=input("Enter brand of telephone:")
typ=input("Enter customer type:")
price=float(input("Enter Price:"))
wo.writerow([s_id,name,brand,typ,price])
print("Record addedd Successfully.")
f.close()
def display_record():
f=open("telephone.csv",'r')
ro=csv.reader(f)
l=list(ro)
for i in range(1,len(l)):
print(l[i])
f.close()
def search_record():
f=open("telephone.csv","r")
ro=csv.reader(f)
sid=input("Enter id to search:")
found=0
for i in ro:
if sid==i[0]:
found=1
print("Record Found:")
print(i)
if found==0:
print("Record not found...")
f.close()
def menu():
while True:
print(''' 1. Add record 2. Display record 3. Search record 4. Exit ''')
ch=int(input("Enter your choice:"))
if ch==1:
add_record()
elif ch==2:
display_record()
elif ch==3:
search_record()
elif ch==4:
print("Thank you, See you again!!")
break
else:
print("Invalid Choice")
menu()
Create a python program to create a stack of student’s marks.
def display_record():
f=open("emp.csv",'r')
ro=csv.reader(f)
for i in ro:
if float(i[2])>=5000 and float(i[2])<=15000:
print(i)
f.close()
create_csv()
display_record()