1、type
type(5) 会得到 int
type('5') 会得到 str
type(ture) 会得到 bool
type(5.2) 会得到 float
type(5.2e12) 会得到 float
2、isinstance
isinstance(5,int) 输出 ture
isinstance(5,str) 输出 false
备注:优先选用isinstance
本文介绍了Python中两种类型检查的方法:type()和isinstance()。type()用于获取变量的具体类型,如int、str、bool等;isinstance()则用于判断变量是否为指定类型或其子类。推荐使用isinstance()进行类型检查。
type(5) 会得到 int
type('5') 会得到 str
type(ture) 会得到 bool
type(5.2) 会得到 float
type(5.2e12) 会得到 float
isinstance(5,int) 输出 ture
isinstance(5,str) 输出 false
备注:优先选用isinstance

被折叠的 条评论
为什么被折叠?