Python provides a lot of different types for programming purposes. While using these structs we may need to decide which variable or identifier is which type. Python provides builtin type
function in order to decide given identifier types. We just need to provide the identifier to the type
function.
Python为编程目的提供了许多不同的类型。 在使用这些结构时,我们可能需要确定哪个变量或标识符是哪种类型。 Python提供了内置的type
函数,以便确定给定的标识符类型。 我们只需要向type
函数提供标识符即可。
识别类型 (Identify Type)
We will provide our identifier or variable like below and type
function will return related type.
我们将提供如下所示的标识符或变量,并且type
函数将返回相关的类型。
name="ismail"
type(name)
#<class 'str'>
We can see that name
is a string object or class type
我们可以看到name
是一个字符串对象或类类型
检查类型(Check Type)
We can check type
returned types with is
keyword. We will first provide type
function and its parameter identifier or variable and than put is
and as the latest type we want to check. In this example we will check if given identifier is string. This will return a boolean result like True
or False
.
我们可以使用is
关键字检查type
返回的类型。 首先,我们将提供type
的功能和它的参数标识符或变量,不是把is
和最新型的,我们要检查。 在此示例中,我们将检查给定的标识符是否为字符串。 这将返回布尔结果,如True
或False
。
name="ismail"
type(name) is str
#True
是清单 (Is List)
We can check whether given identifier or variable is a list like below.
我们可以检查给定的标识符或变量是否是如下所示的列表。
mylist=['test']
type(mylist) is list
#True
是字典 (Is Dictionary)
We can check whether given identifier or variable is a dictionary like below.
我们可以检查给定的标识符或变量是否像下面的字典。
mydict={'a':'b','c':'d'}
type(mydict) is dict
#True
是字符串 (Is String)
We can check whether given identifier or variable is a string like below.
我们可以检查给定的标识符或变量是否为字符串,如下所示。
name="ismail"
type(name) is str
#True
是整数 (Is Integer)
We can check whether given identifier or variable is a integer like below.
我们可以检查给定的标识符或变量是否为整数,如下所示。
mynum=1
type(mynum) is int
#True
是对象 (Is Object)
We can check whether given identifier or variable is a object like below.
我们可以检查给定的标识符或变量是否是如下所示的对象。
type(mynum) is object
#False