Python/gloss python type conversion

来自菜鸟教程
机器人讨论 | 贡献2020年10月29日 (四) 08:48的版本 (机器人:添加分类Python基础教程
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至:导航、​搜索

<languages />

Python类型转换

类型转换

您可以使用 int() , float() ,和 complex() 方法:

从一种类型转换为另一种类型:

  x = 1 # int
y = 2.8 # float
z = 1j # complex

#convert from int to float:

  a = float(x)

#convert from float to int:

  b = int(y)

#convert from int to complex:
c = complex(x)

print(a)
print(b)

  print(c)

print(type(a))
print(type(b))

  print(type(c))

注意: 您不能将复数转换为另一种数字类型。