Document PDF
Document PDF
CODE:
import mysql.connector
if result:
price = result[0]
total_price = price * quantity
query = "INSERT INTO orders (customer_name, item_id,
quantity, total_price) VALUES (%s, %s, %s, %s)"
cursor.execute(query, (customer_name, item_id, quantity,
total_price))
print(f"Order placed successfully. Total price: $
{total_price:.2f}")
else:
print("Invalid item ID.")
else:
print("No orders placed yet.")
def main():
db = connect_to_db()
cursor = db.cursor()
while True:
print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
=-")
print(" RESTAURANT MANAGEMENT SYSTEM ")
print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
=")
if choice == '1':
break
else:
print("Invalid choice. Please try again.")
cursor.close()
db.close()
if __name__ == "__main__":
main()
PROGRAM 10
A binary file “book.dat” has structure [BookNo,Book_Name,Author,Price]
def create_file():
f=open("book.dat",'wb')
l=['book_no','book_name','author','price']
f.close()
def count_rec():
f=open("book.dat",'rb')
c=0
try:
while True:
l=pickle.load(f)
if l[2]==author:
c+=1
except EOFError:
f.close()
create_file()
count_rec(JamesClear)
OUTPUT:
PROGRAM 11
Write a python Function 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.
import pickle
def write():
D={}
f=open("Student.dat","wb")
while True:
Data=[r,n]
pickle.dump(Data,f)
if ch in 'Nn':
break
f.close()
def search() :
c=0
f = open("Student.dat", "rb")
try:
while True:
rec=pickle.load(f)
if rec[0]==rollno:
print(rec[1])
c=1
break
except EOFError:
f.close()
if c==0:
write()
search()
OUTPUT:
PROGRAM 12
A binary file named “TEST.dat” has some records of structure
subject=input("Enter subject")
[testId,subject,Mmrk,Smrk]
pickle.dump(l,f)
f.close()
def display():
Write()
display()
OUTPUT:
PROGRAM 13
Write a python function to function with key and value , and update value at that key in
dictionary entered by user.
OUTPUT:
PROGRAM 14
Write a function to know the cursor position and print the text according to below-given
specifications:
a) Print the initial position. b) Move the cursor to 4th position. c) Display next 5
characters. d) Print the cursor to the next 10 characters.\ e) Print the current cursor
print(f.tell())(f.seek(4,0))
print(f.read(5)) (f.seek(10,0))
print(f.tell())
print(f.seek(7,0))
print(f.read(10))
OUTPUT:
PROGRAM 15
Write a python function to impliment a stsck using list(PUSH & POP Operation on stack)
def push():
global stk
global top
empno=int(input("Enter the employee number to push:"))
stk.append([empno,ename])
top=len(stk)-1
def pop_ele():
global stk
global top
if top==-1:
isEmpty()
else:
e= stk.pop()
print("Element popped:",e)
top=top-1
def main_menu():
while True:
print("\n Stack Implementing for the employee details")
print("1. Push")
print("2. Pop")
print()
if ch==1:
push()
print("Element Pushed")
elif ch==2:
pop_ele()
else:
print("Invalid Choice")
stk=[]
top=-1
main_menu()
PROGRAM 16
A dictionary that stores Product names as key and price as values.
Write a user-defined function to perform the following operations: a) Push the values into
the stack if the price of products is greater than 300. b) Pop and display the content of the
PROGRAM 17
Write a python program using function POP(Arr) , where Arr is a stack implemented by a list
of numbers. The function returns the value delete from the stack.
def pop(Arr):
top=len(Arr)-1
if len(Arr)!=0:
deleted_val=Arr[top]
Arr.pop()
else:
print("stack is emply")
return deleted_val
Arr=[8,56,98]
del_Arr=pop(Arr)
print(del_Arr)
OUTPUT:
PROGRAM 18
Prateek has created a list arr with some elements. Help him to create a user-defined
function to perform the following tasks:
a) Create a stack after checking the elements if it is even then multiply by 2 and if the
element is odd , then multiply by 3.
b) Pop and display the contents of the stack.
stack=[]
arr=[2,3,4,5,6,7,8,9,10]
def push():
for i in arr:
if i%2==0:
stack.append(2*i)
else:
stack.append(3*i)
print(stack)
push()
print(stack)
def pop():
if stack==[]:
return 0
else:
return stack.pop()
while True:
value=pop()
if value==0:
print("underflow")
break
else:
print(value,end=" , ")
OUTPUT:
PROGRAM 19
Integrate MySQL with Python by importing the MySQL module and add
records of students and display all the record.
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",password="s123",
database="School")
cursor = mydb.cursor()
def add_student():
mydb.commit()
cursor.execute(query)
result = cursor.fetchall()
add_student()
display_students()
OUTPUT:
PROGRAM 20
Integrate SQL with Python by importing the MySQL module to search a student using
rollno , update the record.
import mysql.connector
mydb=mysql.connector.connect(host="localhost",
user="root",
password="s123",
database="school")
print("Successfully Connected")
mycur=mydb.cursor()
def search():
c=0
print("updated record")
mydb.commit()
search()
OUTPUT:
Thank
you
Class 12 (Nikola Tesla)