What is the output of this code? And How? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 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))

14th Aug 2019, 12:56 PM
Mahmudul Hasan
Mahmudul Hasan - avatar
8 ответов
+ 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 :)
14th Aug 2019, 1:13 PM
Brave Tea
Brave Tea - avatar
+ 3
I guess so thanks guys. That's right Brave Tea ~ swim ~
14th Aug 2019, 1:15 PM
Mahmudul Hasan
Mahmudul Hasan - avatar
+ 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
14th Aug 2019, 1:02 PM
Mahmudul Hasan
Mahmudul Hasan - avatar
+ 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
14th Aug 2019, 1:02 PM
Brave Tea
Brave Tea - avatar
+ 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.
14th Aug 2019, 1:04 PM
Mahmudul Hasan
Mahmudul Hasan - avatar
+ 2
Brave Tea By the way Thanks for sharing.
14th Aug 2019, 1:09 PM
Mahmudul Hasan
Mahmudul Hasan - avatar
+ 2
welcome :) I liked your post as well. made me learn something as well :)
14th Aug 2019, 1:23 PM
Brave Tea
Brave Tea - avatar
+ 1
24 :) but no clue why. :) will look into it later but will probably be answered by then :)
14th Aug 2019, 12:59 PM
Brave Tea
Brave Tea - avatar