同一个除法在Python2和3中不同解答

what gets printed? Assuming python version 2.x()

print type(1/2)

 

Python2 中除法默认向下取整,因此 1/2 = 0,为整型。

For (plain or long) integer division, the result is an integer. The result is always rounded towards minus infinity: 1/2 is 0, (-1)/2 is -1, 1/(-2) is -1, and (-1)/(-2) is 0.

而 Python3 中的除法为正常除法,会保留小数位,因此 1/2 = 0.5,为浮点型。

posted @ 2018-10-18 10:46  Misakikure  阅读(243)  评论(0编辑  收藏  举报