AI摘要
本文介绍了Python中type()函数的使用方法,包括直接输出类型信息、使用变量存储type()语句的结果以及查看变量中存储的数据类型信息。通过示例代码展示了如何使用type()函数来确定字符串、整数和浮点数的数据类型。文章还通过图片解释了变量的类型概念,并总结了type()函数的用途。
1、使用print直接输出类型信息
print(type("小新"))
print(type(2023))
print(type(2023.09))输出结果:
<class 'str'>
<class 'int'>
<class 'float'>str(string的缩写)为字符串,int为整数,float浮点型
2、使用变量存储type()语句的结果
string_type = type("小新")
int_type = type(2023)
float_type = type(2023.09)
print(string_type)
print(int_type)
print(float_type)输出结果:
<class 'str'>
<class 'int'>
<class 'float'>3、使用type()语句,查看变量中存储的数据类型信息
name = "小新"
name_type = type(name)
print(name_type)输出结果:
<class 'str'>变量有类型吗?
总结