[go: up one dir, main page]

0% found this document useful (0 votes)
10 views6 pages

Programs

The document contains multiple Python programs that implement various menu-driven functionalities, including sorting algorithms, CSV and binary file operations, text file analysis, dictionary management, and stack operations. Each program allows users to perform specific tasks based on their choices, such as inserting, searching, and displaying records or data. The programs demonstrate the use of lists, dictionaries, file handling, and basic control structures in Python.

Uploaded by

rajvidoshi2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Programs

The document contains multiple Python programs that implement various menu-driven functionalities, including sorting algorithms, CSV and binary file operations, text file analysis, dictionary management, and stack operations. Each program allows users to perform specific tasks based on their choices, such as inserting, searching, and displaying records or data. The programs demonstrate the use of lists, dictionaries, file handling, and basic control structures in Python.

Uploaded by

rajvidoshi2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Programs:

1) Write a menu driven program in Python using List operations based on user’s choice.
1. Bubble Sort.
2. Insertion Sort.
3. Exit.
"""Program to implement
sorting on list """
while True:
print("******Menu******")
print("1. Bubble Sort ")
print("2. Insertion Sort ")
print("3. Exit ")
ch=int(input("Enter your choice(1-3)"))

if ch==1:
l=list(eval(input("enter a list")))
length=len(l) #Bubble Sort
for i in range(0,length):
for j in range(0,length-1-i):
if l[j]>l[j+1]:
l[j],l[j+1]=l[j+1],l[j]
print("List after sort",l)
elif ch==2:
l=list(eval(input("enter a list")))
for i in range(1,len(l)): #Insertion Sort
j=i-1
temp=l[i]
while temp<l[j] and j>=0:
l[j+1]=l[j]
j-=1
l[j+1]=temp
print("List after sort",l)
elif ch==3:
break
else:
print("Invalid choice")
2) Write a menu driven program that do the following record file operations based on user’s
choice for a csv file name student.csv. Each record contains rollno, name and marks of a student.
1. Insert record.
2. Search a record.
3. Exit.
"""Program to do csv
file operations on binary file """
import csv
while True:
print("******Menu******")
print("1. Insert record ")
print("2. Search record ")
print("3 Exit ")
ch=int(input("Enter your choice(1-4)"))
if ch==1:
f=open("student.csv","a") #Open file in write mode
csv_w=csv.writer(f)
rollno=int(input("Enter the roll no"))
name=input("Enter the name")
marks=int(input("Enter marks"))
row=[rollno,name,marks]
csv_w.writerow(row)
f.close()
elif ch==2:
f=open("student.csv") #Open file in read mode
r=input("Enter the rollno to be searched")
csv_r=csv.reader(f,delimiter=',')
for row in csv_r:
if r in row:
print(row)
break
else:
print("No record found")
f.close()
elif ch==3:
break
else:
print("invalid choice")
3) Write a menu driven Python program to create and read a text file name “Lines.txt”
containing lines and display based on user’s choice.
1. Number of words.
2. Number of lines.
3. Number of digits.
4. Number of alphabets.
5. Exit.
"""Program to read
lines and store them in a list"""
fp=open("lines.txt","w")
lines=["I Love Python\n","Python Programming is easy\n","Python 3.6\n"]
fp.writelines(lines)
fp.close()
fp=open("lines.txt","r")
lines=fp.readlines()
count=0
count1=0
alpha=0
digits=0
for line in lines:
count1+=1 #count lines

words=line.split()
for word in words:

count+=1 #count words


for char in word:
if char.isalpha():
alpha+=1
elif char.isdigit():
digits+=1
while True:
print("******Menu******")
print("1. Number of words ")
print("2. Number of lines ")
print("3. Number of digits ")
print("4. Number of alphabets ")
print("5. Exit ")
ch=int(input("Enter your choice(1-3)"))

if ch==1:
print("No. of words = ", count)
elif ch==2:
print("No. of lines = ",count1)
elif ch==3:
print("No. of digits = ",digits)
elif ch==4:
print("No. of lines = ",alpha)
elif ch==5:
break
else:
print("Invalid choice")
4) Write a menu driven program that does the following record file operations based on user’s
choice for a binary file name employee.dat. Each record contains empno, name and salary of an
employee.
1. Insert record.
2. Search a record.
3. Exit.
"""Program to do record
file operations using dump and load """
import pickle
while True:
print("******Menu******")
print("1. Insert record ")
print("2. Search a record ")
print("3. Exit ")
ch=int(input("Enter your choice(1-3)"))
if ch==1:
lst=[]

fp=open("employee.dat","ab")
empno=int(input("Enter empno"))
name=input("Enter employee name")
salary=int(input("Enter the salary"))
lst=[empno,name,salary]
pickle.dump(lst,fp) # Insert a record
fp.close()
elif ch==2:
fp=open("employee.dat","rb")
eno=int(input("Enter employee no. to be searched"))
flag=0
try:
while True:
row=pickle.load(fp) #Read a record
if eno in row:
print(row)
flag=1
except EOFError:
fp.close()
if flag==0:
print("Employee not found")
elif ch==3:
break
else:
print("Invalid choice")
5) Write a menu driven program that does the following operations based on user’s choice on a
Dictionary containing Name of the Distributor as key and phone as value.
1. Add a distributor.
2. Remove a distributor.
3. Display all the Distributors.
4. Display phone number of a Distributor.
5. Exit.
"""Program to do dictionary
file operations to store name and phone"""
dict1={}
while True:

print("******Menu******")
print("1. Add a Distributor ")
print("2. Remove a Distributor ")
print("3. Display all the Distributors ")
print("4. Display phone number of a Distributor ")
print("5. Exit ")
ch=int(input("Enter your choice(1-3)"))
if ch==1:
name=input("Enter name")
phno=int(input("Enter phone no"))
dict1[name]=phno
elif ch==2:
nm=input("Enter distributor name")
del dict1[nm] #Remove distributor
elif ch==3:
if dict1=={}:
print("no records found")
else:
for key in dict1:
print("Name of the distributor = ",key)
print("Phone number of the distributor = ",dict1[key])
elif ch==4:
nm=input("Enter distributor name")
for key in dict1:
if key==nm:
print("Phone number of the distributor = ",dict1[key])
elif ch==5:
break
else:
print("Invalid choice")
6) Write a menu driven python program that creates and reads a text file name “numbers.txt”
containing integers and then based on user’s choice display.
1. Prime numbers.
2. Numbers divisible by 5 & 6.
3. Odd negative numbers.
4. Three digit numbers.
"""Program to read
numbers and store them in a list"""
fp=open("numbers.txt","w")
numbers=[12,23,300,-4001,50,60]
for i in range(len(numbers)):
fp.write(str(numbers[i])+"\n")
fp.close()
fp=open("numbers.txt","r")
pr=[]
num56=[]
oddneg=[]
num3=[]
numbers=fp.readlines()
for lines_of_num in numbers:
lines_of_num=lines_of_num.split()
for num in lines_of_num:
if len(num)==3 and num[0]!='-':
num3.append(num)
num=int(num)
for i in range(2,int(num/2)+1):
if num%i==0:
break
else:
pr.append(num)
if num%5==0 and num%6==0:
num56.append(num)
if num%2!=0 and num<0:
oddneg.append(num)

while True:
print("******Menu******")
print("1. Prime Numbers ")
print("2. Numbers divisible by 5 & 6 ")
print("3. Odd negative numbers ")
print("4. Three digit numbers ")
print("5. Exit ")
ch=int(input("Enter your choice(1-5)"))
if ch==1:
print("Prime numbers = ", pr)
elif ch==2:
print("Numbers divisible by 5 & 6 = ",num56)
elif ch==3:
print("Odd Negative numbers = ",oddneg)
elif ch==4:
print("Three digit numbers = ",num3)
elif ch==5:
break
else:
print("Invalid choice")
7) Write a menu driven python program to do following stack operations on a stack of integers.
1. Push
2. Pop
3. Display
4. Exit
"""Program to implement
Stack operations"""
stk=[]
ch='Y'
while(ch=='Y' or ch=='y'):
print("Enter 1 : Push")
print("Enter 2 : Pop")
print("Enter 3 : Display")
print("Enter 4 : Exit")
opt=int(input('enter ur choice:='))
if opt==1:
d=int(input("enter a no. : "))
stk.append(d)
elif opt==2:
if (stk==[]):
print( "Stack empty")
else:
p=stk.pop()
print ("Deleted element:", p)
elif opt==3:
if(stk==[]):
print("Stack empty")
else:
for i in range(len(stk)-1,-1,-1):
print(stk[i],end=" ")
elif opt==4:
break
else:
print('invalid choice')
ch=(input('want to continue?'))

You might also like