str.isdigit() 和 float.is_integer()都可以判断数据是否是数字,但是二者是有一定区别的,具体见代码。
# str.isdigit(),用来判断1个字符串str是否全部都是数字,如果是,返回True
a = 5; print(a.isdigit()) # AttributeError: 'int' object has no attribute 'isdigit'
e = -5; print(e.isdigit()) # AttributeError: 'int' object has no attribute 'isdigit'
b = 5.5; print(b.isdigit()) # AttributeError: 'float' object has no attribute 'isdigit'
f = -5.5; print(f.isdigit()) # AttributeError: 'float' object has no attribute 'isdigit'
c = '5'; print(c.isdigit()) # True
d = '5.5'; print(d.isdigit()) # False
g = '-5'; print(g.isdigit()) # False
h = '-5.5'; print(h.isdigit()) # False
# ------------------------------------------------------------------------
# float.is_integer(),用来判断1个浮点数float是否是整数(小数点都是0),如果是,返回True(正负数均可)
a = 5; print(a.is_integer()) # AttributeError: 'int' object has no attribute 'is_integer'
e = -5; print(e.is_integer()) # AttributeError: 'int' object has no attribute 'is_integer'
c = '5'; print(c.is_integer()) # AttributeError: 'str' object has no attribute 'is_integer'
d = '5.5'; print(d.is_integer()) # AttributeError: 'str' object has no attribute 'is_integer'
g = '-5'; print(g.is_integer()) # AttributeError: 'str' object has no attribute 'is_integer'
h = '-5.5'; print(h.is_integer()) # AttributeError: 'str' object has no attribute 'is_integer'
i = -5.0; print(i.is_integer()) # True
j = 5.0; print(j.is_integer()) # True
b = 5.5; print(b.is_integer()) # False
f = -5.5; print(f.is_integer()) # False