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

Practical file sample 24.pdf

It is a file of cs orafh

Uploaded by

gtft6419
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Practical file sample 24.pdf

It is a file of cs orafh

Uploaded by

gtft6419
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Q16.

Write a function in Python that counts the number of “Me” or “My” (in
smaller case also) words present in a text file “STORY.TXT”. If the “STORY.TXT”
contents are as follows:
My first book was Me and My Family. It gave me chance to be Known to the
world.
The output of the function should be: Count of Me/My in file: 4
Ans:
def countmemy():
f=open("story.txt","r")
d=f.read()
m=d.split()
c=0
for i in m:
if i=="me" or i=="my" or i=="Me" or i=="My":
c=c+1
print("Count of me or my in file",c)
countmemy()
Q17. Write a user defined function in Python that displays the number of lines
starting with ‘H’ in the file story.txt. Eg: if the file contains:
Whose woods these are I think I know.
His house is in the village though;
He will not see me stopping here
To watch his woods fill up with snow.
Then the line count should be 2
Ans:
def countH():
f=open("story.txt","r")
d=f.readlines()
c=0
for i in d:
if i[0]=="H":
c=c+1
print("Line count is",c)
countH()
Q18: Writing Python program to add multiple record in a binary file.
Ans:
import pickle
def write():
file=open('demo.txt','ab')
y='y'
while y=='y':
roll=int(input('Enter roll no.:'))
name=input('Enter name:')
marks=int(input('Enter Marks:'))
data=[roll,name,marks]
pickle.dump(data,file)
y=input('Do you want to enter data again:')
file.close()
Q19. Write a function push() and pop() to add a new student name and remove a student
name from a list student, considering them to act as PUSH and POP operations of stack
Data Structure in Python.
st=[ ]
def push():
y="y"
while y=="y":
sn=input("Enter name of student:")
st.append(sn)
y=input("Do you want to enter more name(y/n):")

def pop():
if(st==[]):
print("Stack is empty")
else:
print("Deleted student name :",st.pop())

Output:-
Q20: Julie has created a dictionary containing names and marks as key value pairs of 6
students. Write a program, with separate user defined functions to perform the
following operations:
● Push the keys (name of the student) of the dictionary into a stack, where
the corresponding value (marks) is greater than 75.
● Pop and display the content of the stack.
R={"OM":76, "JAI":45, "BOB":89,"ALI":65, "ANU":90, "TOM":82}
def PUSH(S,N):
S.append(N)
def POP(S):
if S!=[]:
return S.pop()
ST=[]
for k in R:
if R[k]>75:
PUSH(ST,k)
while True:
if ST!=[]:
print(POP(ST),end=" ")
else:
break

Output:
Q 21: Write a function Addvowel(l) to perform push opration in stack. Where a list is
given:
L=[‘Ansh’,’Vipin’,’Ishan’,’Devesh’,’Om’,’Upashna’]
Push only those element who is started from vowels
Code:
def Addvowel(l):
L2.append(l)

L2=[]
L1=['Ansh','Vipin','Ishan','Devesh','Om','Upashna']
for i in L1:
if i[0] in 'aeiouAEIOU':
Addvowel(i)

print("Original List:\n",L1)
print("List after using push:\n",L2)

Output:

You might also like