+ 1

What is "val" and "abs" in the code, why is the output "3"

a = [x - 2 for x in range (-1, 5)] val = lambda x : x + 3 b = [val (x) for x in a] print(abs(max(a) - max(b)))

23rd Jan 2020, 1:32 PM
Shamil Erkenov
Shamil Erkenov - avatar
3 Respuestas
+ 1
val is a lambda(anonymous function). Learn more here. https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2460/ abs returns absolute value of a number. So after running the program, a = [-3, -2, -1, 0, 1] b = [0, 1, 2, 3, 4], i.e. 3 added to each element of a. So now, max(a) = 1 & max(b) = 4 1 - 4 = -3 And the absolute value of -3 is 3.
23rd Jan 2020, 1:45 PM
XXX
XXX - avatar
+ 2
Thank you very much 😊
23rd Jan 2020, 2:05 PM
Shamil Erkenov
Shamil Erkenov - avatar