Computer Sciencee
Computer Sciencee
CLASS XII
Name:
SET-1
ANS: f=open('sample1.txt','r')
st=' '
while st:
st=f.readline()
print(st.replace(' ','#'))
OUTPUT:
The#rose#is#red.
A#girl#is#playing#there.
There#is#a#playground.
The#rocket#is#in#the#sky.
COMPUTER SCIENCE (083)
CLASS XII
Name:
SET-2
QUES 1: Write a program to get student data(roll no. , name and marks) from user and write
onto a binary file. Also display the student records on the screen.
ANS: import pickle stdic={}
stufile=open('stu.dat','wb') ans='y'
while ans=='y':
rno=int(input("Enter roll number:"))
n=input("enter name:")
m=float(input("enter marks:"))
stdic['roll']=rno
stdic['name']=n
stdic['marks']=m
pickle.dump(stdic,stufile)
ans=input("Want to append more records?")
stufile.close()
stu={}
fin=open('stu.dat','rb')
print("File stu.dat stores these records")
try:
while True:
stu=pickle.load(fin)
print(stu)
except EOFError:
fin.close()
OUTPUT:
QUES 1: Write a menu driven program to push book no in stack and pop it from
stack.
ANS: stk=[]
ch='Y'
while(ch=='Y' or ch=='y'):
print("Enter 1 : Push")
print("Enter 2 : Pop")
print("Enter 3 : Display")
opt=int(input('enter ur choice:='))
if opt==1:
d=int(input("enter book no : "))
stk.append(d)
print("Item appended")
elif opt==2:
if (stk==[]):
print( "Stack empty")
else:
p=stk.pop()
print ("Deleted element:", p)
elif opt==3:
length=len(stk)
for i in range(length-1,-1,-1):
print(stk[i])
else:
print('invalid choice')
ch=(input('Do you want to continue?'))
OUTPUT:
Enter 1 : Push
Enter 2 : Pop
Enter 3 : Display
enter ur choice:=1
enter book no : 1001
Item appended
Enter 1 : Push
Enter 2 : Pop
Enter 3 : Display
enter ur choice:=1
enter book no : 1002
Item appended
Enter 1 : Push
Enter 2 : Pop
Enter 3 : Display
enter ur choice:=1
enter book no : 1003
Item appended
Enter 1 : Push
Enter 2 : Pop
Enter 3 : Display
enter ur choice:=2
Deleted element: 1003
Enter 1 : Push
Enter 2 : Pop
Enter 3 : Display
enter ur choice:=3
1002
1001