Coding by Kasthuri Muthu
Class 12 Computer Science – Discussion Notes
Q1. File Handling
Content of file:
This is a test.
Isn't it? I think so.
Code:
file = open("samp.txt","r")
s = file.readline().split()
print(s[0][-1])
👉 file.readline() reads first line = "This is a test.\n"
👉 .split() → ["This", "is", "a", "test."]
👉 s[0] = "This"
👉 s[0][-1] = "s"
✅ Answer: s
Q2. Dictionary with ASCII
d = {ch: ord(ch) for ch in "AEIOU"}
print(d)
✅ Answer: {'A':65, 'E':69, 'I':73, 'O':79, 'U':85}
Q3. Relative Path
File path:
D:\Program\Project\Railway\Reservation.py
If current directory = Project → relative path:
✅ Answer: Railway/Reservation.py
Q4. Expression
7/5 + 10**2 - (25+12*2)/4
Step:
7/5 = 1.4
10**2 = 100
12*2 = 24 → 25+24 = 49 → 49/4 = 12.25
1.4 + 100 - 12.25 = 89.15
✅ Answer: 89.15
Q5. Loop Output
str1 = "hello"
c = 0
for x in str1:
if x != 'l':
c += 1
else:
continue
print(c)
👉 "hello" has 5 characters.
👉 2 are 'l', so counted = 3.
✅ Answer: 3
Q6. File Binary Mode
f = open("DATA.TXT", "rb")
Q7. Function Prod
def Prod(v):
print(v*v)
print(Prod(2))
Step:
Inside function: 2*2=4 → prints 4
Function returns None → so print(Prod(2)) also prints None
✅ Answer:
4
None
Q8. Function CALLME
def CALLME(M,n1=1,n2=2):
n1=n1*n2
M.append(n1)
n2+=2 → n2 =4
M.insert(n1,n2)
print(M,n1,n2)
D=[10,20,30,45,60,75,90,120]
CALLME(D);
CALLME(D,3)
Step:
n1=3*2=6
Append 6 → [10,20,30,45,60,75,90,120,6]
n2=4
Insert 4 at index 1 → [10,4,20,30,45,60,75,90,120,6]
✅ Answer:
[10,20,4,30,45,60,75,90,120,2] 2 4
[10,4,20,30,45,60,75,90,120,6] 6 4
Q9. Identifiers
Valid: Emp_code, bonus, Emp1, Bond007
Invalid: While (keyword), for (keyword), #count (illegal), 123Go (digit start)
Q10. Jumble Function
msg = "Butterfly"
Final output (after processing each char) =
✅ Answer:
Original: Butterfly
Final: CUuTfRgLz
Q11. Error
👉 No line in this program gives error.
✅ Answer: No error.
Q12–14. File Programs
👉 They ask to read, copy, and process text files (like NEWS.TXT).
Use with open("file.txt") as f:
Use readlines(), split(), isupper(), write().
(We can write model answers if needed.)
Q15. Function with list
def ZeroEnding(SCORES):
add = 0
for x in SCORES:
if str(x)[-1]=='0':
add+=x
print(add)
👉 For [200,456,300,100,234,678] → 200+300+100=600
✅ Answer: 600
Q16(i). Global/Local
value = 50
def display(N):
global value
value = 25
if N%2==0:
value = value + N
else:
value = value - N
print(value, end="#")
display(20)
print(value)
Step:
global value=25
20%2==0 → True → value=25+20=45
Prints 45#
After function call → global value=45
✅ Answer: 45#45
Q16(ii). Random Output
👉 Since randint(0,1) + randint(1,2)+1, final selections depend on random.
✅ Possible outputs given:
[G,'Green'], [G,'Green'], [Y,'Yellow']
[G,'Green'], [B,'Blue'], [G,'Green']
[V,'Violet'], [B,'Blue'], [B,'Blue']
[I,'Indigo'], [B,'Blue'], [B,'Blue']
Q17. Binary File Functions
(i) Enter record:
import pickle
def writeItem():
f=open("Itemdetails.dat","ab")
Itemno=int(input("Enter Item No: "))
ItemName=input("Enter Item Name: ")
Quantity=int(input("Enter Quantity: "))
Price=int(input("Enter Price per Item: "))
rec=[Itemno,ItemName,Quantity,Price]
pickle.dump(rec,f)
f.close()
(ii) Display details for quantity=10:
def readItem():
f=open("Itemdetails.dat","rb")
try:
while True:
rec=pickle.load(f)
if rec[2]==10:
print("Item Name:",rec[1])
print("Quantity:",rec[2])
print("Total Price:",rec[2]*rec[3])
except EOFError:
f.close()
Q18. CSV File
CSV file data:
1,Peter,3500
2,Scott,4000
5,Sam,4200
Python function to read & display:
import csv
def readcsv():
f=open("emp.csv","r")
data=csv.reader(f)
for row in data:
print(row)
f.close()
with open("file1.txt", "r") as file1:
words_file1 = file1.read().split()
# Read file2.txt
with open("file2.txt", "r") as file2:
words_file2 = file2.read().split()
# Find common words
common_words = []
for word in words_file1:
if word in words_file2 and word not in common_words:
common_words.append(word)
# Write common words to Combinedfile.txt
with open("Combinedfile.txt", "w") as combined_file:
for word in common_words:
combined_file.write(word + "\n")
print("Common words have been written to Combinedfile.txt")


