[go: up one dir, main page]

0% found this document useful (0 votes)
9 views28 pages

Comp Prac 2 K23

Uploaded by

rafaelbrendon09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views28 pages

Comp Prac 2 K23

Uploaded by

rafaelbrendon09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Program 1. Write a program to count alphabet “a” from Story .txt text file.

f=open(“Story.txt”, “r”)

Data=f.read()

Allwords=Data.split()

count=0

for Words in Allwords:

if Words==’a’:

count=count+1

print(“number of albhabet ‘a’=”,count)

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

for Words in Data:

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

for Words in Allwords:

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

for Words in Allwords:

if Words isupper() and Words islower():

count=count+1:

print(“Number of upper and lower case=”, count)

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

rollno=input(“Enter roll number”)

Sname=input(“Enter name”)

Marks=int(input(“Enter marks”))

L=[rollno, Sname, 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+”)

rollno=input(“Enter roll number”)

up=input(“Enter modified values”)

try:
while True:

L=pickle.load(f)

if L[0]==rollno:

L[2]=’ up‘ and L[3]=’up’

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

choice=input(“Enter your choice…..”)

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

rollno=input(“Enter roll number”)

Sname=input(“Enter name”)

Marks=int(input(“Enter marks”))

L=[rollno, Sname, 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=[ ]

rollno=input(“Enter roll number”)

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+”)

rollno=input(“Enter roll number”)

up=int(input(“Enter modified Marks”))

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

rollno=input(“Enter roll number”)

Sname=input(“ Enter Name”)

Marks=int(input(“Enter Marks”))

L=[rollno, Sname, Marks]

csvwriter.writerow(L)

f.close()

def disp_record():

f=open(“Record.csv”, “r”)

csvreader=csv.reader(f)

for Records in csvreader:

print(Record)

f.close()

choice=0

while choice!=’3‘:

print(“1.Add Record”)

print(“2.Disply Record”)

choice=input(“Enter your choice…..”)

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:

f=open(“data.csv”, “r”, newline=’\r\n’)

csvreader=csv.read(f)

if i==j:

print(“Data contains”)

for data in csvreader:

print(data)

m=1

if m!=1:

print(“Wrong password or username”)

f.close()
Output:
Program 13. Write a program to implement stack by using list.

Stack=[ ]

def __init__(self):

self.stack = [ ]

def push(self, item):

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 = []

def enqueue(self, item):

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

conob=mysql.connector.connect(host= ‘BSF’, user= ‘mysql’, passwd= ‘mysql’, database=


‘School’)

cursor=conob.cursor()

cursor.execute(“Select * from Student”)

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:

for i in range(2, int(n/2)+1):

if (n % i) == 0:

print( "is not a prime number")

else:

print( "is a prime number")

Output:
Program 17. Write a program to create a function to return 0 if number is even otherwise return
1.

X=int(input(“Enter the number”))

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=[ ]

A=input (“enter Key:”)

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.

External Examiner Mr. A


Dantre
PGT Computer Science
B.S.F. Sr. Sec. School
Shillong-6
ACKNOWLEDGEMENT
I am grateful to God almighty for giving me good health and enabled me
finish this project in time. I would like to express my gratitude to our
Principal Madam, Sarla Mishra, my computer teacher, Sir A Dantre who
gave me opportunity to complete this practical file.
This project could not have been completed without the help and
encouragement I have received. For this, I would like to thank my
parents their support, guidance and encouragement.

Brendon Rafael Diengdoh


SHILLONG

COMPUTER PRACTICAL
FILE
NAME: Brendon Rafael Diengdoh
CLASS: XII Science
ROLL NO.:
SESSION: 2023-24

You might also like