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))
注:複素数を別の数値型に変換することはできません。