文本类型: | str |
数值类型: | int,float,complex |
序列类型: | list,tuple,range |
映射类型: | dict |
集合类型: | set,frozenset |
布尔类型: | bool |
二进制类型: | bytes,bytearray,memoryview |
获取数据类型
您可以使用 type() 函数获取任何对象的数据类型:
实例
打印变量 x 的数据类型:
a = 5
peint(type(a))
#运行结果:
#<class 'int'>
设置数据类型
在 Python 中,当您为变量赋值时,会设置数据类型:
示例 | 数据类型 | |
---|---|---|
x = "Hello" | str | |
x = 10 | int | |
x = 12.5 | float | |
x = 5h | complex | |
x = ["ok", "my", "form"] | list | |
x = ("apple", "oringes", "banana") | tuple | |
x = range(5) | range | |
x = {"name" : "xiao", "age" : 22} | dict | |
x = {"apple", "banana", "oringes"} | set | |
x = frozenset({"apple", "banana", "oringes"}) | frozenset | |
x = true | bool | |
x = b"Hello" | bytes | |
x = bytearray(5) | bytearray | |
x = memoryview(bytes(5)) | memoryview |