+ 2
Help me to solve the code #0002
https://code.sololearn.com/cQMk0u8N6x7I/?ref=app this code gives output 222. i don't understand how..?
3 Answers
+ 2
i don't understand yet how..?
can you explain in detail..?
+ 1
return[lambda x : i*x for i in range(3)]
- This line returns a list containing the argument multiplied by 1, by 2, and by 3, as indicated by the range. For example, using 1 as an argument, this would return [1, 2, 3].
for m in cm() :
- Now, the for loop indicates that this will run 3 times, as there are 3 elements returned in the list by the cm() function. m stands for the index count in the list.
print(m(1),end='')
- Since this print statement only says to print the second element in list m (zero-based indexing), this would return the number multiplied by 2. Since the for loop runs for 3 times, as I've stated, this would print the same element in the list for 3 times. The "end=''" just joins those characters together.
In short, using 1 as the argument to cm() for example, the function would return [1, 2, 3]. The for loop is executed for 3 times, and the print statement only outputs the second element, which is 2 in this list. These characters are joined together and that is what is the result. 222.
+ 1
The lambda function simply returns a list containing the argument multiplied by 2 for 3 times. The for loop iterates through that list and prints each element next to each other. Hence, cm(1) returns 222, cm(2) returns 444, and so on.