100% found this document useful (1 vote)
2K views

Practice Questions For Practical

The document contains sample code and questions related to Python programming and database operations. Specifically, it includes: 1. A menu-driven program to perform add, display, search and exit operations on a binary file containing shoe records with a defined structure. 2. Questions asking to complete code snippets related to connecting to a MySQL database and inserting a record into a customer table, as well as implementing a stack using a list in Python. 3. Blank questions asking to write menu-driven programs to perform operations on telephone records in a CSV file and doctor records in a binary file.

Uploaded by

Ishaan Seth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views

Practice Questions For Practical

The document contains sample code and questions related to Python programming and database operations. Specifically, it includes: 1. A menu-driven program to perform add, display, search and exit operations on a binary file containing shoe records with a defined structure. 2. Questions asking to complete code snippets related to connecting to a MySQL database and inserting a record into a customer table, as well as implementing a stack using a list in Python. 3. Blank questions asking to write menu-driven programs to perform operations on telephone records in a CSV file and doctor records in a binary file.

Uploaded by

Ishaan Seth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Practice questions for practical

Q – 1 a) Write a menu drive program to perform following operations into a binary file
shoes.dat. 8M

1. Add record
2. Display records
3. Search record
4. Exit

The structure of file content is: [s_id, name, brand, type, price]

Ans :
import pickle
while True:
print('''
1. Add Record
2. Display Record
3. Search Record
4. Exit
''')
ch=int(input("Enter your choice:"))
l=[]
if ch==1:
f=open("shoes.dat","ab")
s_id=int(input("Enter Shoes ID:"))
name=input("Enter shoes name:")
brand=input("Enter Brand:")
typ=input("Enter Type:")
price=float(input("Enter Price:"))
l=[s_id,name,brand,typ,price]
pickle.dump(l,f)
print("Record Added Successfully.")
f.close()
elif ch==2:
f=open("shoes.dat","rb")
while True:
try:
dt=pickle.load(f)
print(dt)
except EOFError:
break
f.close()
elif ch==3:
si=int(input("Enter shoes ID:"))
f=open("shoes.dat","rb")
flag=False
while True:
try:
dt=pickle.load(f)
for i in dt:
if i==si:
flag=True
print("Record Found...")
print("ID:",dt[0])
print("Name:",dt[1])
print("Brand:",dt[2])
print("Type:",dt[3])
print("Price:",dt[4])
except EOFError:
break
if flag==False:
print("Record not found...")
f.close()
elif ch==4:
break
else:
print("Invalid Choice")

b) Observe the following code and fill in the given blanks as directed: 4M

import mysql.connector as mycon


cn=mycon.connect(_______________________________________) # Statement 1
cr=cn.___________ # Statement 2
cust_id=int(input(“Enter ID:”))
cust_name=input(“Enter Customer Name:”)
city=input(“Enter City:”)
ba=float(input(“Enter Bill Amount:”))
mno=input(“Enter Mobile No.:”)
cr.execute(__________________________________________) # Statement 3
cn._____________ # Statement 4

The partial code is given for inserting a record in customer table created in Rajwadi
Store. The customer table is given as following:
CustomerID CustomerName City BillAmt MobileNo
111 Abhishek Ahmedabad 1500 9999999999
i. Write the parameters and values required to fill statement 1. The parameters
values are as follows:
DatabaseServer User Pasword Database
main_rajwadi rajwadi_admin Rajwadi@2023 rajwadi
ii.Write function name to create cursor and fill in the gap for statement 2.
iii. Write a query to fill statement 3 with desired values.
iv. Write a query to fill statement 4 to save the records into table.

Ans. 1:
mycon.connect(host=”main_rajwadi”, username=”rajwadi_admin”, password=’Rajwadi@2023′,
database=’rajwadi’ )
Ans. 2: cursor
Ans. 3:
“insert into customer values ({},'{}’,'{}’,{},'{}'”.format(cust_id,cust_name,city,ba,mno)
OR
“insert into customer values(%d,’%s’,’%s’,%f,’%s’)”%cust_id,cust_name,city,ba,mno
Ans. 4:
commit()

Q 2) a) Write a menu drive program in python to implement stack using a list and perform
following functions: 8M

1. Push
2. Pop
3. Display
4. Exit

stk=[ ]
top=-1
def isEmpty():
if stk==[]:
print("Stack is Empty")
else:
None
def push():
global stk
global top
n=int(input("Enter value to append:"))
stk.append(n)
top=len(stk)-1
print("Value is Pushed into stack.")

def display():
global stk
global top
if top==-1:
isEmpty()
else:
top=len(stk)-1
print("Top->",stk[top])
for i in range(top-1,-1,-1):
print(stk[i])

def Pop():
global stk
global top
if top==-1:
isEmpty()
else:
print("Elment Popped:",stk.pop())
top=top-1

def menu():
while True:
print('''
1. Push
2. Pop
3. Display
4. Exit
''')
ch=int(input("Enter any number:"))
if ch==1:
push()
elif ch==2:
Pop()
elif ch==3:
display()
elif ch==4:
print("Bye Bye!")
break
else:
print("Invalid choice")

menu()
Q – 3 a) Write a menu drive program to perform following operations into telephone.csv.

1. Add record
2. Display records
3. Search record
4. Exit
The structure of file content is: [s_id, name, brand, type, price]

Ans:
Q – 4 a) WAP a menu-driven program in python to implement following on binary file of the
given structure:
[Doctor_Id, Doctor_Name, Hospital_Id, Joining_Date, Speciality, Salary]

1. Write a record to the file


2. Read all the records to the file
3. Update Record
4. Exit
cur.fetchall()
Q – 5 a) Write a program to create CSV file and store empno, name and salary of employee.
Display the records whose salary in range of 5000 to 15000.

2) The code given below reads the following record from the table named student and
displays only those records who have marks greater than 75:

RollNo – integer
Name – string
Clas – integer
Marks – integer

Note the following to establish connectivity between Python and MYSQL:


Username is root
Password is tiger
The table exists in a MYSQL database named school.

Write the following missing statements to complete the code:


Statement 1 – to create connection
Statement 2 – to form the cursor object
Statement 3 – to execute the query that extracts records of those students whose marks are greater
than 75.
Statement 4- to read the complete result of the query (records whose marks are greater than 75)
into the object named data, from the table student in the database.

import ________________________ #st1


def sql_data():
con1=mysql.connect(host="localhost",user="root",password="tiger",
database="school")
mycursor=_______________ #St2
print("Students with marks greater than 75 are : ")
_________________________ #Statement 3
data=__________________ #Statement 4
for i in data:
print(i)
print()

Ans :

1. mysql.connector as mysql

2. con1.cursor()

3. mycursor.execute(“select & from students where marks>75”)

4. data = mycursor.fetchall()

You might also like