0% found this document useful (0 votes)
10 views4 pages

Computer Sciencee

The document contains three sets of Python programming exercises for Class XII Computer Science. Set 1 involves reading a text file and displaying its contents with words separated by '#'. Set 2 focuses on collecting student data and storing it in a binary file, while Set 3 presents a menu-driven program for stack operations including push and pop.

Uploaded by

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

Computer Sciencee

The document contains three sets of Python programming exercises for Class XII Computer Science. Set 1 involves reading a text file and displaying its contents with words separated by '#'. Set 2 focuses on collecting student data and storing it in a binary file, while Set 3 presents a menu-driven program for stack operations including push and pop.

Uploaded by

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

COMPUTER SCIENCE (083)

CLASS XII
Name:
SET-1

QUES-1 : Write a Python program to perform the following task:


• Read the contents of a text file line by line.
• For each line, display each word separated by the '#' symbol.

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:

Enter roll number:1


Enter name:Sia
enter marks:89.5
Want to append more records?y
Enter roll number:2
Enter name:Guneet
enter marks:81.5
Want to append more records?y
Enter roll number:3
Enter name:Aman
enter marks:90.5
Want to append more records?n
File stu.dat stores these records
{'roll': 1, 'name': 'Sia', 'marks': 89.5}
{'roll': 2, 'name': 'Guneet', 'marks': 81.5}
{'roll': 3, 'name': 'Aman', 'marks': 90.5}
COMPUTER SCIENCE (083)
CLASS XII
Name:
SET-3

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

You might also like