ComputerPracticalExam_Solutions (1)
ComputerPracticalExam_Solutions (1)
'''1 Write a menu driven program to push, pop, display and search the details of students
using list as stack data structure in Python.
Record of each student is a dictionary and contains the fields: EnrolmentNo , Name and Marks'''
students = []
def push():
student = {}
en_no = int(input("Enter the enrolment no.: "))
name = input("Enter students name: ")
marks = int(input("Enter marks: "))
student['EnrolmentNo'] = en_no
student['Name'] = name
student['Marks'] = marks
students.append(student)
print("Student details added successfully")
def pop():
if len(students) == 0:
print("Stack underflow")
else:
item = students.pop()
print("Student record removed successfully")
def display():
print("#########")
for i in students[::-1]:
print("______________")
print("EnrolmentNo = ", i["EnrolmentNo"])
print("Name = ", i["Name"])
print("Marks = ", i["Marks"])
print("______________")
print("#########")
def search():
id = int(input("Enter the roll number to be searched: "))
for i in students:
if i["EnrolmentNo"] == id:
print("______________")
print("Students details : ")
print("EnrolmentNo = ",i["EnrolmentNo"])
print("Name = ",i["Name"])
print("Marks = ",i["Marks"])
print("______________")
break
else:
print("No student with this ID")
while True:
print("Select 1 to Push ")
print("Select 2 to Pop ")
print("Select 3 to Display stack")
print("Select 4 to Search student details ")
print("Select 5 to exit ")
option = int(input("Enter your option: "))
if option == 1:
push()
elif option == 2:
pop()
elif option == 3:
display()
elif option == 4:
search()
elif option == 5:
break
else:
print("Provide valid option")
print("Program Over")
Program 2:
'''2. Write a menu driven program to push, pop display and search the details of employees
using list as stack data structure in Python.
Record of each employee is a dictionary and contains the fields: EmpID , Name and Salary'''
employees = []
def push():
employee = {}
emp_id = int(input("Enter the Employee ID no.: "))
name = input("Enter employees name: ")
salary = int(input("Enter Salary: "))
employee['EmpID'] = emp_id
employee['Name'] = name
employee['Salary'] = salary
employees.append(employee)
print("Employee details added successfully")
def pop():
if len(employees) == 0:
print("Stack underflow")
else:
item = employees.pop()
print("Employee record removed successfully")
def display():
print("#########")
for i in employees[::-1]:
print("______________")
print("EmpID = ", i["EmpID"])
print("Name = ", i["Name"])
print("Salary = ", i["Salary"])
print("______________")
print("#########")
def search():
id = int(input("Enter the employee id to be searched: "))
for i in employees:
if i["EmpID"] == id:
print("______________")
print("Employee details : ")
print("Employee ID = ",i["EmpID"])
print("Name = ",i["Name"])
print("Salary = ",i["Salary"])
print("______________")
break
else:
print("No employee with this ID")
while True:
print("Select 1 to Push ")
print("Select 2 to Pop ")
print("Select 3 to Display stack")
print("Select 4 to Search employee details ")
print("Select 5 to exit ")
option = int(input("Enter your option: "))
if option == 1:
push()
elif option == 2:
pop()
elif option == 3:
display()
elif option == 4:
search()
elif option == 5:
break
else:
print("Provide valid option")
print("Program Over")
Program 3:
'''3.a. Write a program in Python that reads content of the text file and prints total
count of word “The” in that text file
b. Write a program in Python that reads content of the text file line by line and
prints content of the file with “@” after each word
'''
f = open("prog3.txt","r")
line = " "
count = 0
while line:
line = f.readline()
L = line.split()
for word in L:
if word == "The":
count += 1
print("Total count of 'The' is :",count)
f.close()
f = open("prog3.txt","r")
item = []
line = ""
while True:
line = f.readline()
words = line.split()
for j in words:
item.append(j)
if line == "":
break
print("@".join(item))
Program 4:
'''4.
a. Write a Python program to read a text file line by line and display those words
of each line which starts with any vowel character (uppercase or lowercase)
b. Write a Python program to read a text file and copy only those lines that starts with “T”
into another text file. '''
f = open("prog3.txt", "r")
line = " "
count = 0
while line:
line = f.readline()
L = line.split()
for word in L:
if word[0] in "aeiouAEIOU":
print(word)
f.close()
count = 0
f = open("prog3.txt", "r")
count = 0
str = ""
L1=[]
L = f.readlines()
for line in L:
if line[0] == "T":
L1.append(line)
print(L1)
f1 = open("prog4.txt","w")
f1.writelines(L1)
f1.close()
Program 5:
'''5. Write a menu driven program to do the following in a csv file (Employee.csv) which
contain the details of Employee (ID, Name ,Designation,Salary)
Designation of employees is SE , SSE , Lead , Manager
i. Accept n records
ii. Search for an employee based on ID and display the details of the employee
'''
import csv
def write():
f = open("prog5.csv", "w", newline='')
writer_obj = csv.writer(f)
writer_obj.writerow(["EmpID","Name","Designation","Salary"])
while True:
e_id = input("enter employee ID: ")
name = input("enter name: ")
d = input("enter employee designation: ")
salary = int(input("enter employee salary: "))
record = [e_id, name, d, salary]
writer_obj.writerow(record)
x = input("Want to add more employee details YES ---> Y or y or No---> N or n: ")
if x in "Nn":
break
f.close()
def search():
f = open("prog5.csv", "r", newline='')
reader_obj = csv.reader(f)
emp_id = input("Enter the employee ID to search : ")
for i in reader_obj:
if i[0] == emp_id :
print("#### Employee details #### ")
print("Employement ID : ",i[0])
print("Name : ", i[1])
print("Designation : ", i[2])
print("Salary : ", i[3])
print("######## ")
break
else:
print("Employee ID not found")
f.close()
while True:
print("Select 1 to write employee data ")
print("Select 2 to search employee details ")
print("Select 3 to exit ")
option = int(input("Enter your option: "))
if option == 1:
write()
elif option == 2:
search()
elif option == 3:
break
else:
print("Provide valid option")
print("Program Over")
Program 6:
'''6. Write a menu driven program to do the following in a csv file (Students.csv) which contain
the details of Student (EnrolmentNo, Name, Section and Marks)
i. Accept n records
ii. Search for a student based on EnrolmentNo and display the details of the student '''
import csv
def write():
f = open("prog6.csv", "w", newline='')
writer_obj = csv.writer(f)
writer_obj.writerow(["EnrolmentNo","Name","Section","Marks"])
while True:
e_no = int(input("enter enrolment number: "))
name = input("enter name: ")
section = input("enter student section: ")
marks = int(input("enter student marks: "))
record = [e_no, name, section, marks]
writer_obj.writerow(record)
x = input("Want to add more student details YES ---> Y or y or No---> N or n: ")
if x in "Nn":
break
f.close()
def search():
f = open("prog6.csv", "r", newline='')
reader_obj = csv.reader(f)
to_search = input("enter the enrolment number to be searched: ")
for i in reader_obj:
if i[0] == to_search:
print("#### Student details #### ")
print("Enrolment number : ",i[0])
print("Name : ", i[1])
print("Section : ", i[2])
print("Marks : ", i[3])
print("######## ")
break
else:
print("Enrolment number not found")
f.close()
while True:
print("Select 1 to write student data ")
print("Select 2 to search student details ")
print("Select 3 to exit ")
option = int(input("Enter your option: "))
if option == 1:
write()
elif option == 2:
search()
elif option == 3:
break
else:
print("Provide valid option")
print("Program Over")
Program 7:
''' Write a menu driven program to do the following in a binary file (Employee.dat)
which contain the details of Employee (ID, Name and Salary)
i. Accept n records
ii. Search for an employee based on ID and display the details of the employee '''
import pickle
def write():
employee_data = {}
f = open("Employee.dat","ab")
while True:
emp_id = int(input ("Enter employee ID: "))
name = input("Enter employee name : ")
salary = int(input("Enter employee salary :"))
employee_data['EmpID'] = emp_id
employee_data['Name'] = name
employee_data['Salary'] = salary
pickle.dump(employee_data,f)
ch = input("More ? (Y or y/N or n)")
if ch in ('N','n'):
break
f.close()
def search():
found = 0
emp_id = int(input("Enter employee ID you want to search :"))
f = open("Employee.dat", "rb")
try:
while True:
records = pickle.load(f)
if records['EmpID'] == emp_id:
print("#### Details of Employee ####")
print("Employee ID = ",emp_id)
print("Employee Name = ", records['Name'])
print("Employee Salary = ", records['Salary'])
print("#################")
found = 1
break
except EOFError:
f.close()
if found == 0:
print("Sorry Employee ID not found....")
f.close()
while True:
print("Select 1 to write employee data ")
print("Select 2 to search employee details ")
print("Select 3 to exit ")
option = int(input("Enter your option: "))
if option == 1:
write()
elif option == 2:
search()
elif option == 3:
break
else:
print("Provide valid option")
print("Program Over")
Program 8:
''' 8. Write a menu driven program to do the following in a binary file (Student.dat) which contain the
details of Student (EnrolmentNo, Name, Section and Marks)
The marks are in the range 0 to 100.
i. Accept n records
ii. Print details of all students who have scored more than 75
'''
import pickle
def write():
student_data = {}
f = open("Student.dat","ab")
while True:
rollno = int(input ("Enter roll number : "))
name = input("Enter student name : ")
section = input("Enter student section :")
marks = int(input("Enter student marks :"))
student_data['RollNo'] = rollno
student_data['Name'] = name
student_data['Section'] = section
student_data['Marks'] = marks
pickle.dump(student_data,f)
ch = input("More ? (Y or y/N or n)")
if ch in ('N','n'):
break
f.close()
def search():
f = open("Student.dat", "rb")
try:
while True:
records = pickle.load(f)
if records['Marks'] > 75:
print("#### Details of Student ####")
print("Roll Number = ",records['RollNo'])
print("Students Name = ", records['Name'])
print("Students Section = ", records['Section'])
print("Students marks = ", records['Marks'])
print("#################")
except EOFError:
f.close()
while True:
print("Select 1 to write student data ")
print("Select 2 to search student who scored more than 75 ")
print("Select 3 to exit ")
option = int(input("Enter your option: "))
if option == 1:
write()
elif option == 2:
search()
elif option == 3:
break
else:
print("Provide valid option")
print("Program Over")