Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

Ques-1.

(i)
fout = open(“story.txt”, ‘w’)
fout.write(“Hello How are You\n”)
fout.write(“I am fine\n”)
fout.write(“Thankyou\n”)
fout.write(“This is too much\n”)
fout.write(“What happend\n”)
fout.close()

#Reading line by line and checking

fin = open(“story.txt”, “r”)


line = ‘ ‘
while line:
line = fin.readline()
if line != None and line[0] == ‘T’:
print(line)
fin.close()

(ii)
import pickle
def WriteRec():
fout = open(“Emp.dat”, “wb”) # opening of file
for i in range(5):
rec = []
id = int(input(“Enter Admission Number : “))
name = input(“Enter Name : “)
salary = float(input(“Enter Fee : “))
rec = [id, name, salary]
pickle.dump(rec, fout) #write one record in a file refered by fout
fout.close()
def ReadRec():
fin = open(“Emp.dat”, “rb”)
try:
Study in Monaco
En Savoir …
IUM
while True: # loop is required to read all records
rec = pickle.load(fin) # read only one record
# at a time referred by fin file object
print(rec[0], rec[1], rec[2])
except:
fin.close()
#Always call function after writing all functions
WriteRec()
ReadRec()

Ques-2.
# STACK
student = [] #global variable
def PUSH():
admno = int(input(“Enter Admission No :”))
name = input(“Enter Name : “)
rec = [admno, name]
student.append(rec)
def DISPLAY():
if student == []:
print(“Empty Stack”)
else:
for st in student:
if st[1][0] == ‘A’:
print(st[0], st[1])
#to invoke push for five times
print(“PUSHING”)
for a in range(5):
PUSH()
print(“DISPLAYING RECORDS”)
DISPLAY()

Ques-3.
(i)( host = “localhost”, user = “root”, passwd = “root”, database = “School” )
(ii)(“SELECT NAME, CLASS, GRADE FROM STUDENT WHERE TOTAL < 400”)
(iii)= cursor.fetchall()
(iv)#Statement 2
cursor.execute(“INSERT INTO STUDENT VALUES (“ST/15”, “AMRIT”, “12 D”, 496, “A+”)”)
# Statement 4mycon.commit(

You might also like