Python除法和浮点数

2024-03-29 04:57:39 发布

您现在位置:Python中文网/ 问答频道 /正文

根据这里的Python教程https://docs.python.org/3/tutorial/introduction.html

这将给出一个浮点数:

>>> 17 / 3  # classic division returns a float
5.666666666666667

但是,当我尝试时,我得到:

>>> 17 / 3
5

怎么会这样?FWIW,我在Mac上使用Python 2.7.10。你知道吗


Tags: httpsorgdocsmachtml教程floattutorial
2条回答

这种行为在python2.7和python3之间发生了变化。你知道吗

答案在URL中:

https://docs.python.org/3/tutorial/introduction.html
https://docs.python.org/2.7/tutorial/introduction.html

您发布的URL是针对Python3的,但您使用的是Python2.7。你知道吗

页面顶部有一个下拉列表。您可以从中选择2.7,以获取Python2.7的文档:

>>> 17 / 3  # int / int -> int
5

相关问题 更多 >