A Noob Question

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
fedilink
12
edit-2
1Y

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.

kazutrash
creator
link
fedilink
3
edit-2
1Y

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
fedilink
4
edit-2
1Y

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
link
fedilink
41Y

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
link
fedilink
3
edit-2
1Y

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
creator
link
fedilink
21Y

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
link
fedilink
2
edit-2
1Y

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 using SO’s own search engine actually.

News and discussions about the programming language Python


  • 0 users online
  • 1 user / day
  • 1 user / week
  • 1 user / month
  • 11 users / 6 months
  • 1.12K subscribers
  • 133 Posts
  • 74 Comments
  • Modlog