此篇文章是我學習Python整理的筆記,如果有任何錯誤請留訊息給我,萬分感謝。

Numeric Types int, float, complex

Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
Float, or “floating point number” is a number, positive or negative, containing one or more decimals.

Note: You cannot convert complex numbers into another number type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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))

1.0
2
(1+0j)
<class ‘float’>
<class ‘int’>
<class ‘complex’>