[go: up one dir, main page]

0% found this document useful (0 votes)
4 views2 pages

Assignment 1

Uploaded by

Sanchita C
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)
4 views2 pages

Assignment 1

Uploaded by

Sanchita C
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/ 2

Assignment

1. Write a method COUNTLINES() in Python to read lines from text file ‘POEM.TXT’ and display
the number of lines which are not starting with character ‘J’
Example:
If the file content is as follows:
Jack and Jill went up the hill
To fetch a pail of water;
Jack fell down and broke his crown,
And Jill came tumbling after.
The COUNTLINES() function should display the output as:
The number of lines not starting with J : 2
2. Write a function WCount() in Python, which should read a text file “TESTFILE.TXT” and then count
and display the count of occurrence of word ‘today’ or ‘Today’.
Example:
If the file content is as follows:
Today is a pleasant day.
It might rain today.
It is mentioned on weather sites that;
There are chances of 1mm rain today.
The WCount() function should display the output as:
Count of word today or Today is : 3

3. Write a user defined function in python, Push(Gitem) where, Gitem is a dictionary containing details
of a grocery items: {Gname:price}.
The function should push the names of those items in the stack that have priced greater than 50. Also
display the count of elements pushed into the stack.
For example:
If the dictionary contains the following data:
Gitem={"Milk":80,"Curd":40,"Pasta":120,"Noodles":45, “Eggs”:100}
The stack should contain
Eggs
Pasta
Milk
The output should be:
The count of elements in the stack is:3
4. A list contains following record of a student:
[Student_name, Marks, Div]
Write the following user defined functions to perform given operations on the stack named ‘status’:
(i) Push_element() - To Push an object containing name and Marks of students who are from Div A
to the stack
(ii) Pop_element() - To Pop the objects from the stack and display them. Also, display “Stack Empty”
when there are no elements in the stack.
For example:
If the lists of student details are:
[“Aashish”, 99, “A”]
[“Shriya”, 88, “B”]
[“Om”,77, “C”]
[“Monika”, 90, “A”]
The stack should contain
[“Monika”, 90]
[“Aashish”, 99]
The output should be:
[“Monika”, 90]
[“Aashish”, 99]
Stack Empty
5. Write a Program in Python that defines and calls the following user
defined functions:
ADD() – To accept and add data of an employee to a CSV file ‘record.csv’. Each record consists of
a list with field elements as empid, name and designation to store employee id, employee name and
designation respectively.
COUNTR() – To count the number of records present in the CSV file named ‘record.csv’ having
designation as ‘Manager’.
6. Give any one point of difference between a binary file and a csv file. Write a Program in Python that
defines and calls the following user defined functions:
add() – To accept and add data of a student to a CSV file ‘sdata.csv’. Each record consists of a list
with field elements as sid, sname and smarks to store id, name and marks of student respectively.
search()- To display the records of the students whose marks are more than 70.
7. Akshay is a Python programmer. He has written a code and created a
binary file student.dat with sid, sname and marks. The file contains 10 records.
He now has to update a record based on the student 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
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("student.dat","rb")
fout=open("_____________") #Statement 2
found=False
sid=int(input("Enter student id to update their marks :: "))
while True:
try:
rec=______________ #Statement 3
if rec["sid"]==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 student id ",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? (Statement1)
(ii) Write the correct statement required to open a temporary file named temp.dat. (Statement 2)
(iii) Which statement should Akshay 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?

You might also like