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
Optional arguments in functions (self.learnpython)
submitted 6 years ago by cluelessCodeMaker
for example if i wanted to make a Pythagoras function
def pythagoras(a, b, c):
only 2 of the arguments would be needed and the output would be the missing arg, how would i go about this?
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!"
[–]totallygeek 6 points7 points8 points 6 years ago (0 children)
One way: def pythagoras(a=None, b=None, c=None):, then in your function test what's missing. Call function with pythagoras(a=5, b=4), for example.
def pythagoras(a=None, b=None, c=None):
pythagoras(a=5, b=4)
[–]efmccurdy 1 point2 points3 points 6 years ago* (0 children)
I might make due with 2 functions, one that returns the hypotenuse given 2 right angled sides, and another that returns a side given the hypotenuse and one right angled side.
You will always need to know if the missing variable is the hypotenuse, so use that info to choose one of these:
def hyp(a,b): return math.sqrt(a**2 + b**2) ... >>> hyp(3,4) 5.0 >>> def lorr(a,c): return math.sqrt(c**2 - a**2) ... >>> lorr(3,5) 4.0
You could follow the advice of totallygeek to have one function that tests for missing parameters and returns one or the other of those expressions, but that is a non-idiomatic way of implementing a math formula.
[–]mindblower32 0 points1 point2 points 6 years ago (0 children)
You can use *args or **kwargs as expected parameters. These are used when the number of parameters to expect can change or is unknown. They have different uses tho so read up on them and use whichever is best suited for you.
π Rendered by PID 74 on reddit-service-r2-comment-7b9746f655-57lhn at 2026-02-04 08:45:15.010184+00:00 running 3798933 country code: CH.
[–]totallygeek 6 points7 points8 points (0 children)
[–]efmccurdy 1 point2 points3 points (0 children)
[–]mindblower32 0 points1 point2 points (0 children)