SOLUTIONS
SOLUTIONS
print(row)
mycur.close()
if choice==1:
view( )
elif choice==2:
fno=int(input("enter flight no:"))
fname=input("enter flight name:")
Python Programs Solutions fare=float(input ("enter fare:"))
1. Write a program that writes the content into the flight=[fno,fname,fare]
text file “myfile.txt” and count and display the push(stack,flight)
total number of uppercase and lowercase elif choice==3:
characters pop( )
Sol. else:
def write(): print("wrong choice")
fw=open("myfile.txt","w") break
L=["Twinkle Twinkle Little Star\n" 3. Write a program that writes the content into
"How I wonder what you are\n" the text file “myfile.txt” and count and display
"Up above the world sohigh\n" those lines that starts with ‘T’ or ‘t’.
"Like a diamond in the sky\n"
Sol.
"Twinkle Twinkle Little star\n"]
def write():
fw.writelines(L)
fw=open("myfile.txt","w")
fw.close()
L=["Twinkle Twinkle Little Star\n"
def count():
"How I wonder what you are\n"
fr=open("myfile.txt","r")
"Up above the world sohigh\n"
fc=fr.read()
"Like a diamond in the sky\n"
cu=0
"Twinkle Twinkle Little star\n"]
cl=0
fw.writelines(L)
for i in fc:
fw.close()
if i.isupper():
def count_lines():
cu+=1
fr=open("myfile.txt","r")
elif i.islower():
fc=fr.readlines()
cl+=1
ct=0
print("No. of uppercases:",cu)
for i in fc:
print("No. of lowercases:",cl)
if i[0]=="T" or i[0]=="t":
fr.close()
ct+=1
write( )
print(i)
count( )
print("Count of lines starting with T:",ct)
2.Write a program to implement stack using list data
fr.close()
structure( Push,Pop & View)
write()
Sol.
count_lines( )
stack=[ ]
4.Write a program to create a binary file “stud.dat”
def view ():
to write the content[rno,name,marks] and search for
for x in range(len(stack)-1,-1,-1):
the given rollno and display.
print(stack[x])
Sol.
def push(stk,item):
import pickle
stk.append(item)
def create_binfile():
def pop( ):
stu=dict()
if stack==[]:
stufile=open("stu.dat","wb")
print("stack is empty")
ans='y'
else:
while ans =='y':
item=stack.pop(-1)
rno=int(input("enter roll no:"))
print("delected item:",item)
name=input("enter name:")
print("stack operation")
marks=float(input("enter marks:"))
print("1.view")
stu["rollno"]=rno
print("2.push")
stu["name"]=name
print("3.pop")
stu["marks"]=marks
while True:
pickle.dump(stu,stufile)
choice=int (input("enter your choice:"))
ans=input("want more records?(y/n)")
stufile.close()
def search_binfile():
fin=open("stu.dat","rb")
stud=dict()
rno1=int(input("enter roll no to search"))
print("student details")
try:
while True:
stud=pickle.load(fin)
if stud["rollno"]==rno1:
print(stud)
else:
print("record not found")
except EOFError:
fin.close()
create_binfile()
search_binfile()