code
code
class Stack:
def __init__(self):
self.items = []
self.items.append(item)
# 2. Pop(): Removes the top item from the stack and displays the item.
def Pop(self):
if not self.IsEmpty():
return self.items.pop()
else:
# 3. Display(): Displays all the items currently in the stack, starting from the top.
def Display(self):
if not self.IsEmpty():
print(item)
else:
print("Stack is empty")
# 4. IsEmpty(): Returns True if the stack is empty, otherwise False.
def IsEmpty(self):
return len(self.items) == 0
def stack_operations():
stack = Stack()
while True:
print("\nStack Operations:")
print("1. Push(item)")
print("2. Pop()")
print("3. Display()")
print("4. IsEmpty()")
print("5. Exit")
if choice == '1':
stack.Push(item)
popped_item = stack.Pop()
print(popped_item)
stack.Display()
if stack.IsEmpty():
else:
break
else:
stack_operations()
SET2
import pickle
class Peripheral:
self.name = name
self.category = category
self.price = price
def __str__(self):
def ADD():
try:
pickle.dump(peripheral, file)
except Exception as e:
def DISPLAY():
try:
with open("PERIPHERALS.dat", "rb") as file:
found = False
while True:
try:
peripheral = pickle.load(file)
print(peripheral)
found = True
except EOFError:
break
if not found:
except Exception as e:
# Function to search for a peripheral by PID that falls under the Output category
def SEARCH(pid):
try:
found = False
while True:
try:
peripheral = pickle.load(file)
break
except EOFError:
break
if not found:
except Exception as e:
# Menu-driven program
def menu():
while True:
print("\nMenu:")
print("3. SEARCH - Search for a peripheral with PID and category Output")
print("4. Exit")
if choice == "1":
ADD()
DISPLAY()
break
else:
if __name__ == "__main__":
menu()
SET 3
import pickle
# Define a class to store student data
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
def __str__(self):
return f"{self.name}: {self.marks}"
# Function to write student data to binary file
def write_to_file(filename):
num_students = int(input("Enter the number of
students: "))
set 4
import pickle
# Menu-driven program
def menu():
while True:
print("\nMenu:")
print("1. ADD - Add a new record")
print("2. DISPLAY - Display all peripherals with
Price > 15000")
print("3. SEARCH - Search for a peripheral with
PID and category Output")
print("4. Exit")
if choice == "1":
ADD()
elif choice == "2":
DISPLAY()
elif choice == "3":
pid = input("Enter Peripheral ID (PID) to
search: ")
SEARCH(pid)
elif choice == "4":
print("Exiting the program.")
break
else:
print("Invalid choice. Please try again.")