0% found this document useful (0 votes)
31 views

ANSWER KEY Python

The document contains sample code for Python programs to perform various tasks: 1) Read a text file and count vowels, consonants, uppercase and lowercase characters. 2) Implement a stack data structure using a list and perform push, pop, peek and display operations. 3) Create a binary file with roll number and name, search for a given roll number and display name or message if not found.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

ANSWER KEY Python

The document contains sample code for Python programs to perform various tasks: 1) Read a text file and count vowels, consonants, uppercase and lowercase characters. 2) Implement a stack data structure using a list and perform push, pop, peek and display operations. 3) Create a binary file with roll number and name, search for a given roll number and display name or message if not found.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ANSWER KEY

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

Doc_ID=int(input("Enter the Doctor ID:"))

Doc_Name=input("Enter the Name of the Doctor:")

Mob=int(input("Enter the Mobile Number of the Doctor:"))

Special=input("Enter the Specialization:")

if Special=='ENT':

Stack.append([Doc_ID,Doc_Name])

def Pop():

if Stack==[ ]:

print("Stack is empty")

else:

print("The deleted doctor detail is:",Stack.pop())

def Peek():

if Stack==[ ]:

print("Stack is empty")

else:

top=len(Stack)-1

print("The top of the stack is:",Stack[top])

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("Performing Stack Operations Using List\n")

while ch=='y' or ch=='Y':

print()

print("1.PUSH")

print("2.POP")

print("3.PEEK")

print("4.Disp")

opt=int(input("Enter your choice:"))

if opt==1:

Push()

elif opt==2:

Pop()

elif opt==3:

Peek()

elif opt==4:

Disp()

else:

print("Invalid Choice, Try Again!!!")

ch=input("\nDo you want to Perform another operation(y/n):")

ch=input("\nDo you want to Perform another operation(y/n):")

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

F=open ("Students.dat", 'ab')

opt='y'

while opt=='y':

Roll_No=int(input('Enter roll number:'))

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

F=open ("Students.dat", 'rb')

no=int(input("Enter Roll. No of student to search: "))

found=0

try:

while True:

S=pickle.load (F)

if S[0]==no:

print("The searched Roll. No is found and Details are:",S)

found=1

break

except:

F.close()

if found==0:

print ("The Searched Roll. No is not found")

#Main Program

Create()

Search ()

You might also like