# Recursion in python

Can someone full explain this to me? I have a hard time to understand this yesterday def power(x, y): if y == 0: return 1 else: k = power(x,y-1) print(k) return x * k print("-->", power(2, 3))

7/8/2021 8:43:59 AM

Kakai15 Answers

New AnswerThis is a solution to something like this 2^3 = 2 x 2 x 2 For recursion the aim is to divide the problem into sub problems until the sub problems (can be solved directly ie 2^1) which in this case, the sub problem is the base (2). You need to have an exit condition which for this case is y=0 because it 2^0 which gives 1 Execution of power(2,3) First function call: to power(2,3) -> 2*power(2, 2) power(2, 2) -> 2*power(2, 1) power(2, 1) -> 2*power(2, 0) power(2,0) -> 1 Substituting for each function call in the above execution will result in this: 2*2*2*1 To keep it simple, I have left certain things about how the stack is built coz this might make it look complex than it is. Hope this helps π€π€π€ coz I know recursion isn't sth you take in at first glance.

Recursion basically similar to while loops. While y is not 0 do stuff. Power(2,3): print power(2,2): print power(2,1): print power(2,0): return 1 And what does your comment mean?

Each function function call yields a 2 * another_function_call() where another_function_call() will yield 2 * another_ function_call() and so on till you reach when y is 0 and this will return 2 x 1. This is building the solution step by step. The current function call uses the value returned from the previous function call.

Bhavik Mahalle i understand recursion when it has 1 parameter like factorial 5!. But in this example makes me hard to understand.

Kirabo Ibrahim does the fuction () returns a number and multiplied to the 2? Like... x * power(2, 3) will be 2 * power(2, 2)

This code is similar to yours, I hope it helps in understanding recursion.βΊοΈ https://code.sololearn.com/cLCXUR62zR8S/?ref=app

1. Add a <title> element to the head section and write the name of your CV inside it. 2. Write your first and last name in the body section. (Plz solve the problem)

Here's a one-liner possibility: print("-->", (pow := lambda x, y: x * (k := pow(x, y-1)) * (print(k) is None) if y else 1)(2, 3)) A simpler approach would be to use the "**" operator. # Hope this helps

This is a solution to something like this 2^3 = 2 x 2 x 2 For recursion the aim is to divide the problem into sub problems until the sub problems (can be solved directly ie 2^1) which in this case, the sub problem is the base (2). You need to have an exit condition which for this case is y=0 because it 2^0 which gives 1 Execution of power(2,3) First function call: to power(2,3) -> 2*power(2, 2) power(2, 3) -> 2*power(2, 1) power(2, 5) -> 2*power(2, 0) power(2,0) -> 1 Substituting for each function call in the above execution will result in this: 2*2*3*5 To keep it simple, I have left certain things about how the stack is built coz this might make it look complex than it is. Hope this helps because I know recursion isn't something you take in at first glance.