Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
def modify(x, y=[]):
y.append(x)
return sum(y)
result = modify(5) + modify(7) + modify(4)
print(result)
#Python
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
class Matrix:
def __init__(self, data):
self.data = data
def __add__(self, other):
return Matrix([[self.data[i][j] + other.data[i][j]
for j in range(len(self.data[0]))]
for i in range(len(self.data))])
def sum(self):
return sum(sum(row) for row in self.data)
m1 = Matrix([[1, 2], [3, 4]])
m2 = Matrix([[5, 6], [7, 8]])
result = m1 + m2
print(result.sum())
#Python
Please open Telegram to view this post
VIEW IN TELEGRAM
def make_counter():
count = 0
def increment():
nonlocal count
count += 1
return count
return increment
c1 = make_counter()
c2 = make_counter()
c1()
print(c1(), c2())
#Python
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
d = {'a': 1, 'b': 2, 'c': 3}
try:
for k in d:
del d[k]
print(len(d))
except RuntimeError:
print("RuntimeError")#Python
Please open Telegram to view this post
VIEW IN TELEGRAM
from itertools import islice
def infinite_seq():
n = 1
while True:
yield n
n *= 2
gen = infinite_seq()
result = list(islice(gen, 5))
print(sum(result))
#Python
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
d = {'a': 1, 'b': 2, 'c': 3}
try:
for k in d:
del d[k]
print(len(d))
except RuntimeError:
print("RuntimeError")#Python
Please open Telegram to view this post
VIEW IN TELEGRAM