Algorithms, functions, classes, regular expressions, iterators, generators, OOP, exceptions, NumPy, pandas, scikit-learn
https://telega.in/c/python_tasks
Questions — @dina_ladnyuk
Post #2321
1.36K
PY @python_tasks
Showing posts older than #2322 · Back to latest
class MyExc(Exception):
def __str__(self):
return 'my-exc'
try:
raise MyExc()
print('try')
except MyExc as err:
print(err)
else:
print('else')
val = # ваш код
shoes = {'name': 'fashion shoes', 'price': 14900}
def discount(item, discount):
price = int(item['price']) * (1.0 - discount)
assert 0 <= price <= item['price']
return price
discount(shoes, val)
sum([3 >> i for i in range(200)])
class Error(Exception): pass
def handle(file):
raise Error()
file = open('mydata', 'w')
try:
handle(file)
print('one')
finally:
file.close()
print('two')
class A:
def message(self):
print('1', end='')
def __enter__(self):
print('2', end='')
return self
def __exit__(self, exc_type, exc_value, exc_tb):
if exc_type is None:
print('3', end='')
else:
print ('4', end='')
return True
with A() as action:
action.message()
def f(*seqs):
seqs = [list(s) for s in seqs]
while all(seqs):
yield tuple(s.pop(0) for s in seqs)
s1, s2 = 'ab', '123'
print(list(f(s1, s2)))
x = 0
try:
x += 1
try:
x += 1
1/0
except ZeroDivisionError:
x += 1
except ZeroDivisionError:
x += 1
else:
x += 1
print(x)
sum([1 << i for i in range(5)])
d = {'a': 1, 'b': 2, 'c': 3}
x = [string for string, digit in d.items() if digit > 1]
print(x)x = 0
try:
x += 1
except ZeroDivisionError:
x += 1
else:
x += 1
1/0
x += 1
print(x)