The code is badly written in my opinion, probably by a beginner who thinks they're smart (or it might be way to speed up the code, but in that case it should be heavily commented). So if you don't get it, it's not your fault, but the programmer's.
But let's analyse it:
n and x or 1
will return:
- 1 if n is 0, as
0 and x
is always 0 no matter what x is, and due to shortcut evaluation, x (i.e., n*fax(n-1)) is never evaluated; and 0 or 1
is 1.
- x if n is non-zero, as in this case x cannot be 0.
So the n and x or 1
handles the base case of the recursion, stopping it when n reaches 0. This is also why you get a "maximum recursion depth exceeded" error if you remove the "n and".
The rest is just the definition of factorial, which I guess I don't need to explain here.
Here's a more sane and readable version:
fac = lambda n: (n * fac(n-1)) if n!=0 else 1
And here's how I would prefer to write it, to make it readable and end with the recursive call, as some compilers may be able to optimize the recursion in that situation:
def fac(n):
if n==0:
return 1
else:
return n*fac(n-1)
None of these handles the undefined case when n is negative very well (it results in recursion overflow), so you just have to avoid that situation if you use the function.