I’ve started to learn programming Python (also i’m a beginner) and my code’s print result was “0”. Could someone explain why?

I mean, on my calculator hardware it results on something like “0.5555552”, this has anything to do with the Python Interpreter’s code?

  • @Adda
    link
    12
    edit-2
    3 years ago

    Hey, this is Python2, isn’t it? That would explain this behaviour, because in Python2, the division would be solved as an integer division (whole numbers division) instead of float division (real numbers division). That means it will perform the division and grab only the whole part of the result number. That is zero in this case. If you add a real part (e.g.: 1000.0) to one of the numbers, the expected result should occur. For example: 10*1000.0/(5*3600)

    If you use Python3, your code would return the expected 0.555 without any problem. Btw., I would highly suggest to use Python3 instead of Python2. Python2 is no longer developed and it is discouraged to use it. Nothing would change for you at this stage, but Python3 is where the world lives now.

    • kazutrashOP
      link
      3
      edit-2
      3 years ago

      First: Thank you very much for explaining it, i haven’t knew that kind of division on Python (as if i’m a newbie on programming at all uhhh)

      Second: Now that you’ve mentioned it, i’m using Geany so probably it is running python2, because after running my code from Python3, it printed what i was expecting. I thought Geany was running Python3 as it is already installed on my system, both Python 2 and 3 came pre-installed, but running Python 2 was really unexpected.

      • @Adda
        link
        4
        edit-2
        3 years ago

        It may be that Geany calls for python and that defaults to python2 instead of python3 command, but I would be surprised if your OS still defaulted to Python 2. Glad you figured it out. Have fun learning Python :)