Q1. Write a program in python to input roll no, name, class of student and write
the data into the text file “Student.txt”, using concept of text file handling and
display it .Implement the program using functions.
obj=open("Student.txt","a+")
def add():
n=int(input("Enter the Number of Student Details you want to Enter:"))
rec=[]
for i in range(n):
print("\nEnter Details of Student",(i+1),"Below:")
rno=input("Enter the Roll No:")
name=input("Enter the Name of the student:")
std=input("Enter the Class of the student:")
rec.append(mo+","+namet+","+std+"\n')
obj. writelines(rec)
obj. flush()
def display():
obj.seek(0)
s_rec=[]
s_rec=obj.readlines()
for line in s_rec:
print(line)
while True:
print("1. Add Record")
print("2. Display Record")
print("3. Exit")
choici t(input("Enter your Choice 1/2/3:"))
if choice==1:
add()
elif choice==2:
display()
elif choice==3:
break
else:
print("Please Enter the Correct Choice 1/2/3:")print("Operation Completed")
obj.close()
Q2. Write a program in python to input roll no, name, class of student and
write the data into the text file “Student.txt” and search and display a record
using students name using functions.
obj=open("Student] txt","a+")
def add():
n=int(input("Enter the Number of Student Details you want to Enter:"))
ree=[]
for i in range(n):
print("Enter Details of Student",(i+1),"Below:")
mo=input("Enter the Roll No:")
name=input("Enter the Name of the student:")
std=input("Enter the Class of the student")
rec.append(mot","+name+","+std+'\n’)
obj. writelines(rec)
obj.flush()
def display():
obj.seek(0)
s_rec=[]
s_rec=obj.readlines()
for line in s_rec:
print(line)
def search():
obj.seck(0)
s_rec=[]
s_rec=obj.readlines()
found=False
s_name=input("Enter the Student's Name to search in the record:")
for line in s_rec:
if s_name in line:
print("Data Found")print(line)
found=True
if found==False:
print("Data Not Found in the Record.
while True:
print("1. Add Record")
print("2. Display Record")
print("3. Search Record")
print("4. Exit")
choice=int(input("Enter your Choice 1/2/3/4:"))
if choice—=1:
display()
elif choice==3:
search()
elif choice==4:
break
else:
print("Please Enter the Correct Choice 1/2/3/4:")
print("Operation Completed")
obj.close()
Q3. Write a program in python to input roll no, name, class of a student in a
class and write the data into the binary file handling [data structure list]
and display the content of the file using functions.
import pickle
def write():
fopen("Binarywrite.dat",,wb')
record=|]
while True:
ro=int(input("Enter the Student's Roll Num :"
name=input("Enter the Name of the student
std=int(input("Enter the Class of the student :”))
data=[ro, name, std]
record.append (data)ch=input("Do you want to Enter more records?...Y/N") if
ch=='N' or ch=="'n':
break
pickle.dump(record,f)
def read():
f-open("Binarywrite.dat",'rb’)
s=pickle.load(f)
print("The Students details are :")
for iin s:
rno=i[0]
name=i[1]
std=i[2]
print(rno,name,clas)
write()
read()
Q4. Write a program in python to implement data structure stack using list
in python.Perform Push and Pop operations on it.
stack=[]
choice='y'
while choice=='y':
print("1.PU: SH’)
print("2.POP")
c=int(input("Enter your choice"))
ife==1:
a=int(input("Enter the element which you want to push:"))
stack.append(a)
O:
print("Stack empty...Underflow case....can not delete the element...") else:
print(""The Deleted Element is :", stack.pop())
else:
print("Wrong input")
choice=input("Do you want to continue or Not?...y/n")QS. Read a text file and count the number of vowels present in the
file.{implement using functions]
Content of the file “word.txt” is:
Umbrella
Rainbow
Elephant
Crocodile
Mouse
Television
def word():
file=open("word.txt","r")
data=file.read()
print("The contents of the text file:")
print(data)
vowels=0
for ch in data:
ch=str.lower(ch)
if ch in "aeiou":
vowels+=1
print("The number of Vowels present in the text file is:",vowels)
word()Q6. Remove all the lines that contain the character ‘a’ in a file “word.txt”
and write it to another file “newword.txt” .[implement using functions] [07]
Content of the file “word.txt” is:
Umbrella
Rainbow
Elephant
Crocodile
Mouse
Television
fopen("word.txt","r")
lines=f.readlines()
fclose()
fopen("word.txt","w")
fl=open("newword.txt","w")
for line in lines:
if ‘a’ in line:
fl.write(line)
else:
f.write(line)
print("All the line that contains character 'a' has been removed from word.txt
file.")
print("All the line that contains character 'a' has been saved in newword.txtfile.\n")
f.close()
fl.close()
fopen("word.txt","r")
fl=open("newword.txt",”
lines=f.readlines()
print("New contents of the word.text file are:")
for a in lines:
print(a,end=
f.close()
lines=fl.read()
print(""The contents of the newword.text file are:")
for a in lines:
print(a,end="")
f.close()
fl.close()
Q7. Write a program in python to input roll no, name, class of student in a
class and write the data into the binary file handling [data structure:- list]
and search a record based on student name and display it using functions.
import pickle
def write():
f=open("Binarywrite.dat",'wb')
record=[]
while True:
rmo=int(input("Enter the Student's Roll Num :"))
name=input("Enter the Name of the student :")
std=int(input("Enter the Class of the student :"))
data=[mo, name, std]
record.append(data)
ch=input("Do you want to Enter more records?...Y/N") if
ch=="N' or ch=="n':
break
pickle.dump(record,f)def read():
fopen("Binarywrite.dat",'rb')
s=pickle.load(f)
print("The Students details are :")
for iin s:
mo=i[0]
name=i[1]
std=i[2]
print(mo,name,std)
def search():
fopen("Binarywrite.dat",'rb')
s=pickle.load(f)
found=0
name=input("Enter the student name to search in the record :") fori
ins:
if if1]=-name:
print("Record Found - The student details are") print(i)
found=
if foun :
print("Record not found")
write()
read()
search()
Q8. Write a program in python to implement data structure stack using list
in python.Perform Push and Display operations on it.
stack=[]
choice='y'
while choice='y':
print("1.PUSH")
print("2.Display Elements of stack")
c=int(input("Enter your choice"))a=int(input("Enter the element which you want to push:"))
stack.append(a)
elif 2:
for i in range(len(stack)-1,-1,-1): #G-1,-1,-1) =range(2,-1,-1)
print(stack[i]) #stack[2],stack[1],stack[0]
#102 101 100
else:
print("Wrong input")
choice=input("Do you want to continue or Not?...y/n")
Q9. Read a text file and count the number of consonants present in the
file.{implement using functions]
Content of the file “word.txt” is:
Umbrella
Rainbow
Elephant
Crocodile
Mouse
Television
def consonants():
file=open("word.txt","r")
data=file.read()
print(data)
consonants=0
for ch in data:
if ch in "bedfghjklmnpqrstvwxyz":
consonants+=1
print("Number of Consonants:",consonants)
consonants()
Q10. Read a text file line by line and display each word separated by #.
[implement using functions] [07] Content of the file “word.txt” is:
Umbrella
Rainbow
Elephant
Crocodile
Mouse
Televisionfile=open("word.txt","t")
content=file.readlines()
def word():
for line in content:
#print(line)
words-line.split()
Aprint(words)
for word in words:
print(word+"#",end=!')
print("")
word()
Q11. Write a program in python to input roll no, name, class of student in a
class and write the data into the binary file handling [data structure:- list]
and display all the records saved in file using functions.
import pickle
def write():
f-open("Binarywrite.dat'’ 'wb’)
record=|]
while True:
ro=int(input("Enter the Student's Roll Num :"
name=input("Enter the Name of the student
std=int(input("Enter the Class of the student :"))
data=[ro, name, std]
record.append(data)
ch=input("Do you want to Enter more records?...Y/N") if
ch=='N' or ch=="'n':
break
pickle.dump(record,f)
def read():
f-open("Binarywrite.dat','rb’)
s=pickle.load(f)
print("The Students details are :")
for iin s:print(rno,name,std)
write()
read()
Q12. Write a program in python to implement data structure stack using
list in python. Perform Pop and Display operations on it.
stack=[]
choice='y'
while choice:
print("1.POP")
print("2.Display Elements of stack")
c=int(input("Enter your choice"))
ife==1:
if stack—=[]:
print("Stack empty...Underflow case....cannot delete the element...") else:
print("Deleted Element is :", stack.pop())
elif c—=2:
for i in range(len(stack)-1,-1,-1):
print(stack{i])
else:
print("Wrong input")
choice=input("Do you want to continue or Not?...y/n")
Q13. Read a text file and count the number of lowercase characters present
in the file.{implement using functions]
Content of the file “word.txt” is:
A computer is a machine that can be instructed to carry out sequences of
arithmetic or logical operations automatically via computer programming.
def lower):
file=open("word.txt","r’)
data=file.read()
print(data)
lower=0for ch in data:
if str.islower(ch):
lower+=1
print("Number of Lower Case Letters are:" lower)
lower()
Q14. Read a text file line by line and display each word separated by “@#”.
[Implement using functions]
Content of the file “data.txt” is:
A computer is a machine that can be instructed to carry out sequences of
arithmetic or logical operations automatically via computer programming.
file=open("data.txt","r")
content=file.readlines()
def data():
for line in content:
#print(line)
words=line.split()
#print(words)
for word in words:
print(word+'@#",end=" '
print(" ")
data()
Q15. Write a program in python to input roll no, name, class of student in a
class and write the data into the binary file handling [data structure:- list]
and display all the records saved in file using functions.
import pickle
def write():
f-open("Binarywrite.dat" wb’)
record=|]
while True:
rno=int(input("Enter the Student's Roll Num :"))
name=input("Enter the Name of the student :") std
=int(input("Enter the Class of the student :")) data=[rno,
name, std]
record.append(data)
input("Do you want to Enter more records?...Y/N") if
ch=='N' or ch=="n':
break
pickle.dump(record,f)def read():
fopen("Binarywrite.dat",'rb’)
s=pickle.load(f)
print("The Students details are :”)
foriins:
rno=i[0]
name=i[1]
std =i[2]
print(rno,name,std)
write()
read()
Q16. Write a program in python to implement data structure stack using
list in python. Perform Push and Pop operations on it.
stack=[]
choice='y'
while choice: :
print("1.PUSH")
print("2.POP")
=int(input("Enter your choice"))
Ts
a=int(input("Enter the clement which you want to push:"))
stack.append(a)
elif c==2:
if stack==[]:
print("Stack empty...Underflow case....can not delete the element...") else:
print("The Deleted Element is :", stack.pop())else:
print("Wrong input")
choice=input("Do you want to continue or Not?...y/n")