Q1. Write a Python program to search any word in given string/sentence.
def countWord(str1,word):
s = str1.split()
count=0
for w in s:
if w==word:
count+=1
return count
str1 = input(“Enter any sentence :”)
word = input(“Enter word to search in sentence :”)
count = countWord(str1,word)
if count==0:
print(“## Sorry! “,word,” not present “)
else:
print(“## “,word,” occurs “,count,” times ## “)
Q2. Write a Python program to implement the stack operations, to push and pop the details of
Books i.e (BOOK_ID,BOOK_NAME,PRICE)
def push(Book, stack):
stack.append(Book)
def pop(stack):
if stack == []:
print(‘Underflow’)
else:
print(stack.pop())
stack = []
while True:
print(‘STACK OPERATIONS’)
print(‘1.Push’)
print(‘2.Pop’)
print(‘3.Exit’)
ch = int(input(‘Enter your choice : ‘))
if ch == 1:
BOOK_ID= int(input(‘Enter Book id: ‘))
BOOK_NAME = input(‘Enter Book Name: ‘)
PRICE = int(input(‘Enter price: ‘))
Book = [BOOK_ID,BOOK_NAME,PRICE]
push(Book, stack)
elif ch == 2:
pop(stack)
elif ch == 3:
break
else:
print(‘Invalid choice’)
Q3. Write a program to Create a binary file (Stud.dat) with name and roll number using
dictionary. And search for a record with roll number as 34 or 36, if found display the record.
#Create a binary file with name and roll number
import pickle
file=open(“Stud.dat”,”wb”)
Stud_data={}
no_of_students=int(input(“Enter no of Students:”))
for i in range(no_of_students):
Stud_data[“roll_no”]=int(input(“Enter roll no:”))
Stud_data[“name”]=input(“Enter name: “)
pickle.dump(Stud_data,file)
print(“Data added successfully”)
file.close()/
#search for a record with roll number as 34 or 36, if found display the record.
import pickle
file=open(“Stud.dat”,”rb”)
Stud_data={}
S_k=[34,36]
Found = False
try:
while True:
Stud_data=pickle.load(file)
if Stud_data[“roll_no”] in S_k:
Found=True
print(Stud_data[“name”],”found in file.”)
except:
if (Found==False):
print(“No student data found. Please try again”)
File.close()
Q4. Write a Python program to read a text file and display each word separated by ‘#’
(STORY.TXT)
file=open(“STORY.TXT”,”r”)
lines=file.readlines()
for line in lines:
words=line.split()
for word in words:
print(word+”#”,end=””)
print(“”)
file.close()
Q5. Write a Python program to create csv file (Emp.csv) to store information about some
Employees with the file structure as Employee_id, Employee_Name, Department, Salary.
import csv
myfile=open(‘Emp.csv’,’w’,newline=’’)
Emp_writer=csv.writer(myfile)
Emp_writer.writerow([‘Employee_ID’,’Employee_Name’,’Department’,’Salary’])
N = int(input(‘How many records?’))
for i in range(N):
print(“Employee”, i+1)
Eid =int(input(“Enter Employee id:”))
Ename=input(“Enter Employee Name:”)
Desg=input(“Enter Designation:”)
Salary=int(input(“Enter Salary:”))
Emp=[Eid, Ename, Desg, Salary]
Emp_writer.writerow(Emp)
print(“File created successfully!”)
myfile.close()
Q6. Write a function in python program to count the number of lines in a text file
‘ANSWER.TXT’ which is starting with an alphabet ‘R’ or ‘r’.
def COUNTLINES():
File = open(‘ANSWER.TXT’,’r’)
lines = File.readlines()
Count = 0
for w in lines:
if w[0] == ‘R’ or w[0] == ‘r’:
Count = Count + 1
print(“Total lines started with R or r :”, Count)
File.close()
COUNTLINES()