CBSE Class 12 – Python Programs Collection
This document contains all the Python programs discussed in this chat, written at Grade 12 CBSE level, with
clear structure and comments.
1. Menu-Based Program Using Stack (Hostel Records)
Fields:
• Hostel Number
• Total Students
• Total Rooms
# Menu based program to manage hostel records using stack
stack = [] # stack to store hostel records
def add_record():
hostel_no = int(input("Enter Hostel Number: "))
students = int(input("Enter Total Students: "))
rooms = int(input("Enter Total Rooms: "))
record = [hostel_no, students, rooms]
[Link](record)
print("Record added successfully.\n")
def delete_record():
if stack == []:
print("Stack is empty. No record to delete.\n")
else:
hostel_no = int(input("Enter Hostel Number to delete: "))
found = False
for record in stack:
if record[0] == hostel_no:
[Link](record)
print("Deleted Record:", record, "\n")
found = True
break
if found == False:
print("Record not found.\n")
def display_records():
1
if stack == []:
print("No records to display.\n")
else:
print("Hostel Number Total Students Total Rooms")
for record in reversed(stack):
print(record[0], " " * 13, record[1], " " * 13, record[2])
print()
while True:
print("MENU")
print("1. Add Hostel Record")
print("2. Delete Hostel Record")
print("3. Display Hostel Records")
print("4. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
add_record()
elif choice == 2:
delete_record()
elif choice == 3:
display_records()
elif choice == 4:
print("Exiting program.")
break
else:
print("Invalid choice. Try again.\n")
2. Binary File Program – [Link]
Structure:
[BookNo, Book_Name, Author, Price]
import pickle
def createfile():
with open("[Link]", "ab") as file:
book_no = input("Enter Book Number: ")
book_name = input("Enter Book Name: ")
author = input("Enter Author Name: ")
price = input("Enter Price: ")
2
book = {
"BookNo": book_no,
"Book_Name": book_name,
"Author": author,
"Price": price
}
[Link](book, file)
print("Record added successfully!\n")
for i in range(5):
print(f"Enter details for Book {i+1}")
createfile()
3. CSV File Program – [Link]
Task:
• Store product records
• Display products with price > 300
import csv
def create_csv():
with open("[Link]", "w", newline="") as file:
writer = [Link](file)
[Link](["Product_id", "product_name", "price"])
for i in range(5):
print(f"Enter details for product {i+1}")
pid = input("Product_id: ")
pname = input("Product_Name: ")
price = input("Price: ")
[Link]([pid, pname, price])
def display_products():
result = []
with open("[Link]", "r") as file:
reader = [Link](file)
for row in reader:
if row[0] == "Product_id":
[Link](row)
else:
if int(row[2]) > 300:
[Link](row)
3
print(result)
create_csv()
display_products()
4. Text File Program – Vowel Words Copy
Task:
• Store input in [Link]
• Copy words starting with vowels into [Link]
def create_data_file():
with open("[Link]", "w") as file:
for i in range(5):
line = input(f"Line {i+1}: ")
[Link](line + "\n")
def copy_vowel_words():
vowels = "AEIOUaeiou"
with open("[Link]", "r") as infile, open("[Link]", "w") as outfile:
for line in infile:
words = [Link]()
for word in words:
if word[0] in vowels:
[Link](word + " ")
def display_story():
with open("[Link]", "r") as file:
print([Link]())
create_data_file()
copy_vowel_words()
display_story()
End of Document
All programs are written strictly at CBSE Class 12 level, suitable for practical exams and viva.