+ 3
What is the output of this code? And How?
def f(r): j = range(r) e = eval("*".join([str(i+2) for i in j])) return e print(f(3))
8 Respuestas
+ 1
so if I am getting this corrextly
j = 0,1,2 (because range is 3)
and it does each of those + 2
so 2,3,4
and because the string is joined with * (which after eval is just a multiplier)
it becomes 2*3*4 = 24 :)
+ 2
When I run this code, it provide the results 24. But I don't understand why? I need to understand clearly and I need the clue. So that I asked.. Thanks
+ 2
interesting eval() parses a string as an expression, so everything that is a string inside the eval() will become an expression. so “*” is actually just a multiplier here :)
https://www.programiz.com/JUMP_LINK__&&__python__&&__JUMP_LINK-programming/methods/built-in/eval
+ 2
Yes I am already in programiz.com and learning eval(), how its works. But If you guys do have any ideas about this method you can share.
+ 2
Brave Tea By the way Thanks for sharing.
+ 2
welcome :) I liked your post as well. made me learn something as well :)
+ 1
24 :) but no clue why. :) will look into it later but will probably be answered by then :)