Suggested Practical List Solution of CLASS XII COMPUTER SCIENCE 2021-22 (CBSE)

Suggested Practical List Solution of CLASS XII COMPUTER SCIENCE 2021-22  (CBSE)
Computer Science
CLASS-XII
Subject Code 083
 
Solution of suggested practical list for Term-I

◘ Read a text file line by line and display each word separated by a ‘#’

filein = open("mydoc.txt",'r')
line =" "
while line:
     line = filein.readline()
     #print(line)
     for w in line:
          if w == ' ':
               print('#',end = '')
          else:
               print(w,end = '')
filein.close()

◘ Read a text file and display the number of vowels/consonants/uppercase/lowercase characters in the file.

def cnt():
    f=open("D:\test2.txt","r")
    cont=f.read()
    print(cnt)
    v=0
    cons=0
    l_c_l=0
    u_c_l=0
    for ch in cont:
        if (ch.islower()):
            l_c_l+=1
        elif(ch.isupper()):
            u_c_l+=1
        ch=ch.lower()
        if( ch in ['a','e','i','o','u']):
            v+=1
        elif (ch in ['b','c','d','f','g',
                     'h','j','k','l','m',
                     'n','p','q','r','s',
                     't','v','w','x','y','z']):
            cons+=1
    f.close()
    print("Vowels are : ",v)
    print("consonants are : ",cons)
    print("Lower case letters are : ",l_c_l)
    print("Upper case letters are : ",u_c_l)      
#main program
cnt()

◘ Remove all the lines that contain the character ‘a’ in a file and write it to another file.

with open(“file1.txt”) as r:
   rr=r.readlines()
   k=[]
   for i in rr:
       if i==”a”:
           k.append(i)
with open(“file2.txt”,”w”) as w:
   for q in k:
       w.write(str(q))

◘ Create a binary file with name and roll number. Search for a given roll number and display the name, if not found display appropriate message.

import pickle
import sys
dict={}
def write_in_file():
    file=open("D:\stud2.dat","ab")  #a-append,b-binary
    no=int(input("ENTER NO OF STUDENTS: "))
    for i in range(no):
        print("Enter details of student ", i+1)
        dict["roll"]=int(input("Enter roll number: "))
        dict["name"]=input("enter the name: ")
        pickle.dump(dict,file)  #dump-to write in student file
    file.close()
 
def display():
    #read from file and display
    file=open("D:\stud2.dat","rb")   #r-read,b-binary
    try:
        while True:
            stud=pickle.load(file)   #write to the file
            print(stud)
    except EOFError:
        pass
    file.close()
 
def search():
    file=open("D:\stud2.dat","rb")   #r-read,b-binary
    r=int(input("enter the rollno to search: "))
    found=0
    try:
        while True:
            data=pickle.load(file)   #read from file
            if data["roll"]==r:
                print("The rollno =",r," record found")
                print(data)
                found=1
                break
    except EOFError:
        pass
    if found==0:
        print("The rollno =",r," record is not found")
    file.close()
 
while True:       
    print("MENU n 1-Write in a file n 2-display ")
    print(" 3-searchn 4-exit n")
    ch=int(input("Enter your choice = "))
    if ch==1:
        write_in_file()
    if ch==2:
        display()
    if ch==3:
        search()
    if ch==4:
        print(" Thank you ")
        sys.exit()   

◘ Create a binary file with roll number, name and marks. Input a roll number and update the marks.

import pickle
#creating the file and writing the data
f=open("records.dat", "wb")
#list index 0 = roll number
#list index 1 = name
#list index 2 = marks
pickle.dump([1, "Anurag", 90], f)
pickle.dump([2, "Singh", 80], f)
pickle.dump([3, "Priyashi", 90], f)
pickle.dump([4, "Aman", 80], f)
pickle.dump([5, "Ankit", 85], f)
f.close()

#opeining the file to read contents
f=open("records.dat", "rb")
roll=int(input("Enter the Roll Number: "))
marks=float(input("Enter the updated marks: "))
List = [ ] #empty list
flag = False #turns to True if record is found
while True:
    try:
        record=pickle.load(f)
        List.append(record)
    except EOFError:
        break
f.close()

#reopening the file to update the marks
f=open("records.dat", "wb")
for rec in List:
    if rec[0]==roll:
        rec[2] = marks
        pickle.dump(rec, f)
        print("Record updated successfully")
        flag = True
    else:
        pickle.dump(rec,f)
f.close()
if flag==False:
    print("This roll number does not exist")

◘ Write a random number generator that generates random numbers between 1 and 6 (simulates a dice).

def random():
    import random
    s=random.randint(1,6)
    return s
print(random()) 

◘ Create a CSV file by entering user-id and password, read and search the password for given user- id.

import csv
# User-id and password list
List = [["user1", "password1"],
        ["user2", "password2"],
        ["user3", "password3"],
        ["user4", "password4"],
        ["user5", "password5"]]
# opening the file to write the records
f1=open("UserInformation.csv", "w", newline="n")
writer=csv.writer(f1)
writer.writerows(List)
f1.close()

# opening the file to read the records
f2=open("UserInformation.csv", "r")
rows=csv.reader(f2)
userId = input("Enter the user-id: ")
flag = True
for record in rows:
    if record[0]==userId:
        print("The password is: ", record[1])
        flag = False
        break
if flag:
    print("User-id not found")

• All the best for the BOARD EXAM

Thanks for being here, stay connect with us.

Leave a Comment