Practise qus Flashcards
Class A:
A = 1
def __init__(self):
self.a = 0
print(hasattr(A,’a’)
False
try:
raise Exception
except:
print(‘c’)
except Exception:
print(‘b’)
Syntax Error
as except should be last branch
try:
raise Exception(1,2,3)
except Exception as e:
print(len(e.args))
3
Class A:
def __init__(self, v):
self.__a = v+1
a = A(0)
print(a.__a)
Attribute Error
__a private so have to be called like
_A__a
class A:
def a(self):
print(‘a’)
class B:
def a(self):
print(‘b’)
class C(B,A):
def c(self):
self.a()
o = C()
o.c()
b
try:
raise Exception
except BaseException:
print(‘c’)
except Exception:
print(‘b’)
except:
print(‘h’)
c
Base Exception is main exception
assert var != 0
will stop the program when var == 0
as raises exception with var !=0 == False
my_str = ‘race’
my_str = my_str.copy()
AttributeError
my_str = ‘race’
my_str = my_str.insert(3,q)
Error
for i in range(10):
pass
print(i)
9
list1= [i for i in range(1,10)]
list2 = list1[-1:1:-1]
[9, 8, 7, 6, 5, 4, 3]
Remember the end range value is not included therefore the list reverses and stops at index = 2
Can tuples store tuples?
yes
Can tuples store lists?
yes
What can you deduce from the following statement?
(Select two answers)
str = open(‘file.txt’, “rt”)
a) str is a string read in from the file named file. txt
b) a new line character translation will be performed during the reads
c) if file. txt does not exist, it will be created
d) the opened file cannot be written with the use of the str variable
b,d
The first parameter of each method:
a) holds a reference to the currently processed object
i = 5
while i>0:
i = i//2
if i % 2 = 0:
break
else:
i+=1
print(i)
should be:
i % 2 == 0
i = 9
i =+ 2
What does i equal?
2
as i = +2
just means i equals positive 2
to add you need +=
for i in range(1,3):
print(‘ * ‘,end=’’)
else:
print(‘ * ‘)
Star star star
* * *
def f(n):
for i in range(1,n+1):
yield I
print(f(2))
_______________________________________
def f(n):
for i in range(1,n+1):
yield I
print([i for i in f(2)])
What is the difference in these two outputs? What do you need to remember?
<generator object f at 0x7f53e1f7fc10>
__________________________
NameError as I is not defined
you need to loop through generators
How many stars () does the snippet print?
s = ‘****’
s = s - s[2]
print(s)
Error
strings support concatenation. ‘-‘ is not supported in strings
Which can you do to this tuple: tup = ()
a) tup[:]
b) tup.append(0)
c) tup[0]
d) del tup
a and d
issubclass(B,B)
True
class C():
Z = 15
def __init__(self):
self.c = 2
def multiplier(self):
self.c *= 2
y = C()
x = C()
x.multiplier()
y.multiplier()
y.multiplier()
print(x.c)
4
class C():
Z = 15
def __init__(self):
self.c = 2
def multiplier():
self.c *= 2
y = C()
x = C()
x.multiplier()
y.multiplier()
y.multiplier()
print(x.c)
Type error as no self parameter in multiplier
class A:
X=0
def __init__(self):
self.x = 5
class B(A):
Y=1
def __init__(self):
self.y = 7
a = A()
b = B()
Which of the following do NOT output False?
A. isinstance(A,A)
B. isinstance(A.X,A)
C. isinstance(b,A)
D. isinstance(a,A)
E. isinstance(B,b)
C. isinstance(b,A) Ture
D. isinstance(a,A) True
E. isinstance(B,b) Error
When will isinstance() error?
When the second argument is not a class
‘a’*0
’’
len(234)
TypeError: object of type ‘int’ has no len()
1//2
0
-1//2
-1
class C():
__C=3
def \_\_string_amplify(self,char='b',times=1): self.a += char*times
c = C()
c.__string_amplify(‘d’, 2)
Error
needs to be c._C__string_amplify(‘d’, 2)
LOOK OUT FOR PRIVATE VARIABLES!!!!!!
CAN ONLY CALL THEM _CLASS__VARIABLE
How do you make a class an exception?
class my_exception(Exception)
my exception is now a subclass of Exception
you can raise it now
can make it a subclass of any Error, e.g. LookUpError