0
print (1/3)
print (1./3)

I don't see difference between (1/3) and (1./3). When should I use one or the other, and why?

Quoc Huy
  • 9
  • 3
  • please learn to copy/paste code snippets as _text_, not as images... – bruno desthuilliers Aug 01 '17 at 08:56
  • 2
    In Python 3.x, this will male no difference. In Python 2.x, the first form will perform an integer division and evals to `0`, which _does_ make a difference. – bruno desthuilliers Aug 01 '17 at 08:58
  • In addition to @brunodesthuilliers: You should also consider the different output of `/`-division and `//`-division. – albert Aug 01 '17 at 08:59
  • hi @quốc-huy, `1/3` will return 0 as it is an integer division in `python-2`. Use `1./3` to get the results. `Python3` does not see the difference. – Andy K Aug 01 '17 at 09:00

2 Answers2

1

This is done to make sure the output is a floating point number. 1./3 means 1.0 / 3 which would return 0.33333...

1/3 would produce 0 as an integer

EDIT: This is only valid in Python 2

T.Salvin
  • 211
  • 2
  • 10
0

It depends on the Python version you are using:

With the dot you can specify that the number should be treated as floating point number otherwise it will be an integer.

In Python 2 the devision of an integer will result in an integer value. In this case 0.3333 will be rounded to 0. Whereas in Python3 the devision will result in a floating point number never the less the dividend is an integer or a floating point number. In this case it will be in every case 0.3333

Querenker
  • 1,930
  • 1
  • 15
  • 25