数据类型

数据类型 #

分为 int, float, bool, str, list 等,Python3 中整型和长整型结合在一起了。

一切皆对象

Decimal #

默认浮点数计算也会损失精度,需要使用 Decimal 类型。需要注意创建时使用字符串而不是数字,并且导入 decmial 库。

print(2 - 1.8)  # 0.19999999999999996
print(decimal.Decimal('2') - decimal.Decimal('1.8'))  # 0.2

类型转换 #

print(type(str(10)))
print(type(int('1')))

需要注意转为数字时如果字符串包含不是数字的值时会报错。

类型推断 #

if isinstance(foo, str):
    print('foo is a String')
沪ICP备17055033号-2