如何查看变量的类型?(例如无符号32位)
当前回答
a = "cool"
type(a)
//result 'str'
<class 'str'>
or
do
`dir(a)`
to see the list of inbuilt methods you can have on the variable.
其他回答
python中有许多数据类型,如:
Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
None Type: NoneType
在这里,我写了一个代码,其中有一个包含所有类型数据类型的列表示例,并打印它们的类型
L = [
"Hello World",
20,
20.5,
1j,
["apple", "banana", "cherry"],
("apple", "banana", "cherry"),
range(6),
{"name" : "John", "age" : 36},
{"apple", "banana", "cherry"},
frozenset({"apple", "banana", "cherry"}),
True,
b"Hello",
bytearray(5),
memoryview(bytes(5)),
None
]
for _ in range(len(L)):
print(type(L[_]))
输出:
<class 'str'>
<class 'int'>
<class 'float'>
<class 'complex'>
<class 'list'>
<class 'tuple'>
<class 'range'>
<class 'dict'>
<class 'set'>
<class 'frozenset'>
<class 'bool'>
<class 'bytes'>
<class 'bytearray'>
<class 'memoryview'>
<class 'NoneType'>
Python中的简单类型检查示例:
assert type(variable_name) == int
assert type(variable_name) == bool
assert type(variable_name) == list
您可能正在查找type()内置函数。
请参见下面的示例,但Python中没有像Java一样的“无符号”类型。
正整数:
>>> v = 10
>>> type(v)
<type 'int'>
大正整数:
>>> v = 100000000000000
>>> type(v)
<type 'long'>
负整数:
>>> v = -10
>>> type(v)
<type 'int'>
文字顺序:
>>> v = 'hi'
>>> type(v)
<type 'str'>
浮点整数:
>>> v = 3.14159
>>> type(v)
<type 'float'>
a = "cool"
type(a)
//result 'str'
<class 'str'>
or
do
`dir(a)`
to see the list of inbuilt methods you can have on the variable.
您应该使用type()函数。像这样:
my_variable = 5
print(type(my_variable)) # Would print out <class 'int'>
此函数将查看任何变量的类型,无论是列表还是类。查看此网站了解更多信息:https://www.w3schools.com/python/ref_func_type.asp
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 使用try和。Python中的if
- 如何在Python中获得所有直接子目录