Please open Telegram to view this post
VIEW IN TELEGRAM
def add_item(val, target=[]):
target.append(val)
return target
print(add_item(1), add_item(2))
#Python
Please open Telegram to view this post
VIEW IN TELEGRAM
👍1
colors = ['red', 'green', 'blue']
colors.insert(1, colors.pop())
colors.reverse()
print(colors[0])
#Python
Please open Telegram to view this post
VIEW IN TELEGRAM
d = {'a': 1, 'b': 2}
try:
for k in d:
del d[k]
print("Done")
except RuntimeError:
print("Error")#Python
Please open Telegram to view this post
VIEW IN TELEGRAM
from collections import defaultdict
class Tree(defaultdict):
def __init__(self):
super().__init__(Tree)
t = Tree()
t['a']['b']['c'] = 10
t['a']['b']['d'] = 20
t['a']['e'] = 5
def count_leaves(d):
total = 0
for v in d.values():
if isinstance(v, dict):
total += count_leaves(v)
else:
total += v
return total
print(count_leaves(t))
#Python
Please open Telegram to view this post
VIEW IN TELEGRAM
Python Ready — авторский канал, где Python перестаёт быть только теорией и становится рабочим инструментом. Мини-проекты, боты, советы, разборы задач, гайды и шпаргалки для каждого программиста.
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
def f(x, y, z):
return x + y + z
d1 = {'x': 1, 'y': 2}
d2 = {'y': 3, 'z': 4}
try:
print(f(**d1, **d2))
except TypeError:
print("TypeError")
#Python
Please open Telegram to view this post
VIEW IN TELEGRAM