Posted on Leave a comment

Notes for Class12 Computer Science Students

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")

Leave a Reply