Step-by-Step Explanation:
Function Definition:
def square(n):
return n ** 2
A function named square is defined.
It takes one parameter, n.
The function returns the square of n (n ** 2).
Using map:
result = map(square, [1, 2, 3])
The map() function applies a specified function (square in this case) to each item in an iterable ([1, 2, 3]).
The result of map() is an iterator, which lazily applies the square function to each element in the list.
Execution of map(square, [1, 2, 3]):
Resulting iterator contains the values: 1, 4, 9.
Converting the Iterator to a List:
print(list(result))
The list() function is used to convert the iterator returned by map() into a list.
The resulting list: [1, 4, 9].
The print() function then outputs this list.
Final Output:
[1, 4, 9]
0 Comments:
Post a Comment