ANSWER KEY Python
ANSWER KEY Python
Set A
Question 1
1. Create a python program named Count.py to read a text file – Mytext.txt and display the
number of vowels/consonants/lower case/ upper case characters.
f=open("story.txt",'r')
Contents=f.read()
Vowels=0
Consonants=0
Lowercase=0
Uppercase=0
for ch in Contents:
if ch in 'aeiouAEIOU':
Vowels=Vowels+1
if ch in 'bcdfghjlmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ':
Consonants=Consonants+1
if ch.islower():
Lowercase=Lowercase+1
if ch.isupper():
Uppercase=Uppercase+1
f.close()
print("Vowels:",Vowels)
print("Consonants:",Consonants)
print("Uppercases:",Uppercase)
print("Lowercases:",Lowercase)
Set B
Question 1
1. Write a python program using list to implement stack data structure and perform
the following operations:
a. Push(), b. Pop(), c. Display() d. Peek()
def Push():
if Special=='ENT':
Stack.append([Doc_ID,Doc_Name])
def Pop():
if Stack==[ ]:
print("Stack is empty")
else:
def Peek():
if Stack==[ ]:
print("Stack is empty")
else:
top=len(Stack)-1
def Disp():
if Stack==[ ]:
print("Stack is empty")
else:
top=len(Stack)-1
for i in range(top,-1,-1):
print(Stack[i])
Stack=[]
ch='y'
print()
print("1.PUSH")
print("2.POP")
print("3.PEEK")
print("4.Disp")
if opt==1:
Push()
elif opt==2:
Pop()
elif opt==3:
Peek()
elif opt==4:
Disp()
else:
Set C
Question 1
1. To write a python program to create a binary file with roll number and
name. Search for a given roll number and display the name, if not found
display appropriate message.
import pickle
def Create():
opt='y'
while opt=='y':
Name=input("Enter Name:")
L= [Roll_No, Name]
pickle.dump (L, F)
opt=input ("Do you want to add another student detail (y/n): ")
F.close()
def Search():
found=0
try:
while True:
S=pickle.load (F)
if S[0]==no:
found=1
break
except:
F.close()
if found==0:
#Main Program
Create()
Search ()