Class Xii-Cbse-Practical File 2
Class Xii-Cbse-Practical File 2
1. # Practical prog-1 Read a text file line by line and display each word
separated by a'#'
def word_as():
file=open("C:\\Users\\ranga\\Desktop\\fhandle\\test.txt","r")
lines=file.readline()
for line in lines:
words=line.split()
for word in words:
print(word+"#",end=" ")
print(" ")
file.close()
#main program
word_as()
Output
I# d# r#
n# o#
d# a# t#
i# l# h#
a# l# e#
r#
i# I# s#
s# n#
d# a#
m# i# n#
y# a# d#
n#
c# s# s#
o# i#
u# a# s#
n# r# t#
t# e# e#
r# r#
y# m# s#
y# .#
a#
n# b#
Output:
3. #Practical Program 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("C:\\Users\\ranga\\Desktop\\fhandle\\stud.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("C:\\Users\\ranga\\Desktop\\fhandle\\stud.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("C:\\Users\\ranga\\Desktop\\fhandle\\stud.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()
#main program
#Prepared by Ramesha K S
while True:
print("MENU \n 1-Write in a file \n 2-display ")
print(" 3-search\n 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()
Output:
MENU
1-Write in a file
2-display
3-search
4-exit
MENU
1-Write in a file
2-display
3-search
4-exit
4. Create a binary file with roll number, name and marks. Input a roll
number and update the marks.
import pickle
student_data={}
no_of_students=int(input("Enter no of students: "))
file=open("student0.dat","ab")
for i in range(no_of_students):
student_data["RollNo"]=int(input("Enter roll no: "))
student_data["Name"]=input("Enter Student name: ")
student_data["Marks"]=float(input("Enter Student Marks: "))
pickle.dump(student_data,file)
student_data={}
file.close()
file=open("student0.dat","rb")
try:
while True:
student_data=pickle.load(file)
print(student_data)
except EOFError:
file.close()
found=False
roll_no=int(input("enter the roll no to search: "))
file=open("student0.dat","rb+")
try:
while True:
pos=file.tell()
student_data=pickle.load(file)
if(student_data["RollNo"])==roll_no:
student_data["Marks"]=float(input("Enter marks to be updated: "))
file.seek(pos)
pickle.dump(student_data,file)
found=True
except EOFError:
if(found==False):
print("Roll no not found Please Try Again")
else:
print("Student marks updated successfully")
file.close()
file=open("student0.dat","rb")
try:
while True:
student_data=pickle.load(file)
print(student_data)
except EOFError:
file.close()
file=open("student0.dat","rb")
try:
while True:
student_data=pickle.load(file)
print(student_data)
except EOFError:
file.close()
Output
Enter no of students: 3
Enter roll no: 101
Enter Student name: VINAY
Enter Student Marks: 550
Enter roll no: 102
Enter Student name: SAANVI
Enter Student Marks: 580
Enter roll no: 103
Enter Student name: SUDHA
Enter Student Marks: 450
{'RollNo': 101, 'Name': 'VINAY', 'Marks': 550.0}
{'RollNo': 102, 'Name': 'SAANVI', 'Marks': 580.0}
{'RollNo': 103, 'Name': 'SUDHA', 'Marks': 450.0}
fin=open("C:\\Users\\ranga\\Desktop\\fhandle\\book.txt","r")
fout=open("C:\\Users\\ranga\\Desktop\\fhandle\\book.txt","w")
s=fin.readlines()
for j in s:
if 'a' in j:
fout.write(j)
fout.close()
fin.close()
Ouput
5
4
3
7. Write a Python program to implement a stack and queue using a list data
structure
def isEmpty(stk): # checks whether the stack is empty or not
if stk==[]:
return True
else:
return False
def Push(stk,item): # Allow additions to the stack
stk.append(item)
top=len(stk)-1
def Pop(stk):
if isEmpty(stk): # verifies whether the stack is empty or not
print("Underflow")
else: # Allow deletions from the stack
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)
print("Popped item is "+str(item))
def Display(stk):
if isEmpty(stk):
print("Stack is empty")
else:
top=len(stk)-1
print("Elements in the stack are: ")
for i in range(top,-1,-1):
print (str(stk[i]))
# executable code
if __name__ == "__main__":
stk=[]
top=None
Push(stk,10)
Push(stk,20)
Push(stk,30)
Push(stk,40)
Pop(stk)
Display(stk)
Output:
Popped item is 40
Elements in the stack are:
30
20
10
8. Take a sample of ten phishing e-mails (or any text file) and find most
commonly occurring word(s).
def Read_Email_File():
import collections
fin = open('email.txt','r')
a= fin.read()
d={ }
L=a.lower().split()
for word in L:
word = word.replace(".","")
word = word.replace(",","")
word = word.replace(":","")
word = word.replace("\"","")
word = word.replace("!","")
word = word.replace("&","")
word = word.replace("*","")
for k in L:
key=k
if key not in d:
count=L.count(key)
d[key]=count
word_counter = collections.Counter(d)
fin.close()
#Driver Code
def main():
Read_Email_File()
main()
Output:
the : 5
in : 3
of : 3
for : 2
9. Create a CSV file by entering user-id and password, read and search the
password for given userid.
import csv
def write():
f=open("C:\\Users\\ranga\\Desktop\\Prctical
File\\details.csv","w",newline='')
wo=csv.writer(f)
wo.writerow(["UserId","Password"])
while True:
u_id=input("Enter User-Id:")
pswd=input("Enter Password:")
data=[u_id,pswd]
wo.writerow(data)
ch=input("Do you want to enter more records(Y/N):")
if ch in 'Nn':
break
f.close()
def read():
f=open("C:\\Users\\ranga\\Desktop\\Prctical File\\details.csv","r")
ro=csv.reader(f)
for i in ro:
print(i)
f.close()
def search():
f=open("C:\\Users\\ranga\\Desktop\\Prctical File\\details.csv","r")
Found=0
u=input("Enter user-id to search:")
ro=csv.reader(f)
next(ro)
for i in ro:
if i[0]==u:
print(i[1])
Found=1
f.close()
if Found==0:
print(" Sorry..... no records found")
write()
read()
search()
CSV file
Output:
Enter User-Id: ROHAN
Enter Password: rohan@123
Do you want to enter more records(Y/N): y
Enter User-Id: RANJAN
Enter Password: ranjan@1234
Do you want to enter more records(Y/N): y
Enter User-Id: HEMANTH
Enter Password: hemanth@123
Do you want to enter more records(Y/N): n
['UserId', 'Password']
[' ROHAN', 'rohan@123']
['RANJAN', 'ranjan@1234']
['HEMANTH', 'hemanth@123']
Enter user-id to search: hemanth420
Sorry..... no records found
Database Management
● Create a student table and insert data. Implement the following SQL commands on the
student table:
o ALTER table to add new attributes / modify data type / drop attribute
o GROUP BY and find the min, max, sum, count and average
1. Create Database:
Command:
Output
2. Use Database:
3. Create table:
Command
mysql> Create table student
-> (Rno int primary key,
-> Name varchar(10) NOT NULL,
-> Gender varchar(8),
-> Marks int,
-> Scode varchar(5));
4. Describe the table
Command
mysql>desc student
Output:
5. Insert command
mysql> insert into student values(101,'Hemanth','male',420,'s101');
6. SELECT COMMAND
7. Select command
8. Select Command