內置函數詳解:https://docs.python.org/3/library/functions.html?highlight=built#asciihtml
abs() # 取絕對值 dict() # 把數據轉爲字典 help() # 幫助 min() # 找出最小值 max() # 找出最大值 setattr() # 設置屬性值 bool() # 判斷True or False(bool(0)、bool(Flase)、bool([])) all() # 可循環的數據集合每一個元素bool()均爲True;或者空列表也是True any() # 任意一個值是True即返回True dir() # 打印當前程序裏的全部變量 hex() # 轉換爲16進制數 slice() # 提早定義切片規則 divmod() # 傳入兩個變量a、b,獲得a//b結果和餘數a%b sorted() # 列表排序sorted(li)等同於li.sort() 用法:sorted(iterable, key) ascii(2) # 只能返回ascii碼 enumerate([3,2,13,4]) # 返回列表的索引 input('dasd') oct(10) # 轉八進制 staticmethod() # bin(10) # 轉二進制 open() # 文件打開 str() # 轉字符串 isinstance() ord('a') # 返回97,ascii碼中'a'位置 chr(97) # 返回'a',輸入97位置返回ascii碼對應字符 sum([1,4,5,-1,3,0]) # 計算列表求和 pow(100,2) # 返回x的y次方,10000 callable() # 查看函數是否能夠調用,還可用於判斷變量是不是函數 format() vars() # 打印變量名和對應的值 locals() # 打印函數的局部變量(通常在函數內運行) globals() # 打印全局變量 repr() # 顯示形式變爲字符串 compile() # 編譯代碼 complex() # 將一個數變爲複數 ''' >>> complex(3,5) (3,5j) ''' round(1.2344434,2) # 指定保留幾位小數 輸出1.23 # delattr, hasattr, getattr, setattr # 面向對象中應用 hash() # 把一個字符串變爲一個數字 memoryview() # 大數據複製時內存映射 set() # 把一個列表變爲集合 ''' >>> set([12,5,1,7,9]) {1, 5, 7, 9, 12} '''
f = open("print.py") data =compile(f.read(),'','exec') # 參數:source(字符串對象), filename(代碼文件名), mode(編譯代碼種類exec\eval\single) exec(data) >>> str = "3 * 4 + 5" >>> a = compile(str,'','eval') >>> eval(a) 17
>>> d = {} >>> for i in range(10): ... d[i] = i - 50 ... >>> print(d) {0: -50, 1: -49, 2: -48, 3: -47, 4: -46, 5: -45, 6: -44, 7: -43, 8: -42, 9: -41} >>> d.items() dict_items([(0, -50), (1, -49), (2, -48), (3, -47), (4, -46), (5, -45), (6, -44), (7, -43), (8, -42), (9, -41)]) >>> sorted(d.items()) [(0, -50), (1, -49), (2, -48), (3, -47), (4, -46), (5, -45), (6, -44), (7, -43), (8, -42), (9, -41)] >>> sorted(d.items(), key = lambda x:x[1]) [(0, -50), (1, -49), (2, -48), (3, -47), (4, -46), (5, -45), (6, -44), (7, -43), (8, -42), (9, -41)] >>> sorted(d.items(), key = lambda x:x[1], reverse=True) [(9, -41), (8, -42), (7, -43), (6, -44), (5, -45), (4, -46), (3, -47), (2, -48), (1, -49), (0, -50)]
# eval() # 字符串轉代碼(只能處理單行代碼)(能夠拿到返回值) f = "1+3/2" eval(f) # 輸出結果:2.5 eval('print("hello world")') # 輸出結果:hello world # exec() # 字符串轉代碼(能夠解析多行代碼)(不能拿到返回值) code = "\nif 3>5:\n print('3 bigger than 5')\nelse:\n print('dddd')\n\n" exec(code) # 輸出結果:dddd # eval和exec()返回值驗證 code = ''' def foo(): print('run foo') return 1234 foo() res = eval("1+3+3") res2 = exec("1+3+3") res3 = exec(code) print('res',res,res2,res3) # 輸出結果:res 7 None None
# bytearray() 將字符串轉爲bytearray,完成修改後,decode()後,可在原內存地址修改字符串 >>> s = 'abcd路飛' >>> s 'abcd路飛' >>> s = s.encode('utf-8') >>> s b'abcd\xe8\xb7\xaf\xe9\xa3\x9e' >>> s = bytearray(s) >>> s bytearray(b'abcd\xe8\xb7\xaf\xe9\xa3\x9e') >>> s[4] 232 >>> s[4]=233 >>> s bytearray(b'abcd\xe9\xb7\xaf\xe9\xa3\x9e') >>> s.decode() 'abcd鷯飛' >>> id(s) # 修改內部元素,s指向的內存地址並不會改變 4352883880
map(lambda x:x*x , [1,2,3,4,5]) # 根據提供的函數對指定序列作映射 >>> list(map(lambda x:x*x , [1,2,3,4,5])) [1, 4, 9, 16, 25] >>>def square(x) : # 計算平方數 ... return x ** 2 ... >>> map(square, [1,2,3,4,5]) # 計算列表各個元素的平方 [1, 4, 9, 16, 25] >>> map(lambda x: x ** 2, [1, 2, 3, 4, 5]) # 使用 lambda 匿名函數 [1, 4, 9, 16, 25] # 提供了兩個列表,對相同位置的列表數據進行相加 >>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10]) [3, 7, 11, 15, 19]
filter() # 將符合條件的值過濾出來 # filter(function, iterable) >>> list(filter(lambda x: x>3, [1,2,3,4,5])) [4, 5] import math def is_sqr(x): return math.sqrt(x) % 1 == 0 newlist = filter(is_sqr, range(1, 101)) print(newlist) """ [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] """
>>> s = {12,3,4,4} >>> s.discard(3) # 集合刪除元素,沒有也不會報錯 >>> s {12,4} >>> s = frozenset(s) >>> s. # 已經沒有discard方法能夠調用
zip() # 可將兩個數組一一對應組成元祖 >>> a = [1,2,3,45,6] >>> b = ['a','b','c'] >>> zip(a) <zip object at 0x1034fe408> >>> list(zip(a,b)) [(1, 'a'), (2, 'b'), (3, 'c')]
>>> s = 'hey, my name is alex\n, from shandong' >>> print('haifeng','gangsf',sep='<-') haifeng<-gangsf msg = "回到最初的起點" f = open("print_tofile","w") print(msg,"記憶裏青澀的臉",sep="|",end="",file=f) print(msg,"已經不忍直視了",sep="|",end="",file=f) """ 生成print_tofile文件:回到最初的起點|記憶裏青澀的臉回到最初的起點|已經不忍直視了 """
slice() # 實現切片對象,主要用在切片操做函數裏的參數傳遞 # 例子:slice('start', 'stop', 'step') # 起始位置、結束位置、間距 a = range(20) pattern = slice(3, 8, 2) # 3到8,間隔兩個數 for i in a[pattern]: # 等於a[3:8:2] print(i)
reduce() 函數將一個數據集合(鏈表,元組等)中的全部數據進行下列操做:用傳給 reduce 中的函數 function(有兩個參數)先對集合中的第 一、2 個元素進行操做,獲得的結果再與第三個數據用 function 函數運算,最後獲得一個結果。 python
reduce() 函數語法: reduce(function, iterable[, initializer]) 參數 function -- 函數,有兩個參數 iterable -- 可迭代對象 initializer -- 可選,初始參數
返回值:函數計算結果數組
>>>def add(x, y) : # 兩數相加 ... return x + y ... >>> reduce(add, [1,2,3,4,5]) # 計算列表和:1+2+3+4+5 15 >>> reduce(lambda x, y: x+y, [1,2,3,4,5]) # 使用 lambda 匿名函數 15