TOPIC - PYTHON LIBRARY
Q. No Part A Marks
1. Which python libraries/module need to be imported to use the 1
following functions:
(i)float() (ii) randint()
2. Which python libraries/module need to be imported to use the 1
following functions:
(i)range
3. Which python libraries/module need to be imported to use the 1
following functions:
(i)str ()
4. What would be output produced by following code: 1
import math
import random
print(math.ceil(random.random()))
5. Name the Python Library modules which need to be imported to 1
invoke the following functions :
1. load ()
2. pow ()
6. Name the Python Library modules which need to be imported to 1
invoke the follwing functions :
1. sqrt()
2. dump()
QUESTIONS ON TEXT FILE HANDLING
9 A text file “Quotes.Txt” has the following data written in it: 3
Living a life you can be proud of
Doing your best
Spending your time with people and activities that are important to you
Standing up for things that are right even when it’s hard
Becoming the best version of you
Write a user defined function to display the total number of words
present in the file.
10 Write a method in python to write multiple lines of text contents into a 3
text file myfile.txt
11 Write a python program to copy file1.txt into file2.txt 3
12 Which of the following function returns a list datatype 1
A) d=f.read()
B) d=f.read(10)
C) d=f.readline()
D) d=f.readlines()
13 Write a single loop to display all the contens of a text file file1.txt after 2
removing leading and trailing WHITESPACES
14 Observe the following code and answer the follow 2
f1=open("mydata","a")
______#blank1
f1.close()
(i)what type of file is mydata
(ii) Fill in the blank1 with statement to write "abc" in the file "mydata"
15 A given text file data.txt contains : 2
Line1\n
\n
line3
Line 4
\n
line6
What would be the output of following code?
16 In which of the following file modes the existing data of the file will not 2
be lost?
i) rb
ii) w
iii) a+b
iv) wb+
v) r+
vi) ab
vii) w+b
viii)wb
ix) w+
17 What would be the data types of variables data in following statements? 2
i) Data=f.read( )
ii) Data=f.read(10)
iii) Data=f.readline()
iv)Data=f.readlines()
ANSWERS
9 def countwords():
S=open(“Mydata”, “r”)
f=S.read()
z=f.split()
count=0
for I in z:
count=count+1
print(“Total number of words”,count)
10 def write1():
f = open("myfile.txt","w")
while True:
line = input("Enter line")
f.write(line)
choice = input("Are there more lines")
if choice == "N":
break
f.close()
11 fin=open("file1.txt")
fout=open("file2.txt","w")
data=fin.read()
fout.write(data)
fin.close()
fout.close()
12 d) f.readlines()
13 for line in open(“file1.txt”):
print(line.strip())
14 i) Text file
ii) f1.write(“abc”)
15 Line1
Line3
Line 6
Line 4
16 ab and a+b mode
17 a) string b)string c)string d)list
QUESTIONS ON CSV FILE HANDLING
Q1. Write the full form of CSV
ANS Comma Separated value
Q2. A _________ is a file format which stores records separated by comma.
ANS .CSV
Q.3 One row of CSV file can be considered as _______ in terms of database
ANS Record
Q4 The writerow() function is a part of _________ module.
ANS CSV
Q5 A _____ function allows to write a single record into each row in CSV file.
ANS writerow()
Q6 Which of the following is not a function of csv module?
1. readline()
2. writerow()
3. reader()
4. writer()
ANS readline()
Q7 CSV module allows to write multiple rows using ____________ function.
ANS writerrows()
Q8 Write to ways to import a csv module.
ANS 1. import csv
2. from csv import *
Q9 Write the functions required to handle CSV files.
ANS To handle CSV files following function required:
1. Open()
2. reader()
3. writer()
4. writerow()
5. close()
Q10. The default line terminator is \n in csv file.
ANS True