通过type()可以查看数据类型,type()会返回当前数据的具体类型。
# 使用变量接收 type() 返回的类型
result1 = type('张三')
result2 = type(18)
result3 = type(72.5)
print(result1) # <class 'str'> 注意此处返回的不是string,是 string 的简写:str
print(result2) # <class 'int'>
print(result3) # <class 'float'>
注意:在 Python 中:变量无类型,数据有类型。
例如a = 10,其中a是没有类型的,但a所关联的数据10是有类型的,10是整型,我们经常说a是整型,其实是一种不太严谨的表述,严谨的表述应该是:a所对应的数据10是整型。
也可以把变量交给type(),最终返回的是:变量所对应的数据的类型。
name = '张三'
age = 18
weight = 72.5
# 使用变量接收type()返回的类型
result1 = type(name)
result2 = type(age)
result3 = type(weight)
# 打印这三个数据类型
print(result1) # <class 'str'>
print(result2) # <class 'str'>
print(result3) # <class 'float'>
当然也可以不使用变量接收,直接打印type()的结果
name = '张三'
age = 18
weight = 72.5
# 打印这三个数据类型
print(type(name)) # <class 'str'>
print(type(age)) # <class 'str'>
print(type(weight)) # <class 'float'>
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END














请登录后查看评论内容