精心總結python全部的內置函數,戳進來補習一下!

取絕對值
a = abs(-95) print(a) 值有一個爲假,就全爲假 a = all([True,True,False]) print(a) 有一個爲真,就全爲真 a = any([False,True,True]) print(a) 返回一個可打印的對象字符串方式表示 a = ascii('0x\10000') b = ascii('b\x19') print(a,b) 將整數轉換成二進制字符串 a= bin(95) print(a) 將以個數據轉換成8進制 a = oct(95) print(a) 將一個數據轉成十進制 a = int(95) print(a)
將整數轉換成十六進制字符串 a = hex(95) print(type(a)) 轉爲布爾類型 a = bool("") print(a) 轉換成字節形式bytes a = bytes("吳永聰",encoding='utf-8') print(a) chr返回一個字符串,其ascii碼是一個整形,好比chr(97)返回字符串穿"a",桉樹i的範圍在0-255之間。 a = chr(88) print(a) ord參數是一個ascii字符,返回值是一個對應的十進制整數 a = ord("X") print(a) 建立數據字典 ret = dict({"wyc":2,"two":3}) dict(zip(("one","two"),(2,3))) dict(one=2,two=1) print(ret) dir列出某個類型的全部可用方法 a = dir(list) print(a) help查看幫助文檔 ret = help(list) print(ret) 分別取商和餘數 a = divmod(9,5) print(a) #計算表達式的值 a = eval('1+2*5') print(a) exec用來執行存儲在字符串或文件中的python語句 ret = exec(print("Hi,girl.")) print(ret) filter 過濾 li = [1,2,3,4,5,6,7] a = filter(lambda x:x>3,li) print(a) float浮點型 a = float(1) print(a) 判斷對象是否是屬於int實列,返回True和False a = 5 b = isinstance(a,int) print(b) globals 返回全局變量 locals 返回當前局部變量 name = "wyc" def h1(): a = 1 print(locals()) h1() print(globals()) map遍歷序列,對序列中每一個元素進行操做,最終獲取新的序列 li = [11,22,33] def func1(arg): return arg + 1 new_list = map(func1,li) for i in new_list:print(i) max 返回集合中的最大值 max min 返回集合中的最小值 min a = [1,2,3,4,5,6,7] s = max(a) n = min(a) print(s,n) pow 返回x的y次冪 a = pow(2,10) print(a) t = pow(2,10,100) print(t) round 四捨五入 a = round(9.5) print(a) sorted 隊集合排序 char = ["吳","123","1","25","a","b","c"] new_chat = sorted(char) print(new_chat) for i in new_chat: print(bytes(i,encoding='utf-8')) sum求和的內容 a = sum([1,2,3,4,5]) print(a) a = sum(range(6)) print(a) __import__經過字符串的形式,導入模塊 comm = input("Please:") ccas = __import__(comm) ccas.f1() #須要作拼接時後加 fromlist=True m = __import__("lib."+comm,fromlist=True) print(m)
相關文章
相關標籤/搜索