CBSE Class 12 Computer Science: Python Functions, Scope and File Handling with Examples

Functions and file handling run through CBSE Class 12 Computer Science, from short output-prediction questions to programs you write from scratch. Students usually lose marks by predicting output wrongly when a local variable hides a global one, or by forgetting how default arguments and return values behave. Every program below was run in Python 3, and the output shown is what it actually printed.
Defining a function with a default argument
A function can return more than one value as a tuple, and a parameter can have a default value that is used when no argument is passed. In the programs below, each bullet is one line of code, and the note in front of it tells you how far to indent it when you type it out.
- def area_and_perimeter(length, width=1):
- Indented one level inside the function: return length * width, 2 * (length + width)
- Back at the left margin: a, p = area_and_perimeter(5, 3)
- print(a, p) prints 15 16
- print(area_and_perimeter(4)) prints (4, 10)
In the second call, width takes its default value 1, and the two returned values come back as a tuple. Parameters with defaults must come after parameters without them: def f(x=1, y) is a syntax error.
Positional and keyword arguments
With def f(x, y=2): return x ** y, the calls f(3), f(2, 3) and f(y=1, x=7) print 9, 8 and 7. Keyword arguments are matched by name, so their order does not matter.
Local and global scope
A variable assigned inside a function is local to that function unless it is declared global. Predict the output of this program before reading the answer.
- count = 10
- def show():
- Indented inside show(): count = 5
- Indented inside show(): print("inside:", count)
- def increase():
- Indented inside increase(): global count
- Indented inside increase(): count = count + 1
- Back at the left margin: show(), then print("outside:", count), then increase(), then print("after increase:", count)
Output: inside: 5, then outside: 10, then after increase: 11. In show(), count = 5 creates a new local variable and leaves the global one unchanged. In increase(), the global statement makes the assignment change the global count.
A short file-handling program
Write a function that counts the lines in a text file containing a given word, ignoring case.
- def count_lines_with(filename, word):
- Indented one level: total = 0
- Indented one level: with open(filename, "r") as f:
- Indented two levels: for line in f:
- Indented three levels: if word.lower() in line.lower():
- Indented four levels: total += 1
- Indented one level, after the with block ends: return total
If notes.txt contains the three lines "Python is fun", "Functions keep code tidy" and "I revise Python daily", then count_lines_with("notes.txt", "python") returns 2. The with statement closes the file automatically, even if an error occurs while reading.
A short self-test
- With def f(x, y=2): return x ** y, what does print(f(3)) show? (Answer: 9.)
- Why is def g(a=1, b): invalid? (Answer: a parameter without a default cannot follow one with a default.)
- In the scope program above, what would print("outside:", count) show if show() also contained global count? (Answer: outside: 5.)
Check the current units and practical requirements in the CBSE 2026–27 curriculum, and type each program out and run it rather than only reading it. If output prediction or file programs keep catching you out, a CBSE Class 12 Computer Science tutor can work through your own code with you.
