1

When I type regular division, it defaults to floor. Why is it doing this? How do I change it? Will I have to change it every time?

ex:

>>>97/20
4 
>>>97//20
4
pppery
  • 3,731
  • 22
  • 33
  • 46

3 Answers3

3

That's because both numbers are integers, and in Python 2 it works that way: dividing two integers uses floor division. You could see a difference if you did this:

>>>97.0/20.0
4.85
>>>97.0//20.0
4.0
Alejandro De Cicco
  • 1,216
  • 3
  • 17
  • https://stackoverflow.com/questions/183853/what-is-the-difference-between-and-when-used-for-division – AMC May 03 '20 at 00:27
2

It seems like you are using python 2.x, this is not the problem with python 3.x

If you want to get precise result use 97/20.0.

Adding 20.0 will do the type casting of the result to float

Anmol Batra
  • 159
  • 1
  • 8
  • https://stackoverflow.com/questions/183853/what-is-the-difference-between-and-when-used-for-division – AMC May 03 '20 at 00:27
2

You’re using Python2. The behaviour when dividing integers changed to float division by default in Python3. So if you want float division by default for integers use Python3 or place

from __future__ import division

at the top of your code to use that feature.

In fact, there are many reasons why it’s probably better for you start using Python3 right away.

innisfree
  • 2,044
  • 1
  • 14
  • 24
  • https://stackoverflow.com/questions/183853/what-is-the-difference-between-and-when-used-for-division – AMC May 03 '20 at 00:27