+ 2

Plz Explain the question

def f(r): j = range(1,r+1) return eval("*".join([str(i)for I in j]) print(f(4))

16th Jan 2021, 10:44 AM
คгשเภ๔ кย๓คг
คгשเภ๔ кย๓คг - avatar
2 Answers
+ 2
First, let me explain what eval is. eval means evaluate and it evaluates the string to be specific. For example: x = " 2 + 2 " print(eval(x)) >> 4 - - - - - - - - - So in your code, you called the function with argument 4. j = range(1, 5) j is an iterator from 1 to 4 "*".join means it will convert the list to string with * between each element. So, our list or iterator is like this: [1, 2, 3, 4] Then we will convert it to string with * in between. " 1 * 2 * 3 * 4 " - - - - - - - - - - - - - - Then finally, evaluate the given string using "eval" method/function. eval ( " 1 * 2 * 3 * 4 " ) 1 * 2 * 3 * 4 24 return the 24 and print the returned value Output: >> 24 And by the way, that function is to solve the factorial of a number using iterators instead of recursion.. If my explanation is still unclear, please feel free to ask. Thanks!
16th Jan 2021, 10:48 AM
noteve
noteve - avatar
+ 5
《 Nicko12 》 thank you so much brother..
16th Jan 2021, 10:50 AM
คгשเภ๔ кย๓คг
คгשเภ๔ кย๓คг - avatar