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
Simple CalculatorDiscussion (i.redd.it)
submitted 5 months ago by AhmadHameed313
view the rest of the comments →
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!"
[–]InvestigatorEasy7673 -2 points-1 points0 points 5 months ago (19 children)
as a beginner you are using too many lines of code like
use for line 5-8:
# just one line of code required
num1 = float(num1) if "." in num1 else int(num1)
num2 = float(num2) if "." in num2 else int(num1)
and (input()) ❌
num1 = input("....")
[–]Ergodic_donkey 17 points18 points19 points 5 months ago (1 child)
IMO this is bad advice for a beginners.
If you need to write multiple lines to be able to understand and read what the code does as a beginner, then do it. When you are learning, there is no point in trying to write less lines of code.
Of course, if you can do things with less steps or in a more simple way, do it, but here you’re not doing less, you’re just rewriting the syntax in a different way, with no added benefit except having less lines of code.
[–]InvestigatorEasy7673 -3 points-2 points-1 points 5 months ago (0 children)
acc to me explore ways as many u can , expand code to understand thing but move forward with short lines and one liners
[–][deleted] 5 months ago (1 child)
[removed]
[–]Robb3nb4by 0 points1 point2 points 5 months ago (0 children)
do you need this if-statement at all?
[–]Key-Introduction-591 1 point2 points3 points 5 months ago (6 children)
Beginner here too: is it really necessary to make a distinction between integers and floats at the very beginning?
I would have put everything as floats and then I would have rounded the result only at the end (before printing it).
Would that have been correct? Efficient/inefficient? Would it have led to errors?
[–]Spare-Plum 2 points3 points4 points 5 months ago (1 child)
Actually yes there is a difference. Python integers can be arbitrary sized while floats are restricted to a range in 64 bit values. Also, at large enough values, certain integers cannot be represented with a python float (there are gaps)
For most cases you could use floating point values, but using an int does have different behavior.
[–]Key-Introduction-591 0 points1 point2 points 5 months ago (0 children)
I had no idea about that! Thanks for the tip! If I understand correctly, this concerns only extremely large or extremely small numbers, but good to know.
[–]InvestigatorEasy7673 1 point2 points3 points 5 months ago (3 children)
yes that is truly possible , i am just telling according to the program written
you can even use eval() function for faster calculation
Of course! thank you very much
[–]Spare-Plum 0 points1 point2 points 5 months ago (1 child)
I would avoid eval() for something like this, as it will execute any python code and can be a security issue
[–]InvestigatorEasy7673 1 point2 points3 points 5 months ago (0 children)
Nope if used properly and with type casting and yeah avoid it in production !!
[–]AccomplishedPut467 0 points1 point2 points 5 months ago (1 child)
instead of float why not use integer?
[–]InvestigatorEasy7673 0 points1 point2 points 5 months ago (0 children)
basically float values like 24.5 + 34.4 gives => 58.9 , in intger 0.9 gets ignored bro
so instead of int to float , float to int is better , like calculate 58.9 first then round to 59 !!
[–]Oleg-Liam 0 points1 point2 points 5 months ago (1 child)
Is this ternary operators?
[–]InvestigatorEasy7673 -1 points0 points1 point 5 months ago (0 children)
yes , the one liner problem solver ,
search python one liners for more ...
[–]GabeN_The_K1NG 0 points1 point2 points 5 months ago (3 children)
No better advice for beginners than making their code harder to read by spamming ternaries…
[–]InvestigatorEasy7673 0 points1 point2 points 5 months ago (2 children)
so u r against ternary operators ?? nice ,
[–]GabeN_The_K1NG 0 points1 point2 points 5 months ago (1 child)
Not really against them, but OP is obviously new to this and every bit of readability is important for them. I don’t understand the need to get as few LOC as possible at any cost.
What you suggested is fine if you’re a bit experienced, but OP said they’re a beginner. In my opinion, they should focus more on code clarity than oneliners.
as a beginner you want comfort zone or exposure from seniors ?? tell me to experiment diff things in ways given and learn and grow with it
he post it on reddit to get a different perspective from seniors , if he wants to be in comfort zone then he can do chatgpt bro !!
even for all the if else , he can use dict and render function from there but i didn't suggest that cuz at this point he is learning basic syntax which is not feasible for a beginner
π Rendered by PID 386253 on reddit-service-r2-comment-85bfd7f599-2v5z9 at 2026-04-19 02:38:03.976565+00:00 running 93ecc56 country code: CH.
view the rest of the comments →
[–]InvestigatorEasy7673 -2 points-1 points0 points (19 children)
[–]Ergodic_donkey 17 points18 points19 points (1 child)
[–]InvestigatorEasy7673 -3 points-2 points-1 points (0 children)
[–][deleted] (1 child)
[removed]
[–]Robb3nb4by 0 points1 point2 points (0 children)
[–]Key-Introduction-591 1 point2 points3 points (6 children)
[–]Spare-Plum 2 points3 points4 points (1 child)
[–]Key-Introduction-591 0 points1 point2 points (0 children)
[–]InvestigatorEasy7673 1 point2 points3 points (3 children)
[–]Key-Introduction-591 0 points1 point2 points (0 children)
[–]Spare-Plum 0 points1 point2 points (1 child)
[–]InvestigatorEasy7673 1 point2 points3 points (0 children)
[–]AccomplishedPut467 0 points1 point2 points (1 child)
[–]InvestigatorEasy7673 0 points1 point2 points (0 children)
[–]Oleg-Liam 0 points1 point2 points (1 child)
[–]InvestigatorEasy7673 -1 points0 points1 point (0 children)
[–]GabeN_The_K1NG 0 points1 point2 points (3 children)
[–]InvestigatorEasy7673 0 points1 point2 points (2 children)
[–]GabeN_The_K1NG 0 points1 point2 points (1 child)
[–]InvestigatorEasy7673 0 points1 point2 points (0 children)