0% found this document useful (0 votes)
43 views

Revision Assignment CS

Revision assignment cs.

Uploaded by

roshangandhi95
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)
43 views

Revision Assignment CS

Revision assignment cs.

Uploaded by

roshangandhi95
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/ 13

Assertion and reason

(a)Both (A) and (R) are true and (R)is the correct explanation for (A).

(b) Both (A) and (R) are true and (R)is not the correct explanation for (A).

(c) (A)
is true but (R) is
false.

(d) (A) is false but(R) is true

L. ASsertion (A) :CSV file is a human readable text file where each line has a number of fields, separated
by comma or some other delimiter. Reason

(R): writerow) method is used to write a singie row in a CSV fiie.

2. Assertion (A):The expression "Hello".sort() in Python will give an error.

Reason (R): sort() does not exist as a method/function for strings in Python.

3.Sangeetais a Python programmerworking in a computer hardware company. She has to maintain the
records of the peripheral devices. She created a csv file named Peripheral.csv, to store the details.

The structure of Peripheral.csv is: [P_id,P_name,Price]

where P_id isPeripheral device ID (integer)

P_name is Peripheral device name (String)

Price is Peripheral device price (integer)

Sangeetawants to write the following user definedfunctions:

from the user and add it to a csv file, Peripheral.csv


Add_Device() :to accept a record

count and number peripheral devices whose price is less than 1000.
Count Device():To display of

a binary file, items.dat, containing records stored in the given format: fitem id:
4, Consider
name,amount]}Write a function,Copy_new),that copies all records whose amount is greater
fitem

than 1000from items.dat to new_items.dat

be evaluated to in Python
?
5. What will the following expression

A=(6/3 + 4**3//8- 4)

Print(A)

(R) is the correct explanation for(A).


(a) Both (A) and (R)are true and
for (A).
not the correct explanation
(A) and (R) are true and (R)
is
(b) Both

false.
(c) (A) is true but (R) is

(R) is true.
(d) (A) is false but
O. ASsertion (A): readlines() reads all the lines from a text file and returns the lines along with newline as
a list of
strings.

Keasoning (R) :readline() can read the entire text file line by line without using any looping statements.

6. Ravi, a Pythonprogrammer, is working on a nroiect he wants to


in which write a TunctIon to count
tne number of even andodd values in the list. He has written the foilowing code but his code is having
errors. Rewrite the correct code and
underline the corrections made.

define EOCOUNT (L) :


even no=odd no=0
£or i in range (0,lén
if L[i]%2=0:
(L) )
even no+*1
Else :
odd no+=1
print (even no, odd no)

7. Given isa Python string declaration :

NAME ="Learning Python.is Fun"

Write the output of :


print(NAME[-5:-10:-1])

8.

Write the output of the code given below :


ldictl={1: ["Rohit",20], 2: ("Siya",90]}llnb
dict2={l:["Rahul", 95), 5:["Rajan",80])
sseu aniwolotet ay
dictl. update (dict2)
print (dicti.values () )
9.

Write the output of the Python code given beiow :


g=0 23
def funl (x,y):
global g
g=x+y 5
return Se
def fun2 (m,n):
global g
g=m -2
return g
k=fun1 (2,3) k5
fun2 (k,7)
print (g) -2
10.

Write the output of the Python code given below :


|a=15
def update(x):
gìobai a
a+=2
if x%2==0:
at=x
else:
a//=x
ama+5 1515
print (a,end="$")
update (5)
Iprint (a)
11.

Write a function in Python that displays the book names having "Y'or 'y'in their name
|froma text file "Bookname.txt".
Exampie :
|If the file "Bookname.txt" contains the names of following books :

One Hundred Years of Solitude


The Diary of a Young Girl
On the Road
After execution, the output will be:

|One Hundred Years of Solitude


|The Diary of a Young Girl

12.

the words starting


Write function Revstring() to read a textfile "Input. txt" and prints
a

normally.
|with "0' in reverse order. The rest of the content
is displayed

Example :

if contentin the text file is:


UBUNTU IS AN OPEN SOURCE OPERATING SYSTEM

|Output will be :
UBUNTU IS AN NEPO SoURCE GNITAREPO SYSTEM

'OPEN' and 'OPERATING' are disnlaved in reverse order!


words

13.
Write a
function
search replace () Python which accepts a list L ofnumbers and a
number to be searched. in

not exist, an
If the
number exists, it is replaced by 0 and if the number does
appropriatemessage is displayed.

Example:
L= [10,20,30,10,40)
Number to be
searched = 10
|List after
replacement :
L = O.20.30.0.401
i4.

iü)Predict the output of the


code givenbeiow :
text="Learningcs"
L=len (text) 0
ntextn
for i in range (0,L): 0-9
if teyti].islower{):
ntext=ntext+text[i].upper ()
elif text [ij.isalnum ():
ntext=ntéxt+text {i-l] SE ARNINGgC
else:
ntext=ntext+'&&!
print (ntext)
15.

Write a point of difference between append (a) and write (w) modes in a text file.

write a program in Python that defines and calis the fotiowing user defined
la Add Teacher() :It accepts the values from the user and inserts record
functions
of a
:
teacher
toa csv file Teacher.csv'. Each record consists of a list with field elements as
T id,Tname and desig to store teacher D, teacher name and designation
respectively.

Jti) :
Search_Teacher() To display the records of alt the PGT (designation) teachere

16.

jWrite a program in Python that defines and calls the following user defined functions :
li) Add_ Device() :The function accepts and adds records of the peripheral devices toa
csy file 'peripheral.csv'. Each record consists of a list with field elementsas p id
P name and Price to store peripheral device lD, device name, and price

respectively.
Já) Count_Device() : To count and display number of peripheral devices, whose price is

less than 1000.


17.

Given the following Tuple


Tup- (10, 20, 30, 50)
Which of the following statements will
result an error ?
in
(a) print (Tup[0]) (b) Tup.insert (2,3)
(c) print (Tup[1:2]) (d) print (len (Tup))
18.

Which of the following statement(s) would give an error after


executing
the following code ?
Stud={"Murugan":100, "Mithu":95) # Statement 1
print (Stud[95]) Statement 2 #

Stud ["Murugan"]=99 # Statement 3


print (Stud.pop ()) # Statement 4
print (Stud) # Statement 5

(a) Statement 2 (b) Statement3


(c) Statement4 (d) Statements 2 and 4

19.

What will the following expression be evaluated to in Python ?


print (4+3*5/3-5%2)
20.

(a) Both (À)and (R)are true and (R) is the correctexplanation for (À).
(b) Both (A) and (R) are true and (R) is not the correct explanation for
(A).

(c) (A) is true but (R)is false.


(d) (A) is false but (R) is true.

(A): To use a function fromn a particular module, we need to


Assertion
import the module.
:
Reason (R) import statement can be written anywhere in the program,
that module.
before using a function from

21.
Atharva is a Python tofind and return
the maximum value programmer working on a program
from the list. The code written below has syntactical
errors. Rewrite the correct code and underline the corrections made.
def max num (L) :
max-L (0)
for a in L :
if a> max
max=a
return maX
22.

Given is a Python list deciaration :


Listofnames= ["Aman","Ankit","Ashish","Rajan","Rajat"]
Write the output of :
print (Listofnames (-1:-4:-1])
Page No:

Harion uigmernt
(b) Beth (A) and (R) ase thue lut (R)anet sthe

2 a) Both A) and R) are. true and (R)iu the cesseot

3 name PesibhesalL.csy
btructue IPidPhdme, Price

(aldddevice CO

import CSV
def Add_
f=iopenG
p
Device ():

csV. Ariter
Peripheral.
f)
csu', 'a'neviliae = )
while True:

id = int (input ("Enter the peripheripal Id ")


input" Euten the name:")
price = int Cinput.("Entir the Periphe ral price:")

data.anLidy nayse ipnice]

p. write rol (data).

ch =iapuE ("Do you ant to enter more: :(yla)>

If ch a= N' or chas h'invgn

Add
f.close()Joh
Device )
aoa
Page No

(bCount levice O:

import
def
csV

Count

fraopen
Device (): no hA
("Peripheral.csu''ni)
A

csv. reader )
C)

hext (r)
for i in ri

Litilal< 1o004
print (i)
c+=1

print ("No records found")


else:

(lprint(" Nuimber af periphe iral devices:",c)

f.close()
CountiDeice ()ada")

4.
athuctse = itenid iteiname,amount J

w importpickle na
def Copy ne iaO:ha 4'r
fl= open items dat""rb)
23 open
Count=0
"ne-ikems dat "abos
try
nhile True:

data bickle.load ($)


{for ini data. value s ):
v[1l >1000c)
,
if

pickle.dump Cdata f2).: it

cOunt t =1

tiExceptoirA"apdog1DN
fl.cdose )
42. close )
retunh count

Copyhen()

5. A (6/3 + 4** 3g- 4).

sl3+ 64lg- 4

2.0 + 4l8-4
2.0+ 8-4
10.0- 4

6,0

but R) iu

S define EOCOunT CL)E K0nn#def:(erron1)


even-no = odd_no =0, ixtnggo
range (0,len (L)E #colon Cerror 2)
for i in bn

if Lil220: 041#E) (error3)


evennot =1
Elsei :ofoli # else: (emor4).
oddno + = 1

print (evenno, oddLno)ut


7 Nanme = "Learing Python isEun"2
print (Name [-5:-10:-1])

8. dict- values (Ir"Rah) ",451,[" Siya 401, Rajan", 80])

q.-2.

1o.20 $4

Adef func():
f= open (" Bookname.txt" 'n!) sli
defreadlines ()
for i in d:

if 'y in i on 'y' in ii

print ()
f.close (O

func )
a def RevlString (O1

fz open ("Input.txt"r' hkas


eldafnead ()

loa) he) displit 0


for i' in pi

ke if::-11
print (kendal)
else:

print i =")
end
fclose()
Rev Strin O)
Paga No:
Date:

13def search replace (C,SN):)


=0
found
(Hfori inrange (len(1.)): bi

oi found.e1 hilntoh
iffound
d o) dunprint('Nümben not found)
else :
print (L),nad
L= [10, 20, 30,40, 40]
SN=10
search replace (L,SN)

1943
14SEARNIN Gg C.

Lappend mede Ca):


15.
Je LGpen the lile to wsite thecEntentat
te battam Css emd af eitingcentent.

Luseite mode(w)
De ereate a
to iseta te eomtet

it abay eristr.
CSV
(0iaport
def Add. Teacher ):
(" Teacher. csv"'a', newline )
pa csV f).
riter (f)
hiriter taho A

tohtte ruei

headings "Iid"Taame" "desia"l


Page No

Date:

p.Jrite roja (headins ) anlaos


ahile Tpuei
id = int±(input("Enterthe Idu)
")
Mame = in.put"Enter the name
desiq = input ("Enter the desiqnatio)
data=lid, hame,desiqJ
þ. write ron Cdata). haug
Chachiaput (Do yougant to ente n more
if che= 'N' or chèa
break.)taig
close O).

AddTeacher (1

(ildef Search Teacber ():


f open ("Teacher,cSv",'r)
d= csu. reader E).

hext (d)
for iin dih
it iL2]=="pI":
print (i)
fuclose).
SearchTeachar(O

16
(Repeat-edLquatin)(Sane as Ques 3).

b) Tup.insert (2,3)

ase imutable
Stateenti 2 and 4.

Stateunt Qi 95 is net the dictimany's ke4


Statesestt 4: )
gep () metthod sequies itemis key

19)
4+3*5/3 -5%2.
4+ 15/3-5%o2
4+ 5.0 -5 %2
4+ 5.0- 1

9.0 -1
8.0

20)- (Q Beth (A)Land (R) ae thue btt (R)is et the

ceet eeplamatn ef A).

21)- def maxnum ):


max = L(0) #L[Dl (Error1).
in L:
for a
#colon. (Error2)
ma =0 #inde ntation CError 3)
retun: max,

22."Rajat""Rajan"Ashish"]

You might also like