No10.內建函式和匿名函式
1.內建函式
python3.6中一共提供了68個內建函式,接下來就讓我們看看有哪些。
內建函式分類:
① 與作用域相關:
globals() —— 獲取全域性變數的字典
locals() —— 獲取執行指令碼方法所在名稱空間內的區域性變數的字典
② 其他:
字串型別程式碼的執行:
eval()
exec()
compile()
# exec和eval都可以執行字串型別的程式碼 # eval有返回值,適用於有結果的簡單計算 # exec沒有返回值,適用於流程控制 # eval只能用在你明確的知道要執行的程式碼是什麼 >>> exec('print(123)') 123 >>> eval('print(123)') 123 >>> print(exec('1+2+3')) None >>> print(eval('1+2+3')) 6 # compile編譯 >>> code = 'for i in range(10):print(i)' >>> compile1 = compile(code, '', 'exec') >>> exec(compile1) 0 1 2 3 4 5 6 7 8 9
輸入輸出相關:
input
# print原始碼 def print(self, *args, sep=' ', end='\n', file=None): # known special case of print """ print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream. """ pass 引數說明: file:預設輸出到螢幕,如果設定檔案控制代碼,輸出到檔案 sep:列印多個值之間的分隔符,預設為空格 end:每一次列印的結尾,預設為換行符 flush:立即把內容輸出到流檔案,不做快取 >>>f = open('tmp_file','w') >>>print(123,456,sep=',',file = f,flush=True)
資料型別相關:
type(value)返回引數的資料型別
記憶體相關:
id(value):返回引數的記憶體地址
hash(value):返回一個可hash變數的的雜湊值,不可hash的變數被hash後會報錯
檔案操作相關:
open():詳細用法見檔案處理
模組操作相關:
import:匯入模組
幫助方法:
help():在控制檯執行help()進入幫助模式。可以隨意輸入變數或者變數的型別。輸入q退出
或者直接執行help(o),o是引數,檢視和變數o有關的操作。
和呼叫相關:
callable(value):判斷引數是否可以呼叫
檢視引數所屬型別的所有內建方法:
dir():預設檢視全域性空間內的屬性,也接受一個引數,檢視這個引數內的方法或變數
③ 和數字相關
資料型別相關:bool,int,float,complex
進位制轉換相關:bin,oct,hex
數學運算:abs,divmod,min,max,sum,round,pow
print(divmod(7, 2)) #除餘方法,返回div除,mod取餘
print(round(3.1415, 3)) #保留幾位小數
print(pow(2, 3)) #冪運算
print(pow(2, 3, 1)) #冪運算之後再取餘
print(sum([1, 2, 3, 4, 56]))
>>>
(3, 1)
3.142
8
0
66
④ 和資料結構相關
列表和元組相關:list,tuple
字串相關:str,format,bytes,bytearry,memoryview,ord,chr,ascii,repr
#ord字元轉換為unicode碼
print(ord('是'))
print(ord('1'))
# chr將unicode碼轉換為字元
print(chr(1000))
>>>
26159
49
Ϩ
# bytes轉化成bytes型別
# 我拿到的是gbk編碼,我想轉成utf-8編碼
print(bytes('你好', encoding='gbk').decode('GBK')) #unicode轉換成GBK的bytes
print(bytes('你好', encoding='utf-8')) #unicode轉化成utf-8的bytes
>>>
你好
b'\xe4\xbd\xa0\xe5\xa5\xbd'
# bytearray操縱位元組碼
# 好處是在修改字串的時候可以節省記憶體,不過只能通過位元組編碼來修改
print(bytearray('你好', encoding='utf-8'))
>>>
bytearray(b'\xe4\xbd\xa0\xe5\xa5\xbd')
# memoryview切片——位元組型別 不佔記憶體,就是給你看看
# 在ASCII碼裡就原封不動的返回,不在就返回unicode
print(ascii('好'))
print(ascii('1'))
>>>
'\u597d'
'1'
# repr
name = 'egg'
print('你好%r' % name)
print(repr('1'))
print(repr(1))
>>>
你好'egg'
'1'
1
序列:reversed,slice
#reversed反轉保留原來的列表返回一個反向的迭代器
l = (1,2,23,213,5612,342,43)
print(l)
print(list(reversed(l)))
>>>
(1, 2, 23, 213, 5612, 342, 43)
[43, 342, 5612, 213, 23, 2, 1]
#slice切片方法
未完待續 》》》
2、匿名函式
一般形式:函式名 = lambda 引數 :返回值
# 一般函式
>>> def add(x, y):
return x+y
# 匿名函式
>>> clac = lambda x, y : x + y
>>> print(add(1, 2), clac(1, 2))
3 3
匿名函式一般與帶key的內建函式一起用:max,min,filter,map,sorted
t1 = (('a'), ('b'))
t2 = (('c'), ('d'))
print(list(map(lambda tup : {tup[0]:tup[1]}, zip(t1, t2))))
>>>
[{'a': 'c'}, {'b': 'd'}]