Skip to content
Mentor Subhanu

CBSE Class 12 Computer Science Practical File: Programs With Code

By Subhanu Sankar Roy · · 4 min read

The CBSE Class 12 Computer Science practical file is a record of the programs and SQL queries a student writes through the year. It is marked in the practical exam alongside a lab test, a project and a viva. The CBSE curriculum gives a suggested list of practical programs, and most schools build their list around it. This guide covers those core programs with working Python code a student can type, run and explain.

How the practical marks work

The practical component is worth 30 marks out of 100. In recent years it has been split into a lab test (a Python program and SQL queries), the practical file, the project and the viva. Check the current year's curriculum on the CBSE Academic website for the exact split, because it changes slightly between years.

What matters most: the examiner can ask about any program in your file. Every program should be one the student can explain line by line.

Program 1: Display each word of a text file separated by #

def words_with_hash(filename):
    with open(filename, "r") as f:
        for line in f:
            print("#".join(line.split()))

words_with_hash("story.txt")

Viva question to expect: why use a for loop over the file object instead of read()? Answer: it reads one line at a time, so it works even for very large files.

Program 2: Count vowels, consonants, uppercase and lowercase characters

def count_chars(filename):
    vowels = consonants = upper = lower = 0
    with open(filename, "r") as f:
        for ch in f.read():
            if ch.isalpha():
                if ch.lower() in "aeiou":
                    vowels += 1
                else:
                    consonants += 1
                if ch.isupper():
                    upper += 1
                else:
                    lower += 1
    print("Vowels:", vowels, "Consonants:", consonants)
    print("Uppercase:", upper, "Lowercase:", lower)

count_chars("story.txt")

A common mistake is counting spaces and digits as consonants. The isalpha() check stops that.

Program 3: Move all lines containing 'a' to another file

def move_lines_with_a(src, dest):
    with open(src, "r") as f:
        lines = f.readlines()
    with open(src, "w") as keep, open(dest, "w") as moved:
        for line in lines:
            if "a" in line:
                moved.write(line)
            else:
                keep.write(line)

move_lines_with_a("story.txt", "with_a.txt")

The file is read completely before it is reopened in "w" mode, because "w" empties the file the moment it opens.

Program 4: Binary file of students, search by roll number

import pickle

def write_students(filename):
    records = []
    n = int(input("How many students? "))
    for _ in range(n):
        roll = int(input("Roll number: "))
        name = input("Name: ")
        records.append([roll, name])
    with open(filename, "wb") as f:
        pickle.dump(records, f)

def search_student(filename, roll):
    with open(filename, "rb") as f:
        records = pickle.load(f)
    for r, name in records:
        if r == roll:
            print("Found:", name)
            return
    print("Roll number", roll, "not found")

write_students("students.dat")
search_student("students.dat", int(input("Roll to search: ")))

Program 5: Update marks in a binary file

import pickle

def update_marks(filename, roll, new_marks):
    with open(filename, "rb") as f:
        records = pickle.load(f)  # each record: [roll, name, marks]
    found = False
    for rec in records:
        if rec[0] == roll:
            rec[2] = new_marks
            found = True
    with open(filename, "wb") as f:
        pickle.dump(records, f)
    print("Updated" if found else "Roll number not found")

Some textbooks write one record per pickle.dump() call and read them back in a loop that stops on EOFError. Both approaches are accepted. Use whichever your school teaches, and be ready to explain it.

Program 6: Dice simulator with random numbers

import random

while True:
    print("You rolled", random.randint(1, 6))
    if input("Roll again? (y/n) ").lower() != "y":
        break

Viva question to expect: is 6 included? Yes. randint(a, b) includes both ends, unlike randrange(1, 6).

Program 7: Stack using a list

stack = []

def push(item):
    stack.append(item)

def pop():
    if not stack:
        return "Stack underflow"
    return stack.pop()

def peek():
    return stack[-1] if stack else None

def display():
    for item in reversed(stack):
        print(item)

Know the terms: LIFO (last in, first out), top, push, pop, and underflow (popping from an empty stack).

Program 8: CSV file of user IDs and passwords

import csv

def create_users(filename):
    with open(filename, "w", newline="") as f:
        w = csv.writer(f)
        w.writerow(["user_id", "password"])
        while True:
            uid = input("User ID (blank to stop): ")
            if not uid:
                break
            w.writerow([uid, input("Password: ")])

def find_password(filename, uid):
    with open(filename, "r", newline="") as f:
        r = csv.reader(f)
        next(r)  # skip the header row
        for row in r:
            if row[0] == uid:
                return row[1]
    return None

newline="" stops blank lines appearing between rows on Windows, which is a common viva question.

SQL queries for the practical file

The suggested list asks for a table on which students practise:

  • CREATE TABLE and INSERT to add records.
  • ALTER TABLE to add a column, change a data type and drop a column.
  • UPDATE to change data, and DELETE to remove rows.
  • ORDER BY to sort in ascending and descending order.
  • GROUP BY with MIN, MAX, SUM, COUNT and AVG.

There is also Python–MySQL connectivity: running these queries from a Python program. Both are covered in detail in our guides to Class 12 SQL queries and to Python–MySQL connectivity.

Tips for the viva

  • Type and run every program yourself. Copied code is easy to spot when a student can't explain it.
  • Know the file modes: r, w, a, rb, wb, ab, and what the + versions do.
  • Be ready to change a program on the spot, for example "now count only lowercase vowels".
  • Keep sample input and output with each program in the file.

When to start

Write each program when the topic is taught, not in January. A student who builds the file through the year has already revised half the practical exam by the time it arrives.