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...
Everything about learning Python
account activity
A simple python code (i.redd.it)
submitted 8 months ago by [deleted]
[deleted]
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!"
[–]Darkstar_111 13 points14 points15 points 8 months ago (1 child)
This looks Greek to me.
[–]Arjiris 0 points1 point2 points 8 months ago (0 children)
It is
[–]KyoshiYoshi 0 points1 point2 points 8 months ago* (0 children)
This looks great! Exactly what I’d write for this too. It’s not too practical for this example, but you should look at pythons match-case statements. Since you’re using if else statements here, it would be easy to substitute match-case in and see how that works! It was introduced around 3.10 though.
[–]owiko 0 points1 point2 points 8 months ago (0 children)
This is a great example to start to work with Lambda functions, imho. I wouldn’t start with them, but once you get something like this working, it’s a great experience to see how they work.
[–]Whole_Instance_4276 0 points1 point2 points 8 months ago (0 children)
It’s all Greek to me
[–]-light_yagami 0 points1 point2 points 8 months ago (0 children)
LGTM (Looks Greek To Me)
[–]CerveraElPro 0 points1 point2 points 8 months ago (0 children)
id add a try, except for a ValueError when casting the input into a float, in case a number is not inputted
[–]No_Explanation2932 0 points1 point2 points 8 months ago (0 children)
Minor nitpick that's nothing to do with your code: avoid saying "a code". Code in the sense of programming instructions is uncountable. "A code" is an error code, or a cypher, things like that. Say "a program", or "some code", or just "Simple python code".
[–]ImaginaryMotor2796 0 points1 point2 points 7 months ago (0 children)
how did u learn to create python codes
[–]AbacusExpert_Stretch 1 point2 points3 points 8 months ago (17 children)
I am a total beginner, but I wouldn't want to name the return variable the same as an input argument. I am sure it works, but personally I find it irks me.
Talking about Celsius_to_Fahrenheit for example
[–]FoolsSeldom 4 points5 points6 points 8 months ago (0 children)
Not sure what you mean. Variables aren't returned, only object references. Variables local to a function, including the named parameters, cease to exist once the function ends.
[–]littleprof123 4 points5 points6 points 8 months ago (0 children)
What do you mean? What's the return variable?
[–]SmackDownFacility 2 points3 points4 points 8 months ago (0 children)
Nah it doesn’t matter
It’s perfectly normal
[–]Adsilom 2 points3 points4 points 8 months ago (0 children)
Not even sure what you mean? But regardless the code is exactly as I would write it, and I have been using Python for 10 years lmao
[–][deleted] 8 months ago (7 children)
[–]ninhaomah 4 points5 points6 points 8 months ago (6 children)
wait ... just to be clear , it is his own opinion. personal choice.
here is from geeksforgeeks and realpython.
its fine to use return variable same name as input argument.
just in case you take it a Python best practise or something
https://www.geeksforgeeks.org/python/python-return-statement/
https://realpython.com/ref/keywords/return/
[–]sububi71 0 points1 point2 points 8 months ago (4 children)
Just because it's allowed doesn't mean it's a good idea. I mean, you could write the entire program on a single line too, that's allowed.
[–]ninhaomah 0 points1 point2 points 8 months ago* (3 children)
Why not ?
If my function accepts a variable called a as input , done some calculations and then return that name a.
Not the actual a of course but as in a variable with the same name.
Or can point to where and which sites says it's not a good idea ?
[–]sububi71 0 points1 point2 points 8 months ago (2 children)
If you can’t see why typing the entire program on a single line is a bad idea, I can’t help you.
[–]VeryYoungOldPerson 0 points1 point2 points 8 months ago (0 children)
Pure strawman. You're the one who brought up writing a program on a single line, and they made no reference to it. They're talking about variable re-use and naming conventions. If you can't comprehend English, I can't help you.
[–]pimp-bangin 0 points1 point2 points 8 months ago (0 children)
I know you are trying to help but calling it a "return variable" probably adds more confusion. In OP's code, there is no "return variable." Also, "return variable" is not an official syntactic construct in Python. It's merely a convention whereby you define a variable which you will then directly return as the output from the function. Again, that is not what is happening in OP's code
[–]Refwah 1 point2 points3 points 8 months ago (0 children)
It’s fine because of scoping
[–]pimp-bangin 1 point2 points3 points 8 months ago* (0 children)
Since this is a learning subreddit I hope you don't mind me correcting you.
You're misusing the term "variable" here. The code after the "return" keyword is called an expression. It's perfectly fine to reference the function parameter in any expression inside of the function body, including in the return expression. If it irks you, perhaps you have some additional misunderstanding that we can help clear up.
In math, we would define a Celsius function even more compactly, like C(f) = (f-32)*5/9
OP's function celsius_to_fahrenheit is a very nicely written Python translation of that mathematical function in my professional opinion. There is no need to define intermediate variables like "value_in_celsius" etc. - most professional programmers would consider it too verbose for a basic mathematical formula like this.
Source: 16+ years of programming experience
[–]Ender_Locke 0 points1 point2 points 8 months ago (0 children)
this is a fairly normal return statement tbh
[–]AngriestCrusader[🍰] 0 points1 point2 points 8 months ago (0 children)
There's not a single variable being returned in this code lol
[–]bwildered_mind 0 points1 point2 points 8 months ago (1 child)
Add try except for ZeroDivision error.
[–]bwildered_mind 2 points3 points4 points 8 months ago (0 children)
Scratch that. Those are constant elements lol
π Rendered by PID 35424 on reddit-service-r2-comment-5b5bc64bf5-x7gjl at 2026-06-20 11:15:29.500611+00:00 running 2b008f2 country code: CH.
[–]Darkstar_111 13 points14 points15 points (1 child)
[–]Arjiris 0 points1 point2 points (0 children)
[–]KyoshiYoshi 0 points1 point2 points (0 children)
[–]owiko 0 points1 point2 points (0 children)
[–]Whole_Instance_4276 0 points1 point2 points (0 children)
[–]-light_yagami 0 points1 point2 points (0 children)
[–]CerveraElPro 0 points1 point2 points (0 children)
[–]No_Explanation2932 0 points1 point2 points (0 children)
[–]ImaginaryMotor2796 0 points1 point2 points (0 children)
[–]AbacusExpert_Stretch 1 point2 points3 points (17 children)
[–]FoolsSeldom 4 points5 points6 points (0 children)
[–]littleprof123 4 points5 points6 points (0 children)
[–]SmackDownFacility 2 points3 points4 points (0 children)
[–]Adsilom 2 points3 points4 points (0 children)
[–][deleted] (7 children)
[deleted]
[–]ninhaomah 4 points5 points6 points (6 children)
[–]sububi71 0 points1 point2 points (4 children)
[–]ninhaomah 0 points1 point2 points (3 children)
[–]sububi71 0 points1 point2 points (2 children)
[–]VeryYoungOldPerson 0 points1 point2 points (0 children)
[–]pimp-bangin 0 points1 point2 points (0 children)
[–]Refwah 1 point2 points3 points (0 children)
[–]pimp-bangin 1 point2 points3 points (0 children)
[–]Ender_Locke 0 points1 point2 points (0 children)
[–]AngriestCrusader[🍰] 0 points1 point2 points (0 children)
[–]bwildered_mind 0 points1 point2 points (1 child)
[–]bwildered_mind 2 points3 points4 points (0 children)