I’m using Python 3.1.2 (Mac OS X 10.6) and found this weird behavior (I’m a newbie, btw):
On the interactive prompt:
>>> fraction = 4 / 3
>>> print(fraction)
1.33333333333
>>> print(type(fraction))
<class 'float'>
However, if I do the same thing in a script, results are different:
## fraction.py
fraction = 4 / 3
print(fraction)
print(type(fraction))
Output:
1
<class 'int'>
Is this normal?
No it’s not normal. Are you sure you are running Python 3 in that script? It’s possible that Python 2.5 (the default install on Mac OS X) is chosen. Try to verify by
If you are running the script as
./fraction.py, you could force the shell to use Python 3.1 by puttingin the first line.