获取值的类型可以使用type()方法:
type(50),type("50"),type([5,5]),type({5:5})
分别可以得到
<class 'int'>,<class 'str'>,<class 'list'>,<class 'dict'>
判断值的类型仍然可以使用type()方法:
值的类型是int,打印true
if type(50) is int:
print("是int")
值的类型不是str,打印false
if type(50) != str:
print("不是str")
判断值的类型还可以使用isinstance(50,int),直接返回True或者False
值的类型是int,打印结果
if isinstance(50, int):
print("是int")
值的类型不是str,打印结果
if not isinstance(50, str):
print("不是str")