Given some of the objects in python, we have to determine their types.
给定python中的某些对象,我们必须确定它们的类型。
To determine the type of an object, we use type() function – which is a built-in function in python.
为了确定对象的类型 ,我们使用type()函数 –这是python中的内置函数。
Python type()函数 (Python type() function)
type() function is used to determine the type of an object, it accepts an object or value and returns it's type (i.e. a class of the object).
type()函数用于确定对象的类型 ,它接受对象或值并返回其类型(即对象的类)。
Syntax:
句法:
type(object)
Example:
例:
Input:
b = 10.23
c = "Hello"
# Function call
print("type(b): ", type(b))
print("type(c): ", type(c))
Output:
type(b): <class 'float'>
type(c): <class 'str'>
确定对象类型的Python代码 (Python code to determine the type of objects)
# Python code to determine the type of objects
# declaring objects and assigning values
a = 10
b = 10.23
c = "Hello"
d = (10, 20, 30, 40)
e = [10, 20, 30, 40]
# printing types of the objects
# using type() function
print("type(a): ", type(a))
print("type(b): ", type(b))
print("type(c): ", type(c))
print("type(d): ", type(d))
print("type(e): ", type(e))
# printing the type of the value
# using type() function
print("type(10): ", type(10))
print("type(10.23): ", type(10.23))
print("type(\"Hello\"): ", type("Hello"))
print("type((10, 20, 30, 40)): ", type((10, 20, 30, 40)))
print("type([10, 20, 30, 40]): ", type([10, 20, 30, 40]))
Output
输出量
type(a): <class 'int'>
type(b): <class 'float'>
type(c): <class 'str'>
type(d): <class 'tuple'>
type(e): <class 'list'>
type(10): <class 'int'>
type(10.23): <class 'float'>
type("Hello"): <class 'str'>
type((10, 20, 30, 40)): <class 'tuple'>
type([10, 20, 30, 40]): <class 'list'>
翻译自: https://www.includehelp.com/python/determine-the-type-of-an-object.aspx