Найти в Дзене
10 подписчиков

📑 Dictionary Comprehensions

📑 Task
1) What two numbers will the program print?
2) Replace the first five lines of the program with a single line
containing a dictionary comprehension

dict_ = {}
for i in range(3):
dict_[i] = i
dict_[0] += 1
dict_ = {**dict_, **{i: i ** 2 for i in range(3, 5)}}
print(sum(dict_), sum(dict_.values()))

📑 Answer
1) 10 29
The first number is the sum of the keys of the resulting dictionary
The second number is the sum of its values
2)

dict_ = {i: i ** ((i + 1) // 2) for i in range(5)}
print(sum(dict_), sum(dict_.values()))
📑 Dictionary Comprehensions python-puzzles.blogspot.com/...tml 📑 Task 1) What two numbers will the program print?
Около минуты