确定Python中对象的类型

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值