How is function (square,cube) being stored in operations variable. How function is being called here? How this code actually executes ?
def square(n):
return n**2
def cube(n):
return n**3
operations = [square, cube]
numbers = [2, 1, 3, 4, 7, 11, 18, 29]
for i, n in enumerate(numbers):
action = operations[i % 2]
print(f"{action.__name__}({n}):", action(n))
Output:
square(2): 4
cube(1): 1
square(3): 9
cube(4): 64
square(7): 49
cube(11): 1331
square(18): 324
cube(29): 24389