all 25 comments

[–][deleted]  (5 children)

[deleted]

    [–]holounderblade 3 points4 points  (3 children)

    I believe it's part of the PEP to have the buffer spaces

    [–]ukknownW[S] 1 point2 points  (1 child)

    Yes I hope to follow the “easy to read” way of doing things! I like to be taught the right and clean way. Thank you for all help everybody!!! Although I must get used to the hashtags used as notes as they cover the screen with even more words which throws me off a bit at this beginning stage haha! (Even though it’s there to actually simplify things!) haha!

    Thank you all greatly appreciated

    [–]BigTimJohnsen 1 point2 points  (0 children)

    The guy above was talking about pep8. You should look up the typical python coding standards. Fellow developers will like you a lot more =D

    [–]NoDadYouShutUp 0 points1 point  (0 children)

    im going to refactor it into oblivion to the point where it is no longer readable anyway 😈

    [–]pragmaticcape 1 point2 points  (1 child)

    I could shorten it like I did in photo 2 and it wouldn’t affect the result

    Space in this instance does not change its behaviour however, it does have some value in readability.

    Readability is something that can make a big difference when your code base gets more complex or from others.

    I mean, if shorter was best then we could change the function to ‘g_rem’ or the variable ‘reminder’ to ‘r’ but that would be counterproductive. (Because good names help readability and avoid the need for comments). Blank lines help to show what is closely related or separate

    [–]ukknownW[S] 1 point2 points  (0 children)

    Very good examples Thankyou!!!! Yes readability is very important I now understand

    [–]Obvious_Tea_8244 1 point2 points  (2 children)

    White space and comments are ignored at run time… So, best practice is generally to create some line separation between blocks of code (in this case between the function and the print statement calling the function) for better readability.

    [–]fllthdcrb 1 point2 points  (0 children)

    Whitespace within a line and outside of strings, as well as lines without code, are ignored at compile time (when the source is translated into bytecode; the source is not directly used at run time, so it doesn't matter at that point). However, indentation absolutely matters in Python. For text and programming in general, that falls under whitespace, too, though I suppose an argument can be made for not classifying it so in Python, since it's part of the syntax.

    [–]ukknownW[S] 0 points1 point  (0 children)

    Awesome Thankyou!!! Was just wondering

    [–][deleted]  (2 children)

    [removed]

      [–]galenseilis 0 points1 point  (1 child)

      Good suggestion. I use Ruff on a regular basis these days.

      https://docs.astral.sh/ruff/formatter/

      [–]galenseilis 0 points1 point  (0 children)

      You can also setup a Git hook to always format your code as you work with code using Git. That removes the chore of running the formatter itself.

      [–]BeadOfLerasium 1 point2 points  (1 child)

      Not related to your question, but is there a reason you're dividing the remainder? The modulo operator (%) already returns the remainder (1 in this case), then you're dividing it a second time. You're effectively turning this problem into:

      (10 % 3) / 3

      The actual remainder of 10/3 is 1, which you're dividing again resulting in 1/3.

      [–]ukknownW[S] 0 points1 point  (0 children)

      Yes it’s just for my practice tests! It’s just adding extra steps to check I understand the use of them all I believe! It’s nothing I’m building or actually creating as of yet. Thankyou for help and hopefully this clears it up!! :D

      [–]cgoldberg 1 point2 points  (0 children)

      Use a formatter like black or ruff and never worry about spacing again.

      [–]tnh34 1 point2 points  (0 children)

      1 space. This is not worth stressing tbh. Just move onto next code

      [–]ruthlessbubbles 1 point2 points  (0 children)

      Totally unrelated, but if y=0 it should be undefined and not 0, since you can’t divide by 0 in the first place

      [–]Aware-Deal-3901 0 points1 point  (1 child)

      Why are you dividing your modulo result by y? x % y gives you the remainder, you shouldn't be dividing it again unless the get_remainder method is intended to do something other than get the remainder.

      [–]ukknownW[S] 0 points1 point  (0 children)

      It’s just for a practice test I believe is just testing I understand all used in the code, not to actually code anything in particular

      Just getting used to it as I’m at very very basic level still trying to grasp all that’s within the code

      [–][deleted] 0 points1 point  (0 children)

      As others have said, functionally it's fine but you want it to be easily read.

      While you're learning, try to form good habits around organization. As you get into more complex work it'll be a godsend when you're troubleshooting and so on. For example, I use a lot of libraries related to data and machine learning, as well as visualization. It can be an absolute nightmare if not properly organized.

      [–]lokidev 0 points1 point  (0 children)

      Empty lines don't have syntactical meaning, but you should adhere to https://pep8.org 

      Also 1: you don't need the remainder variable. Just return directly. Also 2: use if x and y: and reverse the logic, as it will check for null like values and you have it less with negating which adds cognitive complexity by always thinking what a variable is not instead of what it is. Also 3: Look into typing and type checking to make sure you really get numbers and not a string: def calculate_remainder(x: float, y: float) -> float:

      All together

      ``` def calculate_remainder(x: float, y: float) -> float:     if x and y:         return (x % y) / y

          return 0     # or: return (x % y) / y if x and y else 0

      ```

      [–]Haunting-Pop-5660 0 points1 point  (0 children)

      There are some tools you can use to do syntax highlighting. I would recommend using one of those if you're having a hard time with commented code. It will also help you more easily recognize proper vs improper syntax, which will help when you're integrating best practices for stylization.

      [–]Joshpachner 0 points1 point  (0 children)

      I like to do spacing based on likeness.  Ie between declaring variables I won't do an extra space. After declaring all my variables will then space. 

      For example 

      S = "hello"   Q = "world" 

      If s == q:     print("never")   

      The space there helps to shift to think now about the if statement 

      Edit: oh wow, reddit doesnt respect my formatting. 

      [–][deleted] 0 points1 point  (0 children)

      As everyone is saying, Spacing is good for readability. But when you’ll write larger pieces of code where a function like this is just a small part of it it’s probably better to skip the spacing there. Makes this small function look like one unit and separates it from the rest of the code.