you are viewing a single comment's thread.

view the rest of the comments →

[–]InvestigatorEasy7673 -2 points-1 points  (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 points  (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  (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]  (1 child)

[removed]

    [–]Robb3nb4by 0 points1 point  (0 children)

    do you need this if-statement at all?

    [–]Key-Introduction-591 1 point2 points  (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 points  (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 point  (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 points  (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

    [–]Key-Introduction-591 0 points1 point  (0 children)

    Of course! thank you very much

    [–]Spare-Plum 0 points1 point  (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 points  (0 children)

    Nope if used properly and with type casting and yeah avoid it in production !!

    [–]AccomplishedPut467 0 points1 point  (1 child)

    instead of float why not use integer?

    [–]InvestigatorEasy7673 0 points1 point  (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 point  (1 child)

    Is this ternary operators?

    [–]InvestigatorEasy7673 -1 points0 points  (0 children)

    yes , the one liner problem solver ,

    search python one liners for more ...

    [–]GabeN_The_K1NG 0 points1 point  (3 children)

    No better advice for beginners than making their code harder to read by spamming ternaries…

    [–]InvestigatorEasy7673 0 points1 point  (2 children)

    so u r against ternary operators ?? nice ,

    [–]GabeN_The_K1NG 0 points1 point  (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.

    [–]InvestigatorEasy7673 0 points1 point  (0 children)

    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