首页 > 试题广场 >

what gets printed? Assuming py

[单选题]
what gets printed? Assuming python version 2.x()

print type(1/2)

  • <type 'int'>
  • <type 'number'>
  • <type 'float'>
  • <type 'double'>
  • <type 'tuple'>

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,为浮点型。

可参考官方文档:

Python2 Numeric Types

Python3 Numeric Types

编辑于 2018-02-11 22:52:36 回复(0)
注意区分python 版本问题,如果是python3,就是float类型。
发表于 2018-04-18 17:30:09 回复(1)
python 3

python 2

发表于 2019-03-02 10:58:38 回复(0)
python2.x和python3.x关于整除的不同
Python2.x
>>>print 3/2
1
>>>print 3//2
1
>>>print 3/2.0
1.5
>>>print 3//2.0
1.0

Python3.x
>>>print(3/2)
1.5
>>>print(3//2)
1
>>>print(3/2.0)
1.5
>>>print(3//2.0)
1.0
 
编辑于 2018-01-10 22:14:09 回复(0)
能不能别给我出python2的题目了,我接触的就是3.9,哪会2啊
发表于 2021-06-03 12:58:47 回复(0)
python2中:
1.如果两个整数相除,1/2=0 
2.如果操作数之一是浮点数,则执行真正的除法: 1.0/2.0=0.5
python3 中:
除法为正常除法,1/2=0.5
发表于 2018-08-23 00:02:03 回复(0)
Python2.x 做/运算时,两个参数都是int,结果也为int
发表于 2018-01-20 00:07:01 回复(0)
谁能告诉我为什么
发表于 2018-01-08 19:42:28 回复(7)
Python2为向下取整运算,所以结果为0,为整型 Python3为数学式运算,结果为0.5,为浮点型 选A
发表于 2023-04-22 16:55:28 回复(0)
如果是python 3,就会写成这样
print(type(1/2))

发表于 2022-02-21 17:12:56 回复(0)
python2,太阴了
编辑于 2024-01-25 23:10:56 回复(0)
现在大家都用3,你这题出的真的是闲的没事做吧
发表于 2023-08-11 09:45:47 回复(0)
为啥要掌握这么奇怪的姿势
发表于 2023-08-04 17:47:29 回复(0)
一会用python2判别答案,一会又用python3判别,给我整不自信了
发表于 2023-02-13 12:15:23 回复(0)
python2,向下取整
发表于 2022-11-22 18:57:29 回复(0)
好家伙,没看到 python version 2.x()
发表于 2022-09-16 16:45:13 回复(0)
/得的是整数
发表于 2022-08-17 15:21:51 回复(0)
版本不同
发表于 2022-06-02 18:05:42 回复(0)

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

发表于 2022-05-17 18:53:29 回复(0)
py2intpy3float
发表于 2021-12-23 20:36:42 回复(0)