0% found this document useful (0 votes)
14 views6 pages

Computer SC (12) Autumn Break Homework

Uploaded by

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

Computer SC (12) Autumn Break Homework

Uploaded by

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

AUTUMN BREAK HOMEWORK

Functions
FILE HANDLING
Q:1 Write a function countINDIA() which read a text file ‘myfile.txt’ and print the frequency of the
words ‘India’ in it (ignoring case of the word).
Example: If the file content is as follows:
INDIA is my country. I live in India. India has many states.
The countIndia() function should display the output as: Frequency of India is 3

Q:2 Vishnu is a Python programmer. He has written a code and created a binary file record.dat with
studentid, subjectcode and marks. The file contains 10 records. He now has to update a record based on
the studentid id entered by the user and update the marks. The updated record is then to be written in
the file temp.dat. The records which are not to be updated also have to be written to the file temp.dat. If
the student id is not found, an appropriate message should to be displayed.
As a Python expert, help him to complete the following code based on the requirement given above:
import _______ #Statement 1
def update_data():
rec={}
fin=open("record.dat","rb")
fout=open("_____________") #Statement 2
found=False
sid=int(input("Enter student id to update his marks :: "))
while True:
try:
rec = ______________ #Statement 3
if rec["studentid"]==sid:
found=True
rec["marks"]=int(input("Enter new marks :: "))
pickle.____________ #Statement 4
else:
pickle.dump(rec,fout)
except:
break
if found==True:
print ("The marks of studentid ", sid ," has been updated.")
else:
print("No student with such id is not found")
fin.close()
fout.close()
(i) Which module should be imported in the program? (Statement 1)
(ii) Write the correct statement required to open a temporary file named temp.dat. (Statement 2)
(iii) Which statement should Aryan fill in Statement 3 to read the data from the binary file, record.dat
and in Statement 4 to write the updated data in the file, temp.dat?
Q:3 A pre-existing text file data.txt has some words written in it. Write a python function displaywords()
that will print all the words that are having length greater than 3.
Example:
For the fie content:
A man always wants to strive higher in his life
He wants to be perfect.
The output after executing displayword() will be: Always wants strive higher life wants perfect
Q:4 Sudheer has written a program to read and write using a csv file. He has written the following code
but failed to write completely, leaving some blanks. Help him to complete the program by writing the
missing lines by following the questions a) to d).

_________________ #Statement 1
headings = ['Country','Capital','Population']
data = [['India', 'Delhi',130],['USA','Washington DC',50],[Japan,Tokyo,2]]
f = open('country.csv','w', newline="")
csvwriter = csv.writer(f)
csvwriter.writerow(headings)
________________ #Statement 2
f.close()
f = open('country.csv','r')
csvreader = csv.reader(f)
head = _________________ #Statement 3
print(head)
for x in __________: #Statement 4
if int(x[2])>50:
print(x)

a) Statement 1 – Write the python statement that will allow Sudheer work with csv files.
b) Statement 2 – Write a python statement that will write the list containing the data available as a
nested list in the csv file
c) Statement 3 – Write a python statement to read the header row in to the head object.
d) Statement 4 – Write the object that contains the data that has been read from the file.

Q:5 Write a Program in Python that defines and calls the following user defined functions:
(i) add() – To accept and add data of an employee to a CSV file ‘furdata.csv’. Each record consists of a
list with field elements as fid, fname and fprice to store furniture id, furniture name and furniture price
respectively.
(ii) search()- To display the records of the furniture whose price is more than 10000.

Q:6 Write a function in Phyton to read lines from a text file visiors.txt, and display only those lines,
which are starting with an alphabet 'P'.
If the contents of file is :
Visitors from various cities are coming here.
Particularly, they want to visit the museum.
Looking to learn more history about countries with their cultures.
The output should be:
Particularly, they want to visit the museum.
DATBASE & SQL

You might also like