如何检查变量是否包含int数据而不是浮动
因此,我需要一个程序来检查变量是否是整数,而不是浮动数据。
我尝试了:
var = 2.5
if var is int :
print(var)
else :
pass
但这无效。你能帮我吗?谢谢。
so I need a program that checks if a variable is integer and it's not float data.
I tried this:
var = 2.5
if var is int :
print(var)
else :
pass
but this didn't work. can you help me on this? thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以简单地使用
isinstance
检查您也可以使用
type 功能,但没有检查检查子类,因此不建议使用,但无论如何:
这里有一些有用的链接
programiziz isinstance
w3schools isinstance
isInstance官方文档
类型vs vs vs isinstance stackoverflow
you can use simply the
isinstance
checkyou could also use the
type
function but it doesn't check for subclasses so it's not recommended but provide anyways:here are some useful links
programiz isinstance
w3schools isinstance
isinstance official docs
type vs isinstance stackoverflow
只需使用
isInstance()
函数:Just use
isinstance()
function:您可以使用type方法
https://wwwww.programiz.com/ Python编程/方法/内置/类型
或
you can use the type method
https://www.programiz.com/python-programming/methods/built-in/type
or
您可以在此处使用用户type()。
例如 :
you can user type() here.
for example :