Please enable java script to visit.
NOTEBOOK
HOMEPHP / MySQLJS / HTMLWXappPythonC++Blender其他
Python 使用内置函数 type 查看变量的类型 - NOTEBOOK
Python 使用内置函数 type 查看变量的类型
Python
Posted on 2023-05-26
摘要 : 在 Python 中,可以使用内置函数 type 查看变量的类型。type 函数返回的是一个表示变量类型的类对象。
x = 10
print(type(x)) # <class 'int'>

s = 'hello, world'
print(type(s)) # <class 'str'>

lst = [1, 2, 3]
print(type(lst)) # <class 'list'>

d = {'name': 'Bob', 'age': 20}
print(type(d)) # <class 'dict'>


上述代码中,type 函数分别用于查看变量 x、s、lst 和 d 的类型,并打印了类型对象。可以看到,type 函数返回的都是类对象,表示变量所属的类型。

需要注意的是,如果变量的值为 None,使用 type 函数会返回 <class 'NoneType'>,表示空值或缺失值类型。例如:


result = None
print(type(result)) # <class 'NoneType'>