explanation on recursion function in python
up vote
0
down vote
favorite
I am a beginner in python and am not getting the concept of recursion.can i get an explanation of the code below:
def rec(k):
if(k>0):
print('the value of k is:',k)
result = k+rec(k-1)
print(result)
else:
result = 0
return result
rec(8)
the output is:
the value of k is: 8
the value of k is: 7
the value of k is: 6
the value of k is: 5
the value of k is: 4
the value of k is: 3
the value of k is: 2
the value of k is: 1
1
3
6
10
15
21
28
36
How is it that we are not getting print(result) after each print('the value of k is',k)
python recursion
add a comment |
up vote
0
down vote
favorite
I am a beginner in python and am not getting the concept of recursion.can i get an explanation of the code below:
def rec(k):
if(k>0):
print('the value of k is:',k)
result = k+rec(k-1)
print(result)
else:
result = 0
return result
rec(8)
the output is:
the value of k is: 8
the value of k is: 7
the value of k is: 6
the value of k is: 5
the value of k is: 4
the value of k is: 3
the value of k is: 2
the value of k is: 1
1
3
6
10
15
21
28
36
How is it that we are not getting print(result) after each print('the value of k is',k)
python recursion
1
You are getting the result printed afterwards, on the way back out of the stack of recursive calls.
– jonrsharpe
Nov 21 at 15:20
4
Why wouldprint(result)be the next thing that is being executed? Python is busy executingrec(k-1)first, and that function call also prints, etc.
– Martijn Pieters♦
Nov 21 at 15:26
Imagine a line of people that you give instructions: Accept a number, shout that number, then give the number minus 1 to the next person. When that next person hands you back their result, add that result to your number, shout the result of the addition, and then hand back the result to whomever had given you the first number. Do you think everyone will shout twice directly, or will they wait?
– Martijn Pieters♦
Nov 21 at 15:28
Put differently, why would Python already know what to print asresultwhen therec(k - 1)call hasn't completed yet? How would a person in your line of people know what to shout for the result without waiting for the next person to be done first, and why would they wait with shouting the first number when given?
– Martijn Pieters♦
Nov 21 at 15:29
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am a beginner in python and am not getting the concept of recursion.can i get an explanation of the code below:
def rec(k):
if(k>0):
print('the value of k is:',k)
result = k+rec(k-1)
print(result)
else:
result = 0
return result
rec(8)
the output is:
the value of k is: 8
the value of k is: 7
the value of k is: 6
the value of k is: 5
the value of k is: 4
the value of k is: 3
the value of k is: 2
the value of k is: 1
1
3
6
10
15
21
28
36
How is it that we are not getting print(result) after each print('the value of k is',k)
python recursion
I am a beginner in python and am not getting the concept of recursion.can i get an explanation of the code below:
def rec(k):
if(k>0):
print('the value of k is:',k)
result = k+rec(k-1)
print(result)
else:
result = 0
return result
rec(8)
the output is:
the value of k is: 8
the value of k is: 7
the value of k is: 6
the value of k is: 5
the value of k is: 4
the value of k is: 3
the value of k is: 2
the value of k is: 1
1
3
6
10
15
21
28
36
How is it that we are not getting print(result) after each print('the value of k is',k)
python recursion
python recursion
edited Nov 21 at 15:25
Martijn Pieters♦
693k12923992236
693k12923992236
asked Nov 21 at 15:17
hellriser
124
124
1
You are getting the result printed afterwards, on the way back out of the stack of recursive calls.
– jonrsharpe
Nov 21 at 15:20
4
Why wouldprint(result)be the next thing that is being executed? Python is busy executingrec(k-1)first, and that function call also prints, etc.
– Martijn Pieters♦
Nov 21 at 15:26
Imagine a line of people that you give instructions: Accept a number, shout that number, then give the number minus 1 to the next person. When that next person hands you back their result, add that result to your number, shout the result of the addition, and then hand back the result to whomever had given you the first number. Do you think everyone will shout twice directly, or will they wait?
– Martijn Pieters♦
Nov 21 at 15:28
Put differently, why would Python already know what to print asresultwhen therec(k - 1)call hasn't completed yet? How would a person in your line of people know what to shout for the result without waiting for the next person to be done first, and why would they wait with shouting the first number when given?
– Martijn Pieters♦
Nov 21 at 15:29
add a comment |
1
You are getting the result printed afterwards, on the way back out of the stack of recursive calls.
– jonrsharpe
Nov 21 at 15:20
4
Why wouldprint(result)be the next thing that is being executed? Python is busy executingrec(k-1)first, and that function call also prints, etc.
– Martijn Pieters♦
Nov 21 at 15:26
Imagine a line of people that you give instructions: Accept a number, shout that number, then give the number minus 1 to the next person. When that next person hands you back their result, add that result to your number, shout the result of the addition, and then hand back the result to whomever had given you the first number. Do you think everyone will shout twice directly, or will they wait?
– Martijn Pieters♦
Nov 21 at 15:28
Put differently, why would Python already know what to print asresultwhen therec(k - 1)call hasn't completed yet? How would a person in your line of people know what to shout for the result without waiting for the next person to be done first, and why would they wait with shouting the first number when given?
– Martijn Pieters♦
Nov 21 at 15:29
1
1
You are getting the result printed afterwards, on the way back out of the stack of recursive calls.
– jonrsharpe
Nov 21 at 15:20
You are getting the result printed afterwards, on the way back out of the stack of recursive calls.
– jonrsharpe
Nov 21 at 15:20
4
4
Why would
print(result) be the next thing that is being executed? Python is busy executing rec(k-1) first, and that function call also prints, etc.– Martijn Pieters♦
Nov 21 at 15:26
Why would
print(result) be the next thing that is being executed? Python is busy executing rec(k-1) first, and that function call also prints, etc.– Martijn Pieters♦
Nov 21 at 15:26
Imagine a line of people that you give instructions: Accept a number, shout that number, then give the number minus 1 to the next person. When that next person hands you back their result, add that result to your number, shout the result of the addition, and then hand back the result to whomever had given you the first number. Do you think everyone will shout twice directly, or will they wait?
– Martijn Pieters♦
Nov 21 at 15:28
Imagine a line of people that you give instructions: Accept a number, shout that number, then give the number minus 1 to the next person. When that next person hands you back their result, add that result to your number, shout the result of the addition, and then hand back the result to whomever had given you the first number. Do you think everyone will shout twice directly, or will they wait?
– Martijn Pieters♦
Nov 21 at 15:28
Put differently, why would Python already know what to print as
result when the rec(k - 1) call hasn't completed yet? How would a person in your line of people know what to shout for the result without waiting for the next person to be done first, and why would they wait with shouting the first number when given?– Martijn Pieters♦
Nov 21 at 15:29
Put differently, why would Python already know what to print as
result when the rec(k - 1) call hasn't completed yet? How would a person in your line of people know what to shout for the result without waiting for the next person to be done first, and why would they wait with shouting the first number when given?– Martijn Pieters♦
Nov 21 at 15:29
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415161%2fexplanation-on-recursion-function-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You are getting the result printed afterwards, on the way back out of the stack of recursive calls.
– jonrsharpe
Nov 21 at 15:20
4
Why would
print(result)be the next thing that is being executed? Python is busy executingrec(k-1)first, and that function call also prints, etc.– Martijn Pieters♦
Nov 21 at 15:26
Imagine a line of people that you give instructions: Accept a number, shout that number, then give the number minus 1 to the next person. When that next person hands you back their result, add that result to your number, shout the result of the addition, and then hand back the result to whomever had given you the first number. Do you think everyone will shout twice directly, or will they wait?
– Martijn Pieters♦
Nov 21 at 15:28
Put differently, why would Python already know what to print as
resultwhen therec(k - 1)call hasn't completed yet? How would a person in your line of people know what to shout for the result without waiting for the next person to be done first, and why would they wait with shouting the first number when given?– Martijn Pieters♦
Nov 21 at 15:29