內置函數介紹:html
內置參數官方詳解 https://docs.python.org/3/library/functions.html?highlight=built#ascii python
下面是本人對經常使用內置函數的理解:函數
print(abs(-1)) #絕對值 1
print(all([1, 2, 3, 4, 5])) #True
print(all(['', 1, 2])) #False #bool爲False的狀況0、 空、None
print(bin(12345), type(bin(12345))) #0b11000000111001 <class 'str'>
print(chr(97)) #按照ascll表將數字轉換成字符 a
print(ord("a")) #按照ascll表將字符轉換成數字 97
print(divmod(10, 3)) #返回(商,餘數) (3, 1)
print(id(1)) #查看內存地址
print(isinstance("123", int)) #False
print(type("123") is str) #判斷數據類型 True
print(len("123")) #3
print("123".__len__()) #計算長度 3
print(pow(2, 3)) #2的3次方 8
print(pow(2, 3, 3)) #前兩個參數是求次方,後一個參數是取餘運算 2
print(type(repr(1234))) #將1234轉成字符串 <class 'str'>
print(type(str(1234))) #<class 'str'>
l = [1, 2, 3] for i in reversed(l): #倒着打印
print(i) print(round(3.5)) # 四捨五入 4
print(round(3.3)) # 3
print(l[0:3:2]) #切片[1, 3]
a = slice(0, 3, 2) print(l[a]) #切片[1, 3]
print(sum([1, 2, 3, 4, 5])) #求和運算 15
print(sum([1, 2, 3, 4, 5]), 100) #能夠設置初始值100, 結果爲115
print(sum([], 100)) #100
print(eval("1+2*3/4")) #將字符串形式的表達式 解析並執行: 2.5
a = "print('hello exec')"
exec(a) #將字符串形式的代碼 解析並執行:hello exec
#compile()#幾乎用不到 #把一個代碼文件加載進來,按exec和eval的方式解析並執行
def Sayhi(): name = "YL"
print(locals()) #打印局部變量 {'name': 'YL'}
print(globals()) #打印全局變量
Sayhi() #------------------------------------------------------------------------------------- #map(function, sequence1, sequence2...)
a = map(lambda x: x*x, range(10)) print(a) #<map object at 0x000001CBF8573A20>
print(list(a)) #[0, 1, 4, 9, 16, 25, 36, 49, 64, 81] #能夠處理多個sequence,實現經過function方法對sequence1,sequence1,..進行處理,這就要求function也支持相應數量的參數輸入:
a = map(lambda x, y, z: x+y+z, range(10), range(10), range(10)) print(list(a)) #[0, 3, 6, 9, 12, 15, 18, 21, 24, 27] #------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------- #reduce(function, sequence, starting_value): #僅能處理一個sequence,starting_value爲初始化值,執行順序爲:先將每一個sequence內部第一個值和starting_value進行function處理, #處理的結果再與sequence第二個值進行function處理,直到結束。 #->reduce的function函數必需要輸入兩個變量才能進行運算
from functools import reduce print(reduce(lambda x, y: x + y, [2, 3, 4, 5, 6], 1)) #21 #結果爲21 執行順序爲---->((((( (1+2) +3)+4)+5)+6))
print(reduce(lambda x, y: x + y, [2, 3, 4, 5, 6])) #20 #-------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------ # filter(function, sequence):對sequence中的item依次執行function(item),只能有1個sequence
a = range(10) b = filter(lambda x: x > 5, a) print(b) #<filter object at 0x00000204ADDC0748>
print(list(b)) #[6, 7, 8, 9]
print(list(filter(lambda x: x+5, a))) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] #filter能夠看做是過濾函數,貌似並不能修改sequence裏面的值
l = [1, 2, 0, 4, 0, 7] print(list(filter(None, l))) #當function是None時,返回的是Ture的item:[1, 2, 4, 7] #-------------------------------------------------------------------------------------
a = list(range(10)) print(a) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = list(range(10, 16)) print(b) #[10, 11, 12, 13, 14, 15]
print(list(zip(a, b))) #[(0, 10), (1, 11), (2, 12), (3, 13), (4, 14), (5, 15)]
#print
msg = "又回到最初的起點" f = open("tofile", "w") print(msg, "記憶中你青澀的臉", sep="|", end="", file=f) #在文件tofile中寫入:又回到最初的起點|記憶中你青澀的臉
import time #老牛逼了
for i in range(10): time.sleep(0.5) #print(".")
print(".", end="", flush=True) # import sys # print("---", vars(sys)) # print("...", dir(sys))
# import time # for n in (100000, 200000, 300000, 400000): # data = b'x'*n # start = time.time() # b = data # while b: # b = b[1:] # print('bytes', n, time.time()-start) # # for n in (100000, 200000, 300000, 400000): # data = b'x'*n # start = time.time() # b = memoryview(data) # while b: # b = b[1:] # print('memoryview', n, time.time()-start)
小結:map、filter、reduce三個函數的區別ui
map函數,能夠有多個sequence參數,而且能夠修改參數sequence中的item的值spa
filter函數,只能有一個sequence參數,而且不能修改參數sequence中的item的值,至關於一個過濾函數3d
reduce函數,只能有一個sequence參數,而且不能修改參數sequence中的item的值,至關於一個累加函數,它的function參數必須有兩個參數傳入code