Finall Class 12th CS Practcal File by Manthan
Finall Class 12th CS Practcal File by Manthan
SESSION:-2022-2023
Computer Science
PRACTICAL FILE
(CODE-083)
_____________
Principal
INDEX
2 Computer Networks 10 15 —
3 Database Management 20 25 20
Total 70 110 70
Practical
2 Report file: 4
1. Minimum 15 Python programs.
2. SQL Queries – Minimum 5 sets using one table /
two tables.
3. Minimum 4 programs based on Python - SQL
connectivity
4 Viva voce 3
Acknowledgement
1. Read a text file line by line and display each word separated by a '#' in
Python
Procedure:
1. Open a file in read mode which contains a string.
2. Use for loop to read each line from the text file.
3. Again use for loop to read each word from the line splitted by ‘ ‘.
4. Display each word seperated by '#' from each line in the text file.
f = open('demo1.txt','r')
for line in f:
word = line.split()
for w in word:
print(w + '#',end ='')
print()
f.close()
Or
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()
OUTPUT:-
Twinkle,#twinkle,#little#star,
How#I#wonder#what#you#are!
Up#above#the#world#so#high,
Like#a#diamond#in#the#sky.
When#the#blazing#sun#is#gone,
When#he#nothing#shines#upon,
Then#you#show#your#little#light,
Twinkle,#twinkle,#all#the#night.
def cnt():
f=open("demo1.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()
OUTPUT:-
Vowels are : 64
consonants are : 118
Lower case letters are : 173
Upper case letters are : 9
3. Remove all the lines that contain the character 'a' in a file and
write it to another file.
Program Logic:
Open input file say ‘assignment.txt’ in read mode and store in temporary
file object
say ‘input_file’
Open output file say ‘dataoutput.txt’ in write mode and store in
temporary file object
say ‘output_file’
Read the contents of input file using readlines()
Iterate through input file using for loop
Within for loop, if statement is used to check input file contains the
character ‘a’ or
not
Write only those lines that does not contain character ‘a’ in output file
using write()
Close all input and output file
fo=open("hp.txt","w")
fo.write("Harry Potter")
fo.close()
fo=open('hp.txt','r')
fi=open('writehp.txt','w')
l=fo.readlines()
for i in l:
if 'a' in i:
i=i.replace('a','')
fi.write(i)
fi.close()
4. 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, "Wakil", 90], f)
pickle.dump([2, "Tanish", 80], f)
pickle.dump([3, "Priyashi", 90], f)
pickle.dump([4, "Kanupriya", 80], f)
pickle.dump([5, "Ashutosh", 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")
def random():
import random
s=random.randint(1,6)
return s
print(random())
Problem Description
The program creates a stack and allows the user to perform push and pop
operations on it.
Problem Solution
1. Create a class Node with instance variables data and next.
2. Create a class Stack with instance variable head.
3. The variable head points to the first element in the linked list.
4. Define methods push and pop inside the class Stack.
5. The method push adds a node at the front of the linked list.
6. The method pop returns the data of the node at the front of the linked
list and removes the node. It returns None if there are no nodes.
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Stack:
def __init__(self):
self.head = None
def pop(self):
if self.head is None:
return None
else:
popped = self.head.data
self.head = self.head.next
return popped
a_stack = Stack()
while True:
print('push <value>')
print('pop')
print('quit')
do = input('What would you like to do? ').split()
operation = do[0].strip().lower()
if operation == 'push':
a_stack.push(int(do[1]))
elif operation == 'pop':
popped = a_stack.pop()
if popped is None:
print('Stack is empty.')
else:
print('Popped value: ', int(popped))
elif operation == 'quit':
break
8. Create a CSV file by entering user-id and password, read and search
the password for given user id.
import csv
with open("7.csv", "w") as obj:
fileobj = csv.writer(obj)
fileobj.writerow(["User Id", "password"])
while(True):
user_id = input("enter id: ")
password = input("enter password: ")
record = [user_id, password]
fileobj.writerow(record)
x = input("press Y/y to continue and N/n to terminate the
program\n")
if x in "Nn":
break
elif x in "Yy":
continue
with open("7.csv", "r") as obj2:
fileobj2 = csv.reader(obj2)
given = input("enter the user id to be searched\n")
for i in fileobj2:
next(fileobj2)
# print(i,given)
if i[0] == given:
print(i[1])
break
stk=[]
top=-1
def line():
print('~'*100)
def isEmpty():
global stk
if stk==[]:
print("Stack is empty!!!")
else:
None
def push():
global stk
global top
empno=int(input("Enter the employee number to push:"))
ename=input("Enter the employee name to push:")
stk.append([empno,ename])
top=len(stk)-1
def display():
global stk
global top
if top==-1:
isEmpty()
else:
top=len(stk)-1
print(stk[top],"<-top")
for i in range(top-1,-1,-1):
print(stk[i])
def pop_ele():
global stk
global top
if top==-1:
isEmpty()
else:
stk.pop()
top=top-1
def main():
while True:
line()
print("1. Push")
print("2. Pop")
print("3. Display")
print("4. Exit")
ch=int(input("Enter your choice:"))
if ch==1:nm
push()
print("Element Pushed")
elif ch==2:
pop_ele()
elif ch==3:
display()
else:
print("Invalid Choice")