0% found this document useful (0 votes)
12 views17 pages

Queriesrehnat

queries of rehnat

Uploaded by

Rehnat Soni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views17 pages

Queriesrehnat

queries of rehnat

Uploaded by

Rehnat Soni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Q) Write a program to create a CSV file to store student data (Roll

no.,Name, Marks).Obtain data from user and write 5 records into the file

import csv

fh=open("Student.csv","w") #open file

stuwriter=csv.writer(fh)

stuwriter.writerow(['roll no','name','marks']) #write header row

for i in range(5):

print("student record",(i+1))

rollno=int(input("enter roll no:"))

name=input("enter name")

marks=float(input("enter your marks:"))

sturec=[rollno,name,marks] #create sequence of user data

stuwriter.writerow(sturec)

fh.close() #close file


Q)Consider the binary file Student.csv storing sudent details,which you
created in earlier programs.Write a program to uptade the records of file
Stu.dat so that those who have scored more than 81,get additional bonus
marks of 2

import pickle

stu={} #declare empty dictionary object to hold read records

found=False

#open binary file in read and write mode

fin=open("Stu.dat","rb+")

#read from the file

try:

while True:

rpos=fin.tell() #store file pointer position before reading the


record

stu=pickle.load(fin)

if stu["Marks"]>81: #changes made in record ;2 bonus marks


added

stu["marks"]+=2 #place the file pointer at the exact location


of record

fin.seek(rpos) #now write uptated record on the exact


location

pickle.dump(stu,fin)

found=True

except EOFerror:

if found==False:

print("sorry,no matching record file found.")

else:
print("Record(s)successfully updated.")

fin.close() #close file


Q)Write a program to open file Student.csv and search for records with
roll numbers as 12 or 14.If found,display te records.

import pickle

stu={} #declare empty dictionary object to hold to read records

found=False

fin=open('Student.csv','rb+') #open binary file in read mode

searchkeys=[12,14] #list contains key values to be searched for

#read from the file

try:

print("Searching in FileStudent.csv...")

while True: #it will become False upon EOF


EXCEPTION

stu=pickle.load(fin) #read the record in stu dictionary from


fin file handle

if stu["roll no"]in searchkeys:

print(stu) #print the record

found=True

except EOFError:

if found==False:

print("no such records found in file")

else:

print("search successfull.")

fin.close() #close file


Q)Consider the following code

import math

import random

print(str(int(math.pow(random.randint(2,4),2))),end=””)

print(str(int(math.pow(random.randint(2,4),2))),end=””)

print(str(int(math.pow(random.randint(2,4),2)))

What could be the possible outputs out of given four choices ?

(i)2 3 4 (ii)9 4 4 (iii)16 16 16 (iv)2 4 9 (v) 4 9 4 (vi)4 4 4


Q)What possible outputs are expected to be displayed on screen at the
time of execution of program from following code?Also specify minimum
and maximum values that can be assigned to the variable End.

Import random

Colurs
=["VIOLET”,”INDIGO”,”BLUE”,”GREEN”,”YELLOW”,”ORANGE”,”RED”]

End=randrange(2)+3

Begin=randrange(End)+1

for i in range(Begin,End):

print(Colours[i],end=”&”)
Q)Write a program to get roll numbers,names,and marks of students of a
class (get from the user)and store these details in a file called
“Student.csv”

count=int(input("how many students are there in the class?"))

fileout=open("Student.csv","w")

for i in range(count):

print("Enter details for student",(i+1),"below:")

rollno=int(input("Rollno:"))

name=input("Enter name;")

marks=float(input("Marks:"))

rec=str(rollno)+","+name+","+str(marks)+"\n"

fileout.write(rec)

fileout.close()
Q)Find and write the output of following python code:

def ChangeVal(M,N):

for i in range(N):

if M[i]%5==0:

M[i]//=5

if M[i]%3==0:

M[i]//3

L=[25,8,75,12]

ChangeVal(L,4)

for i in L:

print(i,end="#")

Q)Find and write the output of the following code :

def Change(P,Q=30):

P=P+Q

Q=P+Q

print(P,"#",Q)

return(P)

R=150

S=100

R=Change(R,S)

print(R,"#",S)

S=Change(S)
Q)Write a Python program to open the file hello.txt used in question 6 in
read mode to display its contents .Whta will be the difference if the file
was opened in write mode instead of append mode

f=open("hello.txt","r")

st=""

while st:

st=f.readline()

print(st)

f.close()
Q)Write a program to accept strings/sentences from the user till the user
enters “END” to.Save the data in a text file and then display only those
sentences which begin with an uppercase alphabet

f=open("new.txt","w")

st=""

while st!="END":

st=input("Enter next line :")

f.write(st+"\n")

f.close()

f=open("new.txt","r")

st=""

while st:

st=f.readline()

if st:

if st[0].isupper():

print(st)

f.close()

You might also like