Professional Documents
Culture Documents
General Instructions:
1. All questions are compulsory.
2. All questions carry equal marks (5 marks each).
3. All questions are divided into 2 parts (2.5 marks each).
4. All programming questions are to be answered using Python
Language only.
# Displays Records
def display_records():
f = open('employee.dat', 'rb')
try:
while True:
x = p.load(f)
print(x)
except EOFError:
print("Complete Data Read Successfully.")
print()
f.close()
# Update a Record
def update_record():
f = open('employee.dat', 'ab+')
f.seek(0)
empid = int(input("Enter ID of record to update: "))
try:
while True:
x = f.tell()
l = p.load(f)
if l[0] == empid:
f.seek(x)
print("Found Record:")
newid = int(input("Update ID: "))
name = input("Update Name: ")
salary = float(input("Update Salary: "))
data = [newid, name, salary]
p.dump(data, f)
print("Record Updated.")
f.close()
break
except EOFError:
print("Error 404: Record Not Found")
f.close()
# Function Calls
add_records()
display_records()
update_record()
display_records()
Q 2. Write a python program to create a binary file with name and
roll number. Search for a given roll number and display name, if not
found display appropriate message.
Solution: import pickle as p
# Function Calls
print("Original list:", input_lst)
bubble_sort_asc(input_lst)
print("Ascending order:", input_lst)
bubble_sort_desc(input_lst)
print("Descending order:", input_lst)
Q 6. Create a Menu-Driven Program which add data and reads data
on the table Students (StudentID, Name, Course) by using python
mysql connector.
Solution:
import mysql.connector as c
password = input("Enter Password to SQL server: ")
db = c.connect(host="localhost", username="root",
passwd=password)
cursor = db.cursor()
# Create Database and Tables
cursor.execute("CREATE DATABASE IF NOT EXISTS Students;")
cursor.execute("USE Students;")
cursor.execute("CREATE TABLE IF NOT EXISTS Students (StudentID
INTEGER PRIMARY KEY, Name VARCHAR(50) NOT NULL, Course
VARCHAR(50) NOT NULL);")
# Menu
while True:
print("""******MENU*******
1. Delete From Dictionary
2. Search Phone Number using Name from Dictionary
3. Exit
""")
option = input("Choose Action [1/2/3]: ")
if option == "1":
del_name = input("Which students details do you want to
delete: ")
print(f"Deleting {del_name.lower()} Details...")
print(student_dict.pop(del_name.lower(), "Student Not Found!
Try Again."))
print()
elif option == "2":
search_name = input("Which students number do you want to
search: ")
print("Searching...")
print(student_dict.get(search_name.lower(), "Student Not
Found! Try Again."))
print()
elif option == "3":
print("Exiting...")
break
else:
print("Invalid Action! Please Enter Valid Action.")
print()
Q 8. Write a program to read a list of n integers (positive as well as
negative). Create two new lists, one having all positive numbers
with sum and the other having all negative numbers with sum from
the given list.
Solution:
input_lst = eval(input("Enter a list of positive and negative integers:
"))
for i in lst:
if i > 0:
positive_lst.append(i)
positive_sum += i
elif i < 0:
negative_lst.append(i)
negative_sum += i
print("Original List:", input_lst)
print("List of Positive Integers:", positive_lst)
print("Sum of Positive Integers:", positive_sum)
print("List of Negative Integers:", negative_lst)
print("Sum of Negative Integers:", negative_sum)
# Function Call
positive_negative_sort(input_lst)