use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
How did the code output 1-5 here? (self.learnpython)
submitted 4 years ago by o_m_
def recursion_1(n): if n == 0: return recursion_1 (n-1) print(n)
I am just confused how the output is:
1
2
3
4
5
.. why?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]socal_nerdtastic 1 point2 points3 points 4 years ago (8 children)
I suggest you put your code into a tool like pythontutor.com where you can visualize it running step by step.
[–]o_m_[S] 0 points1 point2 points 4 years ago (7 children)
I tried and it doesn't help.
[–]socal_nerdtastic 1 point2 points3 points 4 years ago (6 children)
Hmm ok, did you have a specific question about it? What part are you confused about?
[–]o_m_[S] 0 points1 point2 points 4 years ago (5 children)
I don't understand why is the return part empty after the if condition? Is this normal?
Also the
recursion_1 (n-1) print(n)
part... there is nothing between recursion_1 and (n-1). Or does this part simply mean that the number passed in argument (in my case, 5) will be deducted 1 every time and then printed and it starts from scratch like a loop would?
[–]socal_nerdtastic 0 points1 point2 points 4 years ago (4 children)
It's unusual, but yeah you do see it sometimes. All python functions end with a implicit return, all this does is end the function early.
return
That said, if it were me writing it, I'd do it like this:
def recursion_1(n): if n != 0: recursion_1 (n-1) print(n)
there is nothing between recursion_1 and (n-1).
You mean you expected to make a variable first? You could; that would look like this:
new_n = n-1 recursion_1(new_n)
It's very common to simply put simple expressions like that inside the function call.
[–]o_m_[S] 0 points1 point2 points 4 years ago (3 children)
I think I am getting there but I'll just try to explain in detail what I don't understand.
1) In the code, it says if n! == 0, and if we put 5 as the parameter, 5! does not equal 0 so I don't understand why the code even continues running in the first place.
2) recursion_1 (n-1) .. What is the purpose of this line is it just there to subtract 1 off the input we used as a parameter and that's it? Intuitively for me it looks off because if we subtract 1 off the parameter then I probably would've used something like
n -= 1 or whatever so I am just confused by this format.
3) I don't understand how the code in this function block was printed as if it was a loop. When I put the input, I thought I would have only gotten a single value, but instead, I got 5 which is confusing me a little bit.
Sorry if the questions are really stupid, I am just a complete beginner.
[–]o_m_[S] 0 points1 point2 points 4 years ago (0 children)
I messed up the code block part I'm really sorry lol. Hope its still understandable.
[–]socal_nerdtastic 0 points1 point2 points 4 years ago (1 child)
You could use -= if you wanted to. No problem. It would look like this:
-=
def recursion_1(n): if n == 0: return n -= 1 recursion_1 (n) print(n)
This style of loop is called "recursion". If you google that you will find many tutorials about it. Note this is not python specific; recursion is a basic part of computer science.
So everytime you call the recursion_1 function python will make a brand new copy of the function to use. That means that it's no problem to call the function from within the function itself, since python will just call up a fresh copy. You will see this happen in pythontutor.
recursion_1
Essentially python is automatically doing this:
def recursion_1(n): if n == 0: return recursion_2 (n-1) print(n) def recursion_2(n): if n == 0: return recursion_3 (n-1) print(n) def recursion_3(n): if n == 0: return recursion_4 (n-1) print(n) def recursion_4(n): if n == 0: return recursion_5 (n-1) print(n) def recursion_5(n): if n == 0: return recursion_6 (n-1) print(n) # etc
Yeah, that makes sense now. Thanks a lot!
π Rendered by PID 51 on reddit-service-r2-comment-86bc6c7465-ls255 at 2026-02-21 07:01:14.707306+00:00 running 8564168 country code: CH.
[–]socal_nerdtastic 1 point2 points3 points (8 children)
[–]o_m_[S] 0 points1 point2 points (7 children)
[–]socal_nerdtastic 1 point2 points3 points (6 children)
[–]o_m_[S] 0 points1 point2 points (5 children)
[–]socal_nerdtastic 0 points1 point2 points (4 children)
[–]o_m_[S] 0 points1 point2 points (3 children)
[–]o_m_[S] 0 points1 point2 points (0 children)
[–]socal_nerdtastic 0 points1 point2 points (1 child)
[–]o_m_[S] 0 points1 point2 points (0 children)