1

Because that python decide that a=1/3 is int it makes the value of a to be 0, which is very not expected. I believe that reading full documentation will explain it, but lot of search in google didn't find a simple answer for this question.

RafaelJan
  • 3,118
  • 1
  • 28
  • 46
  • 5
    Upgrade to Python 3, it's fixed there. To elaborate on that, check the according release notes, it explains this change. From there, you will probably be able to find further info on that topic and the background. – Ulrich Eckhardt Jan 15 '19 at 07:20
  • 1
    1 and 3 are both `int`, so `1/3` is also an `int`. – Sweeper Jan 15 '19 at 07:20
  • 2
    If you want to learn why, Google "division operators in Python" and how they behave differently in Python2.x and Python 3.x. – jberrio Jan 15 '19 at 07:22
  • Different languages handle `Integer / Integer` differently, but many common ones default to integer division (e.g. Java) – juanpa.arrivillaga Jan 15 '19 at 08:05
  • Python 2 borrowed this behaviour from C, which borrowed it from Fortran. Other languages also borrowed it, such as Java, SQL, and Algol-W. But it has come to be regarded as a misfeature. – BoarGules Jan 15 '19 at 09:21

4 Answers4

2
a=1/3

is an expression and is evaluated as int / int which yields an int result.

a=0.3

is value which is not evaluated and is a float.

However if you still want to use the expression form you will need that either of the numbers to be float, meaning:

a=1.0/3

or

a=1/3.0

The latter two will yield a float result.

Orit
  • 314
  • 2
  • 6
0

In Python2 division of two int will be an int. but in python3 it is fixed.

>>type(1/3)
<type 'int'>

if you want to have them in float in Python2:

>>from __future__ import division
>>type(1/3)
<type 'float'>

or you can get result in float by casting one of them to float:

>>type(1/float(3))
<type 'float'>

in Python3 / result will be float and // result will be int.

In [1]: type(1/3)
Out[1]: float

In [2]: type(1//3)
Out[2]: int
Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59
0

@UlrichEckhardt is really correct, in Python 3, this is what you get:

>>> a=1/3
>>> a
0.3333333333333333
>>> type(a)
<class 'float'>
>>> 

But in Python 2, at least one value should be float:

>>> a=1.0/3
>>> a
0.3333333333333333
>>> type(a)
<class 'float'>
>>> 

Or:

>>> a=1/3.0
>>> a
0.3333333333333333
>>> type(a)
<class 'float'>
>>> 
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
0

There are differences between python2 and python3:

With python2:

$python
Python 2.7.6 (default, Nov 13 2018, 12:45:42)

>>> type(1/3)
<type 'int'>
>>> type(0.3)
<type 'float'>

With python3:

$python3
Python 3.6.1 (default, Dec 19 2018, 09:17:24)

>>> type(1/3)
<class 'float'>
>>> type(0.3)
<class 'float'>

Shortly:

  • In python2: / when used with integers would mean integer division, then 1/3 simply = 0 -> integer.
  • In python3: / even used with integers would mean real number division, then 1/3 = 0.33333... -> float.
duong_dajgja
  • 4,196
  • 1
  • 38
  • 65