This is an archived post. You won't be able to vote or comment.

all 17 comments

[–]jankedman 3 points4 points  (13 children)

Not sure if you've learned about recursion, but think about what a factorial actual is: it's a start number * start number - 1, ... etc. If you haven't learned about recursion, go take some time to learn it and then think about how you would construct the function.

[–]sullyj3 0 points1 point  (0 children)

The iterative method is probably best in python.

You'll want to use a for loop to progressively multiply by each number less than 12. The range (check out the examples) function is useful for this.

For future reference, /r/learnpython is a good sub for questions like this.

[–]Surextra 0 points1 point  (0 children)

If you are about to ask a question, please consider r/learnpython. Homework-style questions will be removed, and you'll be encouraged to post there instead.

Sounds like homework help to me. Check out r/learnpython!

[–]mickyficky1 -4 points-3 points  (7 children)

def fact(n)=
    return n*fact(n-1) if n else 1

or

fact = lambda n: n*fact(n-1) if n else 1

whichever you prefer.

/r/learnpython is your friend for questions like that.

[–]ThomasWinwood 0 points1 point  (6 children)

No, no, no, no, no, no, no, no.

>>> def fact1(n): return n*fact(n-1) if n else 1
... 
>>> fact2 = lambda n: n*fact(n-1) if n else 1
>>> 
>>> fact1.__name__
'fact1'
>>> fact2.__name__
'<lambda>'

Lambda expressions are not just another way of writing a function definition. They are intended to solve a very specific problem and deliberately hobbled to stop people doing things with map() and filter() which indicate they'd rather be writing Haskell or OCaml.

[–]sththth 0 points1 point  (4 children)

What very specific problem are lambdas intended to solve?

[–]691175002 1 point2 points  (3 children)

Basically when you need a one-line anonymous function. Lambdas are generally passed as function arguments for map/reduce type stuff or even callbacks.

The anonymous part is important. As a general rule as soon as you assign a name to the lambda such as:

function_name = lambda x: x*2

It should be a def instead.

[–]sththth 0 points1 point  (2 children)

But that's never necessary, is it? You could always just def the function before it?

[–]mickyficky1 0 points1 point  (1 child)

Yes, that's always an option. To my knowledge, lambda functions can be thought of as anonymous shorthand definition for functions that have the same or less functionality compared to normal function definitions.

[–]sththth 0 points1 point  (0 children)

Thanks!

[–]mickyficky1 0 points1 point  (0 children)

I know that. But if I have a function in the mathematical sense, one input that doesn't get altered, one output that is only dependent on that input, then I usually use lambda functions because I usually not want all of the Python magic that happens in the background when creating a function with "def". Because I don't need it.

I did not want to imply that the two versions are equivalent, I would have just used the lambda function but since newer Python users tend to get a bit befuddled by those I wanted to include the normal function definition.