0% found this document useful (0 votes)
12 views9 pages

Practcial Final 2023 1

The document contains multiple programming tasks in Python, including a menu-driven program for managing employee records using pickling, a file analysis program for counting characters in a text file, and a stack implementation for maintaining book details. Additionally, it includes SQL queries for retrieving information from tables related to games, loans, and cars. Each section provides code snippets and example outputs for clarity.

Uploaded by

pixelizedmusicic
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)
12 views9 pages

Practcial Final 2023 1

The document contains multiple programming tasks in Python, including a menu-driven program for managing employee records using pickling, a file analysis program for counting characters in a text file, and a stack implementation for maintaining book details. Additionally, it includes SQL queries for retrieving information from tables related to games, loans, and cars. Each section provides code snippets and example outputs for clarity.

Uploaded by

pixelizedmusicic
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/ 9

Q.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.

AccNo Cust_Name Loan_Aount Instalments Int_Rate Start_Date Interest

1 R.K.Gupta 300000 36 12.00 19-07-2009 1200

2 S.P.Sharma 500000 48 10.00 22-03-2008 1800

3 K.P.Jain 300000 36 NULL 08-03-2007 1600

4 M.P.Yadav 800000 60 10.00 06-12-2008 2250

5 S.P.Sinha 200000 36 12.50 03-01-2010 4500

6 P.Sharma 700000 60 12.50 05-06-2008 3500

7 K.S.Dhall 500000 48 NULL 05-03-2008 3800

TABLE: LOANS

i. Display the sum of all loans amount whose interest rate is


greater than 10.
>>> select sum(Loan_Amount) from LOANS where
interest>10;
ii. Display the Maximum Interest from Loans table.
>>> select MAX(Interest) from LOANS;
iii. Display the count of all loans holders whose interest is Null.
>>> SELECT COUNT(*) FROM LOANS WHERE Interest is
NULL;
iv. Display the interest-wise details of loan account holder.
>>> SELECT * FROM LOANS GROUP BY Interest;

3. Consider the following tables SHOP and ACCESSORIES and answer (a) to (d) parts of the
question:
Table: CARDEN

Ccode CarName Make Color Capacity Charges


501 A-Star Suzuki RED 3 14
503 Indigo Tata SILVER 3 12
502 Innova Toyoto WHITE 7 15
509 SX4 Suzuki SILVER 4 14
510 C Class Mercedes RED 4 35

TABLE : CUSTOMER

CCode CName Ccode


1001 Hemant SAHU 501
1002 Raj Lal 509
1003 Feroz Shah 503
1004 Ketan Dhal 502

a. To display the names of all silver-colored cars.


>>> SELECT CarName From carden where color LIKE ‘silver’;
b. To display name of car, make and capacity of cars in descending order of their
sitting capacity.
>>> SELECT CarName, Make,Capacity from carden ORDER BY capacity;
c. SELECT COUNT(DISTINCT MAKE ) FROM CARDEN;

COUNT(DISTINCT MAKE)
4

d. SELECT CarName FROM CARDEN WHERE CAPACITY=4;


CarName
SX4
C Class

You might also like