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@lemmy.ml
    link
    fedilink
    arrow-up
    12
    ·
    edit-2
    4 years ago

    Hey, this is Python2, isn’t it? That would explain this behaviour, because in Python2, this would solve the division 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, this 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.

    • kazutrash@lemmy.mlOP
      link
      fedilink
      arrow-up
      3
      ·
      edit-2
      4 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@lemmy.ml
        link
        fedilink
        arrow-up
        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 :)

  • cheer@lemmy.ml
    link
    fedilink
    arrow-up
    4
    ·
    4 years ago

    As the other guy said, you’re using a really old version of python. For reference, the current version is 3.9.2, but using any python3 would give you the result you need.

  • roastpotatothief@lemmy.ml
    link
    fedilink
    arrow-up
    3
    ·
    edit-2
    4 years ago

    The first thing you need to know about programming: stackoverflow.com. Any question you will ever have, somebody has already posted the answer there.

    Once you are using python 3, you’ll find there are two types of division

    • integer division: 15//4=3
    • floating point (normal) division: 15/4=3.75

    Often you do want integer division, so use the // operator.

    • kazutrash@lemmy.mlOP
      link
      fedilink
      arrow-up
      2
      ·
      4 years ago

      I really appreciate that explaining, thank you. I’ve already heard about StackOverflow but never searched through it very much, from now i’m going to take a look over it as probably will help me a lot.

      • Adda@lemmy.ml
        link
        fedilink
        arrow-up
        2
        ·
        edit-2
        4 years ago

        You don’t even need to search for SO (Stack Overflow) specifically. It very often happens to be the first result in your search feed, especially when trying to search for something programming- or tech-related. Thinking about it, I have never searched for something through SO’s own search engine actually.