The answer of "1/2" is different between python2 and 3

The answer for dividing integers is different between python2 and 3.

python2


>>> 1/2
0

python3


>>> 1/2
0.5    

If you're designing code that works on both python2 and python3 If you expect 1/2 = 0.5:

python2or3


>>> 1./2.
0.5

If you expect 1/2 = 0:

python2or3


>>> int(1/2)
0

You should do it like this.

** Addition **

By using the __future__ module, it seems that division can be performed in python2 series as well as in python3 series. The __future__ module has a function that complements the compatibility of python2 series and python3 series, and imports division in it.

python2or3



from __future__ import division

print(1/2)   # => 0.5
print(1//2)  # => 0

reference

--Comment by @ LouisS0616 -future module --- Salinger's programming diary

Recommended Posts

The answer of "1/2" is different between python2 and 3
Indent behavior of json.dumps is different between python2 and python3
The timing when the value of the default argument is evaluated is different between Ruby and Python.
Summary of the differences between PHP and Python
About the difference between "==" and "is" in python
The story of Python and the story of NaN
The behavior of @property is different between class definition and old and new styles
Difference between == and is in python
[python] [meta] Is the type of python a type?
What is the difference between `pip` and` conda`?
[Introduction to Python] What is the difference between a list and a tuple?
[Xonsh] The Python shell is sharp and god
Specifying the range of ruby and python arrays
the zen of Python
Compare the speed of Python append and map
This is the only basic review of Python ~ 1 ~
This is the only basic review of Python ~ 2 ~
This is the only basic review of Python ~ 3 ~
About the * (asterisk) argument of python (and itertools.starmap)
A discussion of the strengths and weaknesses of Python
What is the difference between Unix and Linux?
[Comparison of PHP, Ruby, and Python description] For those who are wondering how the description method is different.
Consideration of the difference between ROC curve and PR curve
The story of Python without increment and decrement operators.
Python --Explanation and usage summary of the top 24 packages
Difference between Ruby and Python in terms of variables
What is the difference between usleep, nanosleep and clock_nanosleep?
Visualize the range of interpolation and extrapolation with python
Referencing and changing the upper bound of Python recursion
I checked out the versions of Blender and Python
Visualization of the connection between malware and the callback server
The VIF calculated by Python and the VIF calculated by Excel are different .. ??
Why is the first argument of [Python] Class self?
About the ease of Python
[Python] Python and security-① What is Python?
About the features of Python
Identity and equivalence Python is and ==
Source installation and installation of Python
The Power of Pandas: Python
[Python] Heron's formula functionalization and calculation of the maximum area
Different from the import type of python. from A import B meaning
[python] plot the values ​​before and after the conversion of yeojohnson conversion
The process of making Python code object-oriented and improving it
A rough summary of the differences between Windows and Linux
The websocket of toio (nodejs) and python / websocket do not connect.
I want to know the features of Python and pip
What is the default TLS version of the python requests module?
I tried to enumerate the differences between java and python
[Tips] Problems and solutions in the development of python + kivy
Initial setting of Mac ~ Python (pyenv) installation is the fastest
Play with the password mechanism of GitHub Webhook and Python
Is the lottery profitable? ~ LOTO7 and the law of large numbers ~
pca.components_ of sklearn is the correlation coefficient between the principal component and the feature, and is called the factor loading.
Python a + = b and a = a + b are different
Environment construction of python and opencv
Difference between Ruby and Python split
Difference between java and python (memo)
Difference between list () and [] in Python
Installation of SciPy and matplotlib (Python)
[Python] The stumbling block of import
First Python 3 ~ The beginning of repetition ~