Number
數字,是一個大的分類,細分四小類
- 整數:int
- 浮點數:float
- 布爾:bool
- 復數:complex
int 的栗子
print(type(-1)) print(type(1)) print(type(-999999999999999)) print(type(9999999999999999)) // 輸出結果 <class 'int'> <class 'int'> <class 'int'> <class 'int'>
- 無論正數負數都是 int
- 即使數字再長也還是 int,不會變成像 java 的 long
float 的栗子
print(type(-1.0)) print(type(1.11)) print(type(-1.11111111111111)) //輸出結果 <class 'float'> <class 'float'> <class 'float'>
即使精度再大,也還是 float,不會像 java 分單精度、雙精度
加法
print(type(1 + 1)) print(type(1 + 1.0)) print(type(1 + 0.0)) print(type(1 + 1.11)) # 輸出結果 <class 'int'> <class 'float'> <class 'float'> <class 'float'>
- int + int = int
- int + float = float,會自動轉型為浮點數
- float + float = float
減法
print(type(1 - 1)) print(type(1 - 0.0)) print(type(1 - 1.1)) print(type(2.0 - 1)) # 輸出結果 <class 'int'> <class 'float'> <class 'float'> <class 'float'>
和加法一個道理
乘法
print(type(1 * 1)) print(type(1 * 1.0)) print(type(-1 * -1.0)) print(type(2.0 * 1)) # 輸出結果 <class 'int'> <class 'float'> <class 'float'> <class 'float'>
和加減法一個道理
除法
print(type(2 / 2)) print(type(2 / 1.0)) print(type(2 // 2)) print(type(2 // 1.0)) # 輸出結果 <class 'float'> <class 'float'> <class 'int'> <class 'float'>
和加減乘法稍稍不一樣哦,具體看下面
/ 和 // 的區別
- / 除法,自動轉型成浮點數
- // 整除,只保留整數部分
print(2 / 2) print(2 // 2) print(1 / 2) print(1 // 2) # 輸出結果 1.0 1 0.5 0
進制數
10 進制
- 0,1,2,3,4,5,6,7,8,9
- 滿 10 進 1 位
- 正常寫的 Number 都是 10 進制
2 進制
- 0,1
- 滿 2 進 1 位
# 二進制 print(0b10) # 2^1 + 0 print(0b11) # 2^1 +2^0 print(0b100) # 2^2 + 0 + 0 # 輸出結果 2 3 4
8 進制
- 0,1,2,3,4,5,6,7
- 滿 8 進 1 位
# 八進制 print(0o1) # 1 print(0o11) # 8^1 + 1 print(0o117) # 8^2 + 8^1 + 7 # 輸出結果 1 9 79
16 進制
- 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
- 滿 16 進 1 位
# 十六進制 print(0x1) # 1 print(0x19) # 16+9 print(0x2A) # 16*2+10 print(0x9F) # 16*9+15 # 輸出結果 1 25 42 159
int() 轉成十進制
int 可以將數字字符串和 Number 類型的值轉成整數
# 轉成十進制 print(0b101) print(0o777) print(0xBBB) print(int(0b101)) print(int(0o777)) print(int(0xBBB)) print(int("-123")) print(int(1.1)) print(int(1.9)) # 輸出結果 5 511 3003 5 511 3003 -123 1 1
- 不寫 int() 的話,也可以將其他進制的數自動轉成十進制
- int() 能將純整數(不能是浮點數)的字符串轉成 int 類型
- 傳入浮點數不會進行四舍五入,直接取整數部分
bin() 其他進制數轉二進制
# 轉成二進制 print(bin(10)) # 10 轉成 2進制 print(bin(0o7)) # 7 轉成 2進制 print(bin(0xA)) # 10 轉成 2進制 print(bin(0o27)) # 8*2+7 轉成 2進制 print(bin(0x22E)) # 16^2*2+16*2+14 轉成 2進制 # 輸出結果 0b1010 0b111 0b1010 0b10111 0b1000101110
oct() 其他進制轉成八進制
# 轉成八進制 print(oct(110)) print(oct(0b100)) print(oct(0xAAA)) # 輸出結果 0o156 0o4 0o5252
hex() 其他進制轉成十六進制
# 轉成十六進制 print(hex(110)) print(hex(0b100)) print(hex(0o777)) # 輸出結果 0x6e 0x4 0x1ff
求模
# 求模 print(5 % 2) print(5.1 % 2) # 輸出結果 1 1.0999999999999996
- 浮點數的求模結果為一堆小數位,而不是 1.1
- 因為在計算機內存,有的浮點數無法被精確的表示,在這里,只能使用近似值來表示 1.1
靈魂拷問:為什么%
表示模除而不是“百分號”?
- 平時我們把它看做一個“百分號”
- 在編程計算中,通常把它和
/
一樣當做除法的運算符 - 求模是一個不同的運算,只是用
%
符號來表示
% 求模是如何運算的
- X 除以 Y 余 J 比如, 100 除以 16 余數為 4
- 100%16 就等於 4
冪運算
# 冪運算 print(2 ** 3) # 2 的 3次方 print(1.1 ** 3) # 1.1 的 3 次方 # 輸出結果 8 1.3310000000000004
bool
布爾類型
- 真:True
- 假:False
# 打印 bool 和 type print(True) print(False) print(type(True)) print(type(False)) # 輸出結果 True False <class 'bool'> <class 'bool'>
注意不是 true 和 false哦
為什么說 bool 屬於 Number 的一種呢?
# 可以將它轉成 int 呢? print(int(True)) print(int(False)) # 輸出結果 1 0
因為 int 能講 bool 轉成整型,True 就是 1,False 就是 0
那只有 1 和 0 能表示 True 和 False嗎?
並不是
Number
# 數字 print(bool(1)) print(bool(1.1)) print(bool(-1)) print(bool(0)) # 輸出結果 True True True False
字符串
# 字符串 print(bool("123")) print(bool("")) print(bool(" ")) print(bool("\n")) # 輸出結果 True False True True
列表
# 列表 print(bool([1, 1])) print(bool([])) # 輸出結果 True False
元組
# 元組 print(bool((1, 1))) print(bool(())) # 輸出結果 True False
set
# set print(bool({1, 1, 1})) print(bool({})) # 輸出結果 True False
None
# None print(bool(None)) # 輸出結果 False
總結
無論什么數據類型,主要是空值就會為 False,非空就是 True
復數
- 36j,直接在數字后面加 j
- 用的比較少,不寫了