Computer Science Class 12: Python Programs That Repeat in Board Exams
Every year, the CBSE and state board Computer Science practical and theory papers feature a core set of Python programming patterns. Examiners repeatedly test fundamental logic rather than obscure syntax. If you master text file manipulation, binary files using pickle, CSV reading/writing, and basic stack operations, you can secure full marks in the programming section.
Let us break down the exact program structures that appear consistently across board papers under the 2026-27 curriculum guidelines.
1. Text File Handling: Counting and Filtering
Text file questions usually ask you to read a .txt file line by line or character by character, perform a conditional check, and display or write the result.
Common variations include:
- Counting specific vowels, uppercase characters, or words starting with a specific letter.
- Displaying lines that do not contain a particular word (e.g., "the").
- Copying specific lines from one file to another.
Example: Count Specific Words and Vowels
def count_file_stats():
file = open("story.txt", "r")
vowels = "AEIOUaeiou"
vowel_count = 0
the_count = 0
for line in file:
# Count vowels
for char in line:
if char in vowels:
vowel_count += 1
# Count occurrences of the word 'the' (case-insensitive)
words = line.split()
for w in words:
if w.lower() == "the":
the_count += 1
file.close()
print(f"Total Vowels: {vowel_count}")
print(f"Total 'the': {the_count}")
Exam Tip: Always remember to call file.close() or use a with context manager. Missing file closure can cost you marks in school practical evaluations.
2. Binary Files Using pickle
Binary file handling using the pickle module is a high-yield topic. Examiners test your ability to append records (usually dictionaries or lists), search for a specific record by roll number or ID, and update records.
Frequently Asked Functions
pickle.dump(data, file): To write data.pickle.load(file): To read data inside atry-except EOFErrorblock.
Example: Search Record in a Binary File
import pickle
def search_student(rno):
found = False
try:
with open("student.dat", "rb") as file:
while True:
record = pickle.load(file)
if record["RollNo"] == rno:
print(f"Found! Name: {record['Name']}, Marks: {record['Marks']}")
found = True
break
except EOFError:
pass
if not found:
print("Record not found.")
3. CSV File Operations
CSV (Comma-Separated Values) handling requires the csv module. Questions typically test writing rows using csv.writer() and reading them using csv.reader().
Example: Write and Read Employee CSV Data
import csv
def write_csv():
with open("emp.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["EmpID", "Name", "Salary"])
writer.writerow([101, "Aarav", 55000])
writer.writerow([102, "Diya", 62000])
def read_csv():
with open("emp.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
# row is a list of strings
if len(row) > 0 and row[0] != "EmpID":
if int(row[2]) > 60000:
print(row[1], "gets salary above 60000")
4. Stack Implementation Using Lists
Data structures—specifically stacks—are a mandatory long-answer question. You are expected to implement a stack for a list of dictionaries (like books or employee records).
Essential Stack Operations Table
| Operation | Function Name | Logic |
| :--- | :--- | :--- |
| Push | Push(Stack, item) | Stack.append(item) |
| Pop | Pop(Stack) | Check if empty, else Stack.pop() |
| Display | Peek(Stack) | Check if empty, else print Stack[-1] |
Example: Stack Push and Pop for Book Records
def push_book(stack, book):
stack.append(book)
print("Book pushed successfully.")
def pop_book(stack):
if len(stack) == 0:
print("Stack Underflow! No books to remove.")
else:
removed = stack.pop()
print(f"Removed Book: {removed['Title']} by {removed['Author']}")
Quick Checklist for Python Programs
- [ ] Did you import required modules (
pickle,csv,math) at the top? - [ ] Are file open modes correct? (
"r","w","rb","wb","a") - [ ] Did you handle
EOFErrorwhen reading binary files with awhile Trueloop? - [ ] Is
newline=""included when opening CSV files for writing? - [ ] Did you check for Stack Underflow (
len(stack) == 0) before popping elements?
Practice these four patterns repeatedly on Edumath by sereddy. Writing out the syntax without an IDE during your revision will ensure zero silly mistakes in the final theory paper.