SET–1
Q1 (a) Stack – Products
Program
# Stack program for Products
L = [("Laptop", 90000), ("Mobile", 30000), ("Pen", 50), ("Headphones", 1500)]
Products = []
def Push_element():
for item in L:
if item[1] > 50:
Products.append(item)
print("After Push:", Products)
def Pop_element():
if len(Products) == 0:
print("Stack Empty")
else:
while Products:
print(Products.pop())
print("Stack Empty")
def Show_stack():
print("Current Stack:")
for i in range(len(Products)-1,-1,-1):
print(Products[i])
# Menu driven
while True:
print("\n1.Push\n2.Pop\n3.Show\n4.Exit")
ch = int(input("Enter choice: "))
if ch == 1:
Push_element()
elif ch == 2:
Pop_element()
elif ch == 3:
Show_stack()
elif ch == 4:
break
else:
print("Invalid choice")
Q1 (b) Binary File – records.dat
import pickle
def write_data():
f = open("records.dat", "wb")
for i in range(4):
roll = int(input("Enter roll: "))
name = input("Enter name: ")
address = input("Enter address: ")
stu = {"roll": roll, "name": name, "address": address}
pickle.dump(stu, f)
f.close()
print("Record saved.")
def show_data():
try:
f = open("records.dat", "rb")
while True:
data = pickle.load(f)
print(data)
except EOFError:
f.close()
write_data()
show_data()
Q2 SQL Queries (SALES Table)
I. Total quantity sold for each product whose total exceeds 12
SELECT Product, SUM(Quantity)
FROM SALES
GROUP BY Product
HAVING SUM(Quantity) > 12;
II. Sort SALES table by Product name (descending)
SELECT * FROM SALES
ORDER BY Product DESC;
III. Display distinct product names
SELECT DISTINCT Product FROM SALES;
IV. Customers whose names end with ‘e’
SELECT * FROM SALES
WHERE Customer LIKE '%e';
SET–2
Q1 (a) Stack – Toppers
Marks_List = [68,45,85,27,26,48,26,75,16,48]
Toppers = []
def Push_element():
for m in Marks_List:
if m > 65:
Toppers.append(m)
print("After Push:", Toppers)
def Pop_element():
if len(Toppers) == 0:
print("Stack Empty")
else:
while Toppers:
print(Toppers.pop())
print("Stack Empty")
def Show_stack():
print("Current Stack:")
for i in range(len(Toppers)-1,-1,-1):
print(Toppers[i])
while True:
print("\n1.Push\n2.Pop\n3.Show\n4.Exit")
ch = int(input("Enter choice: "))
if ch == 1:
Push_element()
elif ch == 2:
Pop_element()
elif ch == 3:
Show_stack()
elif ch == 4:
break
Q1 (b) Binary File – students.dat
import pickle
def write_data():
f = open("students.dat", "wb")
for i in range(4):
roll = int(input("Enter roll: "))
name = input("Enter name: ")
marks = int(input("Enter marks: "))
stu = {"roll": roll, "name": name, "marks": marks}
pickle.dump(stu, f)
f.close()
print("Record saved.")
def Show_toppers():
try:
f = open("students.dat", "rb")
while True:
s = pickle.load(f)
if s["marks"] > 50:
print(s)
except EOFError:
f.close()
write_data()
Show_toppers()
Q2 SQL Queries
I. Total quantity for each product excluding quantity < 5
SELECT Product, SUM(Quantity)
FROM SALES
GROUP BY Product
HAVING SUM(Quantity) >= 5;
II. Orders sorted by total price (descending)
SELECT * FROM Orders
ORDER BY Total_Price DESC;
III. Distinct customer names
SELECT DISTINCT Customer_Name FROM Orders;
IV. Sum of price where quantity is NULL
SELECT SUM(Price)
FROM Orders
WHERE Quantity IS NULL;