Practcial Final 2023 1
Practcial Final 2023 1
Write a menu driven program in Python that asks the user to add, display, and
search records of employee stored in a binary file. The employee record contains
employee code, name and salary. It should be stored in a list object. Your program
should pickle the object and save it to a binary file.
Coding:
import pickle
def set_data():
empcode = int(input('Enter Employee code: '))
name = input('Enter Employee name: ')
salary = int(input('Enter salary: '))
print()
#create a list
employee = [empcode,name,salary]
return employee
def display_data(employee):
print('Employee code:', employee[0])
print('Employee name:', employee[1])
print('Salary:', employee[2])
print()
def write_record():
#open file in binary mode for writing.
outfile = open('emp.dat', 'ab')
#serialize the object and writing to file
pickle.dump(set_data(), outfile)
#close the file
outfile.close()
def read_records():
#open file in binary mode for reading
infile = open('emp.dat', 'rb')
#read to the end of file.
while True:
try:
#reading the oject from file
employee = pickle.load(infile)
#display the object
display_data(employee)
except EOFError:
break
#close the file
infile.close()
def search_record():
infile = open('emp.dat', 'rb')
empcode = int(input('Enter employee code to search: '))
flag = False
#read to the end of file.
while True:
try:
#reading the oject from file
employee = pickle.load(infile)
#display record if found and set flag
if employee[0] == empcode:
display_data(employee)
flag = True
break
except EOFError:
break
if flag == False:
print('Record not Found')
print()
#close the file
infile.close()
def show_choices():
print('Menu')
print('1. Add Record')
print('2. Display Records')
print('3. Search a Record')
print('4. Exit')
def main():
while True:
show_choices()
choice = input('Enter choice(1-4): ')
print()
if choice == '1':
write_record()
elif choice == '2':
read_records()
elif choice == '3':
search_record()
elif choice == '4':
break
else:
print('Invalid input')
#call the main function.
if __name__ == "__main__":
main()
output:
Q.2. Write a python program to read a file named "story.txt"and count and print
the following: (i) length of the file (total no. of characters in file) (ii)total number of
alphabets (iii) total number of upper case alphabets (iv) total number of lower case
alphabets (v) total no, of digits (vi) total no. of white spaces characters (vii) total no.
of special characters
Coding:
def count_all():
# Initialize the counters
alpha_count = 0
upper_count = 0
lower_count = 0
digit_count = 0
wspaces_count = 0
splchar_count = 0
# Open the text file
with open("myfile.txt") as f:
# read the content of the file
content = f.read()
for ch in content:
if((ch >='A' and ch <='Z') or (ch >= 'a' and ch <= 'z')):
alpha_count += 1
if(ch >= 'A' and ch <= 'Z'):
upper_count += 1
else:
lower_count += 1
elif(ch >= '0' and ch <= '9'):
digit_count += 1
elif (ch == ' ' or ch == '\t' or ch == '\n'):
wspaces_count += 1
else:
splchar_count += 1
print("The total no. of alphabets: ", alpha_count)
print("The total no. of upper case alphabets: ", upper_count)
print("The total no. of lower case alphabets: ", lower_count)
print("The total no. of digits: ", digit_count)
print("The total no. of whitespace characters: ", wspaces_count)
print("The total no. of special characters: ", splchar_count)
# driver's code
count_all()
output:
Q.3. Write a python program to maintain book details like book code, book title and
price using stacks data structures. Implement push(), pop() and traverse() function
Coding:
book_stack = []
def push():
print("Enter the details of the book:")
book_code = input("Book code: ")
book_title = input("Book title: ")
book_author = input("Author: ")
book_price = float(input("Price(in Rs.): "))
book = (book_code, book_title, book_author, book_price)
book_stack.append(book)
def pop():
if(book_stack ==[]):
print("Underflow / Book Stack in empty")
else:
book_code, book_title, book_author, book_price = book_stack.pop()
print("The popped element is: ")
print("Book code: ", book_code)
print("Book title: ", book_title, "Author: ", book_author)
print("Price(in Rs.): ", book_price)
def traverse():
if not (book_stack == []):
n = len(book_stack)
for i in range(n-1,-1,-1):
print(book_stack[i])
else:
print("Empty , No book to display")
while True:
print("1. Push")
print("2. Pop")
print("3. Traversal")
print("4. Exit")
choice = int(input("Enter your choice: "))
if(choice == 1):
push()
elif(choice == 2):
pop()
elif(choice == 3):
traverse()
elif(choice == 4):
print("End")
break
else:
print("Invalid choice.")
output :
1. Consider the following tables GAMES and PLAYER. Write SQL commands for the
following statements.
TABLE: GAMES
GCode GameName Number PrizeMoney ScheduledDate
101 Kabaddi 2 5000 23-Jan-2007
102 Badminton 2 12000 12-Dec-2013
103 Table Tennis 4 8000 14-Feb-2014
105 Chess 2 9000 1-Jan-2015
108 Table Tennis 4 25000 19-Mar-2014
Table: PLAYER
PCode Name GCode
1 Ravi Shankar 101
2 Amir 108
3 Jatin 101
4 Shahrukh 103
I. To display details of those Games which have PrizeMoney more than 7000.
>>> select * from Games where prizemoney>7000;
II. To display the content of the GAMES table in ascending order of ScheduleDate.
>>> select * from Games order by ScheduleDate ASC;
III. To display GCode,Name,GameName from GAMES, PLAYER table.
>>> select G.Gcode,G.GameName,P.Name from Games G, Player P where
G.GCode=P.GCode;
IV. To display PCode, Name from player whose name starts with ‘R’.
>>> select PCode,Name from player where Name like ‘R%’;
2. Consider the table LOANS. Write the sql command for the following quaries.
TABLE: LOANS
3. Consider the following tables SHOP and ACCESSORIES and answer (a) to (d) parts of the
question:
Table: CARDEN
TABLE : CUSTOMER
COUNT(DISTINCT MAKE)
4