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

📑 A Series of Variables

📑 Task
1) What will the program print for each case?
2) How to change f_string_y in the program so that variables y0, y1, y2 become global

f_string_x = lambda i: \
f'''x{i} = {2 ** i}; print(f"{{x{i}=}}", end=' ')'''
for i in range(3):
exec(f_string_x(i))
print('x0' in globals())
f_string_y = lambda i: \
f'''y{i} = {2 ** i}; print(f"{{y{i}=}}", end=' ')'''
[exec(f_string_y(i)) for i in range(3)]
print('y0' in globals())

📑 Answer
1) x0=1 x1=2 x2=4 True and y0=1 y1=2 y2=4 False
2)

f_string_y = lambda i: \
f'''globals()["y{i}"] = {2 ** i}; print(f"{{y{i}=}}", end=' ')'''
[exec(f_string_y(i)) for i in range(3)]
print('y0' in globals())
📑 A Series of Variables python-puzzles.blogspot.com/...tml 📑 Task 1) What will the program print for each case?
Около минуты