0% found this document useful (0 votes)
4 views

Important Python Programs

The document outlines several Python programs that implement basic data management functionalities, including adding, displaying, and searching records in binary and CSV files. It also includes a stack implementation using lists with operations such as push, pop, and display. Each program is structured with a menu-driven interface for user interaction.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Important Python Programs

The document outlines several Python programs that implement basic data management functionalities, including adding, displaying, and searching records in binary and CSV files. It also includes a stack implementation using lists with operations such as push, pop, and display. Each program is structured with a menu-driven interface for user interaction.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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():

n=int(input("Enter value to insert in Stack"))

Stack.append(n)

def POP():

if Stack==[]:

print("Underflow!!!\n Stack is Empty")


else:

print(Stack.pop())

def PEEK():

if Stack==[]:

print("Underflow!!!\n Stack is Empty")

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:

print("Thank you!!!! - Exit")


 Write a menu drive program to perform following operations into worldcup2023.csv.

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.

1. Write function PUSH to add marks in to the stack.


2. Write function POP to remove the marks from stack and display the same.
 Write a program to create CSV file and store empno, name and salary of employee.
Display the records whose salary in range of 5000 to 15000.
import csv
def create_csv():
f=open("emp.csv",'w',newline='')
wo=csv.writer(f)
n=int(input("How many records?:"))
for i in range(n):
empno=int(input("Enter employee number:"))
ename=input("Enter Ename:")
salary=float(input("Enter Salary:"))
wo.writerow([empno,ename,salary])
print("Record file created Successfully.")
f.close()

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()

You might also like