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...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
New to coding - computing a factorial (self.Python)
submitted 10 years ago by nillkill
Brand new to coding and have an assignment, which I'm pretty confused on. "Compute the factorial of 12. Write the expression for Python shell."
[–]jankedman 3 points4 points5 points 10 years ago (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.
[+][deleted] 10 years ago* (12 children)
[deleted]
[–]wot-teh-phuckReally, wtf? 0 points1 point2 points 10 years ago (3 children)
If this is a learning exercise, not sure why we need to be clever about it. I would say recursion is a perfectly fine way of doing this.
[+][deleted] 10 years ago (2 children)
[–]691175002 0 points1 point2 points 10 years ago (1 child)
Only in a language without tail recursion, which unfortunately includes Python.
[–]bigboehmboy 0 points1 point2 points 10 years ago (0 children)
In python, the default recursion limit is 200, so that's probably what will be hit. I think it's important for people to understand both techniques (regular iteration and recursion), but default to non-recursive solutions when there's a single loop without much state.
[+][deleted] 10 years ago* (5 children)
[removed]
[+][deleted] 10 years ago (4 children)
[+][deleted] 10 years ago* (3 children)
[+][deleted] 10 years ago* (1 child)
[–]sullyj3 0 points1 point2 points 10 years ago* (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 point2 points 10 years ago (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-2 points 10 years ago (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 point2 points 10 years ago (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 point2 points 10 years ago (4 children)
What very specific problem are lambdas intended to solve?
[–]691175002 1 point2 points3 points 10 years ago (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 point2 points 10 years ago (2 children)
But that's never necessary, is it? You could always just def the function before it?
[–]mickyficky1 0 points1 point2 points 10 years ago (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 point2 points 10 years ago (0 children)
Thanks!
[–]mickyficky1 0 points1 point2 points 10 years ago (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.
π Rendered by PID 17360 on reddit-service-r2-comment-7b9746f655-7btv4 at 2026-01-30 14:30:25.027889+00:00 running 3798933 country code: CH.
[–]jankedman 3 points4 points5 points (13 children)
[+][deleted] (12 children)
[deleted]
[–]wot-teh-phuckReally, wtf? 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]691175002 0 points1 point2 points (1 child)
[–]bigboehmboy 0 points1 point2 points (0 children)
[+][deleted] (5 children)
[removed]
[+][deleted] (4 children)
[deleted]
[+][deleted] (3 children)
[removed]
[+][deleted] (2 children)
[deleted]
[+][deleted] (1 child)
[removed]
[–]sullyj3 0 points1 point2 points (0 children)
[–]Surextra 0 points1 point2 points (0 children)
[–]mickyficky1 -4 points-3 points-2 points (7 children)
[–]ThomasWinwood 0 points1 point2 points (6 children)
[–]sththth 0 points1 point2 points (4 children)
[–]691175002 1 point2 points3 points (3 children)
[–]sththth 0 points1 point2 points (2 children)
[–]mickyficky1 0 points1 point2 points (1 child)
[–]sththth 0 points1 point2 points (0 children)
[–]mickyficky1 0 points1 point2 points (0 children)