Learn Programming in Python with Cody Jackson
上QQ阅读APP看书,第一时间看更新

Python 2 versus Python 3 division

While most Python 2.7 code is compatible with 3.x code, you can see that certain things don't carry over well. For example, Python 2 truncates the output of division calculations:

# Python 2
>>> 7/2
3

Python 3, as the following shows, provides the remainder when dividing. This is important to remember, as the code you're writing will break if it uses features or side effects of a particular version but is run on a different version:

# Python 3
>>> 7/2
3.5