#!/usr/bin/env python __author__ = "lrtao2010" #python3.7 內置函數整理 #abs(x) #返回數字的絕對值。 參數可以是整數或浮點數。 如果參數是復數,則返回其大小 # print(abs(1)) # print(abs(-1)) # print(abs(-1.234)) # 1 # 1 # 1.234 #all(iterable) #如果一個iterable(可迭代對象)的所有元素都為true(或者iterable為空),則返回True。 # a = all([1]) # b = all([]) # c = all([1,[],0]) # d = all(('a','b','c',)) # e = all(('',)) # f = all(('a','',)) # print(a,b,c,d,e,f) # True True False True False False #any(iterable) #如果一個iterable的任一元素為true,則返回True。 如果iterable為空,則返回False。 # a = any([1]) # b = any([]) # g = any([0]) # c = any([1,[],0]) # d = any(('a','b','c',)) # e = any(('',)) # f = any(('a','',)) # print(a,b,g,c,d,e,f) # True False False True True False True #repr(object) #class str(object='') #class str(object=b'', encoding='utf-8', errors='strict') #將任意值轉換為字符串 #repr() 轉化為供解釋器讀取的形式,得到的字符串通常可以用來重新獲得該對象 #str() 用於將值轉化為適於人閱讀的形式, # obj ='I love Python3.7' # print(repr(obj)) # print(str(obj)) # print(obj == eval(repr(obj))) # 'I love Python3.7' # I love Python3.7 # True #ascii(object) #這個函數跟repr()函數一樣,返回一個可打印的對象字符串方式表示。 # 當遇到非ASCII碼時,就會輸出\x,\u或\U等字符來表示。 # 與Python 2版本里的repr()是等效的函數。 # print(ascii('a'), ascii(10), ascii('u\中')) # 'a' 10 'u\\\u4e2d' #bin(x) #十進制轉二進制 # a = bin(16) # b = bin(-16) # print(a,b) # 0b10000 -0b10000 #hex() #十進制轉十六進制 # a=hex(16) # b=hex(-16) # print(a,b) # 0x10 -0x10 #oct() #十進制轉八進制 # a=oct(16) # b=oct(-16) # print(a,b) # 0o20 -0o20 #class bool([x]) #返回一個布爾值,即True或False之一。 # 使用標准真值測試程序轉換x。 如果x為false或省略,則返回False; 否則返回True # a = bool(0) # b = bool(1) # c = bool(-1) # d = bool("") # print(a,b,c,d) # False True True False #breakpoint(*args, **kws) #Python3.7帶有一個名為breakpoint()的內置函數,它在調用站點時進入調試器。 #雖然它是相同的結果,但它更直觀和慣用。 # def divide(divisor, dividend): # breakpoint() #出現異常后在(Pdb)后輸入atgs(a),可以打印變量的具體值 # return dividend / divisor # # if __name__ == '__main__': # print(divide(0, 4000)) # > e:\python\learning\app\module_and_functions\built_in_functions.py(84)divide() # -> return dividend / divisor # (Pdb) args # divisor = 0 # dividend = 4000 # (Pdb) #bytearray() #返回一個可變byte數組 #bytes() #返回一個新的 bytes 對象,該對象是一個 0 <= x < 256 區間內的整數不可變序列。 # 它是 bytearray 的不可變版本 #chr() #用一個范圍在 0~1114111(0x10ffff)整數作參數,返回一個對應的Unicode字符。 # a=chr(98) # print(a) # b #ord() #返回一個字符串的Unicode 編碼 # a=ord('b') # print(a) # 98 #callable() #檢查對象是否可以被調用 #############################類方法################################ #classmethod() #是用來指定一個類的方法為類方法,沒有此參數指定的類的方法為實例方法 # delattr() # 用於刪除屬性 #getattr() #用於返回一個對象屬性,或者方法 #hasattr() #確定一個對象是否具有某個屬性。 #setattr() #將值賦給屬性的 #issubclass() #issubclass(class,classinfo) #判斷參數 class 是否是類型參數 classinfo 的子類。 #property() #作用是在新式類中返回屬性值 #super() 函數是用於調用父類(超類)的一個方法 ################################################################### #compile() #將一個字符串編譯為字節代碼 #complex() 返回一個復數 # a=complex(1,3) # print(a) # (1+3j) #dict() 生成字典 # a=dict() # b=dict(age=1,name='test') # print(a) # print(b) # {} # {'age': 1, 'name': 'test'} #dir() #不帶參數時,返回當前范圍內的變量、方法和定義的類型列表; # 帶參數時,返回參數的屬性、方法列表。 # 如果參數包含方法__dir__(),該方法將被調用。 # 如果參數不包含__dir__(),該方法將最大限度地收集參數信息。 # a=dir(tuple) # print(a) # ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index'] #help() #查看幫助信息 # a=help(tuple) # print(a) # class tuple(object) # | tuple(iterable=(), /) # | # | Built-in immutable sequence. # | # | If no argument is given, the constructor returns an empty tuple. # | If iterable is specified the tuple is initialized from iterable's items. # divmod() 返回兩個數的商和余數 # k,v = divmod(10,3) # print(k,v) # 3 1 #enumerate() #用於將一個可遍歷的數據對象(如列表、元組或字符串)組合為一個索引序列, # 同時列出數據和數據下標,一般用在 for 循環當中 #enumerate(sequence, [start=0]) # a=['name','age'] # for k,v in enumerate(a): # print(k,v) # 0 name # 1 age #eval() # 功能:將字符串str當成有效的表達式來求值並返回計算結果。 # 語法: eval(source[, globals[, locals]]) -> value # 參數: # source:一個Python表達式或函數compile()返回的代碼對象 # globals:可選。必須是dictionary # locals:可選。任意map對象 # a = '7*8' # b=eval(a) # print(b) # 56 # a='{"name":"n"}' # b=eval(a) # print(a,type(a),b,type(b)) # {"name":"n"} <class 'str'> {'name': 'n'} <class 'dict'> # exec() # exec函數和eval函數類似,也是執行動態語句, # 只不過eval函數只用於執行表達式求值,而exec函數主要用於執行語句塊 # exec('a = 1 + 2') # print(a) # 3 # eval('a = 1 + 2') # print(a) # 3 # eval('a = 1 + 2') # File # "<string>", line # 1 # a = 1 + 2 # ^ #filter() #接收一個函數和一個list,這個函數的作用是對每個元素進行判斷, # 返回 True或 False,filter()根據判斷結果自動過濾掉不符合條件的元素, # 返回由符合條件元素組成的新list。 # def is_odd(x): # return x % 2 == 1 # a = [1,2,3,4,5,6,7,8,9] # b=list(filter(is_odd,a)) # print(a,b) # [1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 3, 5, 7, 9] #map()將函數調用映射到每個序列的對應元素上並返回一個含有所有返回值的列表 # a = [1,2,3,4,5,6,7,8,9] # b=list(map(lambda x:x*2,a)) # print(a,b) # [1, 2, 3, 4, 5, 6, 7, 8, 9] [2, 4, 6, 8, 10, 12, 14, 16, 18] #reduce() #reduce函數會對參數序列中元素進行累積, #在Python 3里,reduce()函數已經被從全局名字空間里移除了, # 它現在被放置在fucntools模塊里,用的話要先引入 # from functools import reduce # a = [1,2,3,4,5,6,7,8,9] # b=reduce(lambda x,y:x+y,a) # print(a,b) # [1, 2, 3, 4, 5, 6, 7, 8, 9] 45 #float()將數字或者可轉換成數字的字符串轉換成浮點數 # a=float(10) # b=float('10') # print(a,b) # 10.0 10.0 #format()字符串格式化 # goods_c={'car':1200,'apple':200} # for goods_n, goods_p in goods_c.items(): # print('{:^7}'.format(goods_n), '{:<9}'.format(goods_p)) # car 1200 # apple 200 #set()集合(可變) #frozenset() 不可變集合 # a=set(range(11)) # b=frozenset(range(11)) # print(a,type(a)) # print(b,type(b)) # {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} <class 'set'> # frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) <class 'frozenset'> # globals() # 返回一個全局變量的字典,包括所有導入的變量 # print(globals()) # locals() #返回一個局部變量的字典 #vars() #對象object的屬性和屬性值的字典對象, #如果沒有參數,就打印當前調用位置的屬性和屬性值 類似 locals() #hash() 返回一個不可變數據類型的hash 值 # test_hash = hash('test') # print(test_hash) # -4403578415847662099 #id()獲取對象的內存地址 # a = 'test' # print(id(a)) # 30823904 #input()接受一個標准輸入數據,返回為 string 類型。 # a = input(">>>") # print(a,type(a)) # >>>123 # 123 <class 'str'> #int() int(x, base=10) # 基數默認為10.有效基數為0和2-36。 # 基數0表示將字符串中的基數解釋為整數文字。 # a=int(1) # b=int('0x120',0) # c=int('11',16) # d=int('12',20) # print(a,b,c,d) # 1 288 17 22 #type()一個參數返回對象類型, 三個參數,返回新的類型對象。 # a = 123 # t_a=type(a) # t_b=type(a) == int # print(t_a,t_b) # <class 'int'> True #isinstance() #isinstance() 判斷一個對象是否是一個已知的類型,類似 type() # a = 123 # is_a=isinstance(a,int) # is_b=isinstance(a,str) # is_c=isinstance(a,(int,str,tuple)) # print(is_a,is_b,is_c) # True False True #iter() #用來生成迭代器 #next() #next(iterator[, default]) # iterator -- 可迭代對象 # default -- 可選,用於設置在沒有下一個元素時返回該默認值, # 如果不設置,又沒有下一個元素則會觸發 StopIteration 異常。 #len() 返回長度 #list() 生成列表 #max() 返回最大值 #min() 返回最小值 # a=[10,34,5,67,] # a_max=max(a) # a_min=min(a) # print(a_max,a_min) # 67 5 #memoryview() #返回給定參數的內存查看對象(Momory view) #object() #基類 #open() #打開打開一個文件進行處理 #pow() #Equivalent to x**y (with two arguments) or x**y % z (with three arguments) # a = pow(2,3) # b = pow(2,3,3) # print(a,b) # 8 2 #print() #print(self, *args, sep=' ', end='\n', file=None) # objects - - 復數,表示可以一次輸出多個對象。輸出多個對象時,需要用, 分隔。 # sep - - 用來間隔多個對象,默認值是一個空格。 # end - - 用來設定以什么結尾。默認值是換行符 \n,我們可以換成其他字符串。 # file - - 要寫入的文件對象。 # print('www','google','com',sep='.') # www google com # www.google.com #range()創建一個整數列表 #range(start, stop[, step]) # for i in range(10,0,-2): # print(i) # 10 # 8 # 6 # 4 # 2 #reversed()返回一個反轉后的迭代器 # a = [1,2,3,4,5] # b = reversed(a) # print(a,list(b)) # [1, 2, 3, 4, 5] [5, 4, 3, 2, 1] #sorted() 升序排序 # a = [1,2,5,4,3] # b= sorted(a) # print(a,b) # [1, 2, 5, 4, 3] [1, 2, 3, 4, 5] #round() #返回浮點數的四舍五入值 # a = round(3.545) # print(a) # 4 #slice() #實現切片對象,主要用在切片操作函數里的參數傳遞。 # m_s=slice(0,10,2) # print(m_s) # m_list=[0,1,2,3,4,5,6,7,8,9,10] # print(m_list[m_s]) # print(m_list[0:10:2]) # slice(0, 10, 2) # [0, 2, 4, 6, 8] # [0, 2, 4, 6, 8] #staticmethod()返回函數的靜態方法 #sum()求和 #sum(iterable[, start]) # # iterable - - 可迭代對象,如:列表、元組、集合。 # start - - 指定相加的參數,如果沒有設置這個值,默認為0。 # # a=sum([1,2,3]) # b=sum([1,2,3],1) # c=sum([1,2,3],2) # print(a,b,c) # 6 7 8 #zip() #用於將可迭代的對象作為參數,將對象中對應的元素打包成一個個元組, # 然后返回由這些元組組成的列表。 #如果各個迭代器的元素個數不一致,則返回列表長度與最短的對象相同, # 利用 * 號操作符,可以將元組解壓為列表, # a= [1,2,3] # b= [4,5,6,7] # c=list(zip(a,b)) # d=list(zip(*zip(a,b))) #返回一個矩陣 # print(c) # print(d) # # [(1, 4), (2, 5), (3, 6)] # [(1, 2, 3), (4, 5, 6)] # __import__() # 函數用於動態加載類和函數 。 # 如果一個模塊經常變化就可以使用 __import__() 來動態載入。 # import sys # __import__('a') # 導入 a.py 模塊