说明
可以通过type()函数来得到数据的类型,语法格式为:
type(被查看类型的数据)
也可以通过type(变量)
来得到类型,但这本质是上查看变量存储的数据的类型。因为在python中,变量是没有类型的,但它存储的数据有类型。
示例
示例:用print直接输出类型信息
print(type('hello'))
print(type(11))
print(type(11.0))
运行输出:
示例:用变量存储类型信息
str_type = type('hello')
int_type = type(123)
float_type = type(1.23)
print(str_type)
print(int_type)
print(float_type)
运行输出:
示例:使用type()查看变量中存储的类型信息
name = 'hello'
print(type(name))
运行输出:
示例:使用type(变量)
本质是查看变量中存储的数据的类型
name = 'hello'
print(type(name))
name = 123
print(type(name))
运行输出: