一、Number(數值)
1) 整數 : int
2) 浮點數: float
1 type(1) //int 2 type(1.0) // float 3 type(1+1) // int , 2 4 type(1+0.1) // float, 1.1 5 type(1+1.0) // float, 2.0 6 7 type(1*1) // int, 1 8 type(1*1.0) // float, 1.0 9 type(1/1) // float, 1.0 10 type(1//1) // int, 1 取整 11 type(1/2) // float, 0.5 12 type(1//2) // int, 0 取整
3) 復數(實際中很少用): complex: 36j, 1+2x
4) 布爾值(在Python2中bool不屬於Number類型): bool [True, False]
1. int(True) == 1, int(False) == 0
2. bool(1) == True,bool(0) == False,bool(2) == True,bool(-1) == True,bool('') == False, bool([]) == False, bool(()) == False, bool({}) == False, bool(None) ==False
3. 總結:bool(非空值) == True, bool(空值或0或None) == False
5) 進制及轉換:
二進制(0b**): 0b10==2, 0b11==3 ;方法: bin()
八進制(0o**):0o10==8, 0o11==9 ;方法:otc()
十進制:10==10, 9==9, 1==1 ;方法: int()
十六進制(0x**): 0x10==16, 0x11==17,0x1F==31 ;方法: fex()
二、String(字符串)
1) 表示方法(必須成對出現): 單引號(' hello '), 雙引號(" hello "), 三引號(''' hello ''' 或 """ hello """)
1. 特殊情況 : "let's go" 內部的單引號為字符, 如外部使用單引號, 內部需使用雙引號或者將單引號轉義 ' let\'s go '
2. 三引號內字符串允許換行, 其他不允許換行
2) type(1) => int;type('1') => str
3) 特殊字符需轉義(要將\轉義則前面再加\, 即\\則輸出一個\字符)
\n 換行
\' 單引號
\t 橫向制表符
\r 回車
\ n
4)字符串操作
1. 字符串拼接(只有+和*) :
'hello ' + 'world' => 'hello world'
'hello' * 3 => 'hello hello hello'
2. 字符串切片:
1 'hello'[0] // 'h' 2 'hello'[3] // 'l' 3 'hello'[-1] // 'o' 4 'hello'[-4] // 'e' 5 'hello world'[0:4] // 'hell' 索引0開始,至索引4-1位置 6 'hello world'[0:-1] // 'hello wolr' 索引0開始, 除去倒數第1個 7 'hello world'[3:10] // 'lo worl' 索引3開始,至索引10-1位置 8 'hello wordl'[3:20] // 'lo world' 索引3開始, 至最后位置, 因為字符串長度不夠20 9 10 11 'hello world'[3:] // 'lo world' 索引3開始至最后位置 12 'hello world'[:-3] // 'hello wo' 除去后三位 13 'hello world'[0:-3] // 同上 14 'hello world'[-3:] // 'orld' 從倒數第三位置截取到最后一位置
3. 原始字符串(特殊符號不用轉義)
r' hello world ' ; r' let 's go ' ; r' C:\Windows'