Python入门教程之类型判别
Python入门教程:https://www.bilibili.com/video/BV1rmGczpEbG
可以通过一些方式查看或判断数据的类型。
查看类型
使用"type()"可以查看数据的类型
s1 = type(100)
s2 = type(3.14)
s3 = type(True)
s4 = type('你好')print(s1) # <class 'int'>
print(s2) # <class 'float'>
print(s3) # <class 'bool'>
print(s4) # <class 'str'>
变量没有数据类型,变量中存储的数据有数据类型。
判断类型
使用"isinstance()"可以判断数据是否为某种类型
b1 = isinstance(100, int)
b2 = isinstance(3.14, float)
b3 = isinstance(True, bool)
b4 = isinstance('你好', str)print(b1) # True
print(b2) # True
print(b3) # True
print(b4) # True