# Python type 示例代码
# 定义不同类型的变量
integer_var = 42
float_var = 3.14
string_var = "Hello, World!"
list_var = [1, 2, 3]
dict_var = {"key": "value"}
tuple_var = (1, 2, 3)
set_var = {1, 2, 3}
# 使用 type() 函数检查变量的类型
print(f"Type of integer_var: {type(integer_var)}") # 输出: <class 'int'>
print(f"Type of float_var: {type(float_var)}") # 输出: <class 'float'>
print(f"Type of string_var: {type(string_var)}") # 输出: <class 'str'>
print(f"Type of list_var: {type(list_var)}") # 输出: <class 'list'>
print(f"Type of dict_var: {type(dict_var)}") # 输出: <class 'dict'>
print(f"Type of tuple_var: {type(tuple_var)}") # 输出: <class 'tuple'>
print(f"Type of set_var: {type(set_var)}") # 输出: <class 'set'>
# 自定义类
class MyClass:
pass
my_object = MyClass()
print(f"Type of my_object: {type(my_object)}") # 输出: <class '__main__.MyClass'>
type()
函数用于返回对象的类型。type()
检查其类型。上一篇:python html转pdf
下一篇:python 字符串比较
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站