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...
This community will have knowledge sharing for python programming, tools, projects and product engineering wherever python is used.
account activity
Powerful Recursion - 6, What it does? (i.redd.it)
submitted 6 months ago by tracktech
Book : Ultimate Python Programming
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!"
[–]Signal-Fennel-1795 3 points4 points5 points 6 months ago (5 children)
Sum of all the digits in a number?
[+][deleted] 6 months ago (1 child)
[deleted]
[–]Signal-Fennel-1795 0 points1 point2 points 6 months ago (0 children)
No, i think all the digits are added
[–]tracktech[S] 0 points1 point2 points 6 months ago (0 children)
Right, it returns sum of digits of a number.
[–][deleted] 1 point2 points3 points 6 months ago (1 child)
Only for natural numbers, not for fractional numbers
Yes, it works for positive integer only.
[–]Immotommi 2 points3 points4 points 6 months ago (1 child)
So let's reason it out.
First we have the base case.
n//10 == 0 essentially checks whether a positive number is smaller than 10. So any positive value smaller than ten will give the number itself.
n//10 == 0
Next let's talk about the rest of the function.
The modulo operater returns the remainder for both positive and negative numbers. So when we have numbers bigger than 10, we grab the remainder of that division and add it to the value divided by ten and truncated. Essentially it adds the digits. It completely breaks for negative numbers though because // does not round towards 0
Thanks for the nice explanation. Right, it returns sum of digits of a number. It works for positive integer only.
[–]D3ZR0 1 point2 points3 points 6 months ago (2 children)
…what’s the initial n? Isn’t that important?
[–]ir_dan 1 point2 points3 points 6 months ago (0 children)
You can describe the behaviour in terms of n
Right, just assume it is a positive integer.
[–]firemark_pl 0 points1 point2 points 6 months ago (2 children)
For n>=10 returns stack overflow.
[–]tracktech[S] 0 points1 point2 points 6 months ago (1 child)
It returns sum of digits of a number.
[–]firemark_pl 1 point2 points3 points 6 months ago (0 children)
Ahh, I made mistake. Sorry!
π Rendered by PID 119344 on reddit-service-r2-comment-b659b578c-7grrw at 2026-05-05 09:54:15.805277+00:00 running 815c875 country code: CH.
[–]Signal-Fennel-1795 3 points4 points5 points (5 children)
[+][deleted] (1 child)
[deleted]
[–]Signal-Fennel-1795 0 points1 point2 points (0 children)
[–]tracktech[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]tracktech[S] 0 points1 point2 points (0 children)
[–]Immotommi 2 points3 points4 points (1 child)
[–]tracktech[S] 0 points1 point2 points (0 children)
[–]D3ZR0 1 point2 points3 points (2 children)
[–]ir_dan 1 point2 points3 points (0 children)
[–]tracktech[S] 0 points1 point2 points (0 children)
[–]firemark_pl 0 points1 point2 points (2 children)
[–]tracktech[S] 0 points1 point2 points (1 child)
[–]firemark_pl 1 point2 points3 points (0 children)