Python 3 Functions and OOPs
Python 3 Functions and OOPs
is _______.
prints all characters that are not vowels
The output of the expression [ chr(i) for i in [65, 66, 67] ] is _______.
['A', 'B', 'C']
Generators consume more space in memory than the lists. State if the statement
is True or false.
False
A generator function can have multiple yield expressions. State if the statement
is True or False.
True
m
function name and parameter list
er as
co
The elements of an iterator can be accessed multiple times. State if the
eH w
statement is True or False.
False
o.
rs e
The output of the expression {0 if i%2 ==0 else 1 for i in range(8)} is _______.
ou urc
{0, 1}
--------------------------
ar stu
2 2 3 2
Th
Which of the following keyword is used for creating a method inside a class ?
This study source was downloaded by 100000825773299 from CourseHero.com on 06-17-2021 04:02:40 GMT -05:00
https://www.coursehero.com/file/40256172/Python-3-Functions-and-OOPstxt/
def
Which of the following method is used by a user defined class to support '+'
operator?
__add__
Which methods are invoked on entering into and exiting from the block of code
written in 'with' statement?
__enter__, __exit__
--------------------------
The output of the expression '2' == 2 is _________.
False
hich of the keyword is used to display a customised error message to the user?
m
raise
er as
co
In which of the following scenarios, finally block is executed?
eH w
always
o.
Which of the following execption occurs, when a number is divided by zero?
rs e
ZeroDivisionError
ou urc
Which of the following exception occurs, when an integer object is added to a
string object?
TypeError
o
aC s
If a list has 5 elements, then which of the following exceptions is raised when
8th element is accessed?
IndexError
ed d
ar stu
--------------------------
Any Python Script can act like a Module. State if the statement is True or
Th
False?
True+
This study source was downloaded by 100000825773299 from CourseHero.com on 06-17-2021 04:02:40 GMT -05:00
https://www.coursehero.com/file/40256172/Python-3-Functions-and-OOPstxt/
element, randomly, from a given list of elements?
choice+
Which of the following statement retreives names of all builtin module names?
import sys; sys.builtin_module_names+
Which of the following is not a way to import the module 'm1' or the functions
'f1' and 'f2' defined in it?
import f1, f2 from m1+
Which of the following modules are used to deal with Data compression and
archiving?
All of those mentioned+
Which of the following module is not used for parsing command line arguments
automatically?
cmdparse+
/////////////////////////Asses 1
m
Which methods are defined in an iterator class?
er as
iter, next
co
eH w
Which of the following keyword is used for creating a method inside a class ?
def
o.
rs e
What is the output of the following code?
ou urc
(<class '__main__.child'>, <class '__main__.mother'>, <class '__main__.father'>,
Which methods are invoked on entering into and exiting from the block of code
aC s
__enter__, __exit__
docstr*
Th
Which of the following modules contain functions that create iterators for
efficient looping?
itertools
Which of the following module is not used for parsing command line arguments
automatically?
cmdparse
This study source was downloaded by 100000825773299 from CourseHero.com on 06-17-2021 04:02:40 GMT -05:00
https://www.coursehero.com/file/40256172/Python-3-Functions-and-OOPstxt/
What is the output of the following code?
2 2 3 2
The output of the expression {i:j for i in "abcd" for j in "kiwi"} is _______.
{'a': 'kiwi', 'd': 'kiwi', 'c': 'kiwi', 'b': 'kiwi'}*
Which of the following statement retreives names of all builtin module names?
import sys; sys.builtin_module_names
m
more than zero
er as
co
Which of the following exception occurs, when an integer object is added to a
eH w
string object?
TypeError
o.
//////////////Exercise1
rs e
ou urc
import math
class Point:
def __init__(self,x,y,z):
o
self.x=x
aC s
self.y=y
v i y re
self.z=z
def __str__(self):
display="point : ("+str(self.x)+","+str(self.y)+","+str(self.z)+")."
return display
def __add__(self,other):
ed d
return Point(self.x+other.x,self.y+other.y,self.z+other.z)
ar stu
def distance(Point1,Point2):
distance=math.sqrt( (Point1.x-Point2.x)**2 + (Point1.y-Point2.y)**2 +
(Point1.z -Point2.z)**2 )
return distance
sh is
p1=Point(5,5,5)
p2=Point(3,4,5)
Th
print(p1+p2)
//////////////Exercise2
import unittest
def isEven(x=2):
return x%2==0
class TestIsEvenMethod(unittest.TestCase):
def test_isEven1(self):
self.assertEqual(isEven(5),False)
def test_isEven2(self):
self.assertEqual(isEven(10),True)
def test_isEven3(self):
self.assertRaises("TypeError",isEven("hello"))
if __name__ == '__main__':
unittest.main()
This study source was downloaded by 100000825773299 from CourseHero.com on 06-17-2021 04:02:40 GMT -05:00
https://www.coursehero.com/file/40256172/Python-3-Functions-and-OOPstxt/
//////////////Exercise3
import sys
class Circle:
def __init__(self,radius):
if not (isinstance(radius,int)):
raise RadiusInputError(radius+" is not a number")
self.radius=radius
class RadiusInputError(Exception):
def __init__(self,value):
self.value=value
def __str__(self):
return str(self.value)
try:
#n = int(sys.stdin.readline())
#if not 0 <= n <= 100:
# raise ValueError('the number is not between 0 and 100')
#string=sys.stdin.readline()
#if(len(string)>10):
# raise ValueError('the string has more than 10 #characters')
m
er as
#open("hello.txt")
co
eH w
Circle("7")
o.
#except ValueError as e: rs e
# print(e)
ou urc
#except IOError as e:
# print("File not found")
except RadiusInputError as e:
print(e)
o
aC s
//////////////Exercise4
v i y re
import itertools as it
n=list([10,13,16,22,9,4,37])
even=[]
odd=[]
ed d
group=it.groupby(n)
ar stu
print(even,odd)
Th
//////////////Exercise5
import os
print ("*" * 20)
path="/home"
lstFiles=[]
lstDir=os.walk(path)
for root, dirs, files in lstDir:
for fichero in files:
(nombreFichero,extension)=os.path.splitext(fichero)
if(extension==".py"):
lstFiles.append(nombreFichero+extension)
print(lstFiles)
print("LISTADO FINALIZADO")
print ("longitud de la lista = ", len(lstFiles))
This study source was downloaded by 100000825773299 from CourseHero.com on 06-17-2021 04:02:40 GMT -05:00
https://www.coursehero.com/file/40256172/Python-3-Functions-and-OOPstxt/
//////////////Exercise6
import calendar
months_ans=[]
for month in range(1,13):
mycal=calendar.monthcalendar(2019,month)
if mycal[4][6]!=0:
months_ans.append(month)
print(months_ans)
//////////////Exercise7
import timeit
def f1():
x=list(range(1,21))
y=[i**2 for i in x]
return y
def f2():
x=list(range(1,21))
g=(i**2 for i in x)
return g
m
print(timeit.timeit(f1,number=100000))
er as
print(timeit.timeit(f2,number=100000))
co
eH w
//////////////Exercise8
o.
import cProfile rs e
def f1():
ou urc
x=list(range(1,200001))
y=[i**2 for i in x]
return y
def f2():
o
x=list(range(1,200001))
aC s
g=(i**2 for i in x)
v i y re
return g
print(cProfile.runctx("f1()",globals(),locals()))
print("\n"*2+"*"*20+"\n"*2)
print(cProfile.runctx("f2()",globals(),locals()))
ed d
ar stu
sh is
Th
This study source was downloaded by 100000825773299 from CourseHero.com on 06-17-2021 04:02:40 GMT -05:00
https://www.coursehero.com/file/40256172/Python-3-Functions-and-OOPstxt/
Powered by TCPDF (www.tcpdf.org)