Python Functions and Oop
Python Functions and Oop
zenPython = '''
The Zen of Python, by Tim Peters
words = zenPython.split()
print(len(words))
unique_words = set(words)
print(len(unique_words))
import math
class Point:
def __init__(self, x,y,z):
self.x, self.y, self.z = x,y,z
def __str__(self):
return "point : (%d, %d, %d)"%(self.x, self.y, self.z)
def distance(self, point):
return math.sqrt( (point.x - self.x)**2 + (point.y - self.y)**2 +
(point.z - self.z)**2 )
def __add__(self,punto):
return Point( self.x + punto.x, self.y + punto.y, self.z + punto.z)
p1 = Point(4, 2, 9)
print(p1)
p2 = Point(4, 5, 6)
p3 = Point(-2, -1, 4)
print(p2.distance(p3))
print(p2 + p3)
############################################333
import itertools
def even_or_odd(entero):
if entero % 2 == 0:
return 'even'
return 'odd'
#############################3333333
valor = input()
try:
entero = int(valor)
if not (entero in list(range(0,101)) ):
raise ValueError()
except ValueError as ex:
print('Input integer value must be between 0 and 100.')
#########################################3
import os
import os.path
def imprimirPYs(ruta):
for root, dirs, files in os.walk(path, topdown=False ):
files1 = [ x for x in files if x.endswith('.py') ]
for file in files1:
print(file)
for directorio in dirs:
imprimirPYs(directorio)
path = os.path.abspath(os.curdir)
imprimirPYs(path)
##################
valor = input()
try:
if len (valor) > 10:
raise ValueError()
print(valor)
except ValueError as ex:
print('Input String contains more than 10 characters.')
###########################
import calendar
c = calendar.Calendar(0)
print(lista)
###############################
def isEven(numero):
return ( numero % 2 ) == 0
print (isEven(43))
import unittest
class TestIsEventMethod(unittest.TestCase):
def test_isEven1(self):
self.assertEqual( isEven(5), False)
def test_isEven2(self):
self.assertEqual( isEven(10), True)
def test_isEven3(self):
with self.assertRaises( TypeError ):
isEven('hello')
unittest.main()
#############################################
###################################3333333333
#What is the output of the following code?
class A:
def __init__(self, x=5, y=4):
self.x = x
self.y = y
def __str__(self):
return 'A(x: {}, y: {})'.format(self.x, self.y)
def __eq__(self, other):
return self.x * self.y == other.x * other.y
def f1():
a = A(12, 3)
b = A(3, 12)
if (a == b):
print(b != a)
print(a)
f1()
############################################33
False
True
A(x: 12, y: 3)
True
False
A(x: 12, y: 3) ## Esta es la salida
################################################3
#What is the output of the following code?
class grandpa(object):
pass
class father(grandpa):
pass
class mother(object):
pass
class child(mother, father):
pass
print(child.__mro__)
####################################3333333
How are variable length non-keyword arguments specified in the function heading?
* Two stars followed by a valid identifier
Two underscores followed by a valid identifier
One underscore followed by a valid identifier
One star followed by a valid identifier
Which of the following modules are used to deal with Data compression and
archiving?
lzma
* All the options
tarfile
zlib
Which of the following module is not used for parsing command-line arguments
automatically?
* cmdparse
argparse
getopt
optparse
Which of the following keyword is used for creating a method inside a class?
method
sub
* def
class
Which of the following method is used by a user defined class to support '+'
operator?
plus
__plus__
* __add__
add
Which of the following statement retrieves names of all builtin module names?
import builtins; builtins.builtins_names
import builtins; builtins.module_names
import sys; sys.builtins_names
* import sys; sys.builtin_module_names
def f(self):
print(float())
print(hex(-255))
class B(A):
def __init__(self):
print('two')
def f(self):
print(float())
print(hex(-42))
########################
x = B()
x.f()
one
0.0
-0xff
two
0.0
-0xff
one
0.0
-0x2a
* two
0.0
-0x2a
#############################################33
What is the output of the following code?
class A:
x = 0
def __init__(self):
A.x += 1
def displayCount(self):
print('Count : %d' % A.x)
def display(self):
print('a :', self.a, ' b :', self.b)
a1 = A('George', 25000)
a2 = A('John', 30000)
a3 = A()
a1.display()
a2.display()
print(A.x)
a : George b : 25000
a : John b : 30000
3
a : George b : 25000
a : John b : 30000
Results in Error
a : George b : 25000
a : John b : 30000
2
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
from m1 import *
import m1
from m1 import f1, f2