在Python中,你可以使用`type()`函数和`isinstance()`函数来判断数据类型。以下是使用这些函数的示例:
使用`type()`函数
x = 5
print(type(x)) 输出:
y = 3.14
print(type(y)) 输出:
z = "Hello"
print(type(z)) 输出:
lst = [1, 2, 3]
print(type(lst)) 输出:
tpl = (1, 2, 3)
print(type(tpl)) 输出:
dct = {'a': 1, 'b': 2}
print(type(dct)) 输出:
st = {1, 2, 3}
print(type(st)) 输出:
使用`isinstance()`函数
x = 5
print(isinstance(x, int)) 输出: True
y = 3.14
print(isinstance(y, float)) 输出: True
z = "Hello"
print(isinstance(z, str)) 输出: True
lst = [1, 2, 3]
print(isinstance(lst, list)) 输出: True
tpl = (1, 2, 3)
print(isinstance(tpl, tuple)) 输出: True
dct = {'a': 1, 'b': 2}
print(isinstance(dct, dict)) 输出: True
st = {1, 2, 3}
print(isinstance(st, set)) 输出: True
`type()`函数返回对象的类型,而`isinstance()`函数可以检查对象是否属于特定的类型,包括继承关系。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://sigusoft.com/bj/113355.html