Comp Prac 2 K23
Comp Prac 2 K23
f=open(“Story.txt”, “r”)
Data=f.read()
Allwords=Data.split()
count=0
if Words==’a’:
count=count+1
f.close()
Output:
Program 2. Write a program to separate each word of Story.txt text file by ‘#’ sign.
f=open(“Story.txt”, “r”)
Data=f.read()
for ch in Data:
if ch=” ”:
print(‘#’)
else
print(ch)
f.close()
Output:
Program 3. Write a program to display those lines of Story.txt text file which starts with ‘a’.
f=open(“Story.txt”, “r”)
Data=f.readlines()
count=0
if Words[0]==’A’ or Words[0]==’a’:
count=count+1
print(“Lines=”,count)
f.close()
Output:
Program 4. Write a program to write all lines which starts by vowels in another file Temp.txt
from Story.txt text file.
f=open(“Story.txt”, “r”)
Data=f.read
f2=open(“Temp.txt”, “w”)
Allwords=Data.split()
if Words[0]== ‘a’ and Word[0]== ‘e’ and Word[0]== ‘i’ and Word[0]== ‘o’ and
Word[0]== ‘u’:
f2.write(Word + ‘ ’)
f.close()
f2.close()
Output:
Program 5. Write a program to count upper and lower case characters from Story.txt text file.
f=open(“Story.txt”, “r”)
Data=f.read()
Allwords=Data.split()
count=0
count=count+1:
f.close()
Output:
Program 6. Write a Menu Driven program to perform add record, display all records roll no.
wise, modify record roll no. wise from Student.dat binary file.
import pickle
def add_record():
f=open(“Student.dat”, “wb”)
Sname=input(“Enter name”)
Marks=int(input(“Enter marks”))
pickle.dump(L,f)
f.close()
def disp_record():
f=open(“Student.dat”, “rb”)
L=[ ]
try:
while True:
L=pickle.load(f)
print(L)
except EOFError:
f.close()
def search_record():
f=open(“Student.dat”, “rb+”)
try:
while True:
L=pickle.load(f)
if L[0]==rollno:
Pos=f.tell()
f.seek(Pos)
pickle.dump(L, f)
except EOFError:
f.close()
choice=0
while choice!=’5‘:
print(“1.Add Record”)
print(“2.Disply Record”)
print(“3.Search Record”)
print(“4.Modify Record”)
print(“5.Exit”)
if choice==’1’:
add_record()
elif choice==’2’:
disp_record()
elif choice==’3’
search_record()
elif choice==’4’
search_record()
else:
print(“Exit”)
Output:
Program 7. Write a program to input Roll number, Name and Marks. Write record in
Student.dat binary file.
import pickle
def add_record():
f=open(“Student.dat”, “wb”)
Sname=input(“Enter name”)
Marks=int(input(“Enter marks”))
pickle.dump(L,f)
f.close()
Output:
Program 8. Write a program to display all records of Student.dat binary file.
import pickle
def disp_record():
f=open(“Student.dat”, “rb”)
L=[ ]
try:
while True:
L=pickle.load(f)
print(L)
except EOFError:
f.close()
Output:
Program 9. Write a program to input roll no and display all information of given roll no from
Student.dat binary file.
import pickle
f=open(“Student.dat”, “rb”)
L=[ ]
try:
while True:
L=pickle.load(f)
if L[0]==rollno:
print(L)
exceptEOFError:
f.close()
Output:
Program 10. Write a program to input Roll no and modify marks of given roll no in Student.dat
binary file.
import pickle
f=open(“Student.dat”, “rb+”)
try:
while True:
L=pickle.load(f)
if L[0]==rollno:
L[3]=’ up‘:
Pos=f.tell()
f.seek(Pos)
pickle.dump(L, f)
exceptEOFError:
f.close()
Output:
Program 11. Write a Menu Driven program to perform add record and display record operation
in CSV file.
import.csv
def add_record():
f=open(“Records.csv”, “w”)
Marks=int(input(“Enter Marks”))
csvwriter.writerow(L)
f.close()
def disp_record():
f=open(“Record.csv”, “r”)
csvreader=csv.reader(f)
print(Record)
f.close()
choice=0
while choice!=’3‘:
print(“1.Add Record”)
print(“2.Disply Record”)
if choice==’1’:
add_record()
elif choice==’2’:
disp_record()
else:
print(“Exit”)
Output:
Program 12. Write a program to input username and password. If given user name and
password match with password.csv file then display all records of data.csv file. Otherwise give
message- “Wrong username or password”
import csv
f=open(“password.csv”, “r”)
name=input(“Enter Username”)
password=input(“Enter password”)
j=[name, password]
i=( )
m=0
for i in read:
csvreader=csv.read(f)
if i==j:
print(“Data contains”)
print(data)
m=1
if m!=1:
f.close()
Output:
Program 13. Write a program to implement stack by using list.
Stack=[ ]
def __init__(self):
self.stack = [ ]
self.stack.append(item)
def pop(self):
if len(self.stack) == 0:
return None
return self.stack.pop()
def is_empty(self):
return len(self.stack) == 0
def peek(self):
if len(self.stack) == 0:
return None
return self.stack[-1]
myStack = Stack()
myStack.push(1)
myStack.push(2)
myStack.push(3)
print(myStack.pop())
print(myStack.pop())
print(myStack.pop())
print(myStack.pop())
Output:
Program 14. Write a program to implement queue by using list.
class Queue:
def __init__(self):
self.queue = []
self.queue.append(item)
def dequeue(self):
if len(self.queue) == 0:
return None
else:
return self.queue.pop(0)
def size(self):
return len(self.queue)
def is_empty(self):
return len(self.queue) == 0
myQueue = Queue()
myQueue.enqueue(1)
myQueue.enqueue(2)
myQueue.enqueue(3)
print(myQueue.dequeue())
print(myQueue.dequeue())
print(myQueue.dequeue())
print(myQueue.dequeue())
Output:
Program 15. Write a program to connect MySQL with Python program.
import mysql.connector
cursor=conob.cursor()
data=cursor.fetchall()
print(data)
conob.close()
Output:
“CONNECTED”
Program 16. Write a program to create a function to check if a number is prime or not.
n=int(input(“Enter umber”))
if n > 1:
if (n % i) == 0:
else:
Output:
Program 17. Write a program to create a function to return 0 if number is even otherwise return
1.
if x%2==0:
print(0)
else:
print(1)
Output:
Program 18. Write a program to sort a list.
A=int(input(“enter values”))
print(A.sort(-1))
print(“Sorted list=”A)
Output:
Program 19. Write a program to create a dictionary.
Key=[ ]
Val=[ ]
B=input(“Enter values:”)
Key.append(A)
Val.append(B)
d=(Key[0]:Val[0])
print(“Create Dictionary”)
print(d)
Output:
CERTIFICATE
This is to certify that Master Brendon Rafael Diengdoh of class XII
Science bearing Roll No. _________ has successfully completed all the
required programs in this computer practical file under my supervision
for the partial fulfillment of AISSCE 2023.
COMPUTER PRACTICAL
FILE
NAME: Brendon Rafael Diengdoh
CLASS: XII Science
ROLL NO.:
SESSION: 2023-24