python中的除法运算’/'的结果值始终为浮点数,即使能够整除:
C:\Users\lenovo>python
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> type(2/2)
<class 'float'>
因此,在判断一个大于等于2的整数是否为2的倍数时,不能使用type函数来判断:
>>> type(2/2) == 'int'
False
这时可以使用取余运算’%'来判断:
>>> 2 % 2 == 0
True
python里浮点数和整数可以直接相等,不受数据类型影响:
>>> type(2/2)
<class 'float'>
>>> type(1)
<class 'int'>
>>> 2/2 == 1
True