內建函數,python內建函數

來源:互聯網
上載者:User

內建函數,python內建函數

'''abs()dict()help()min()setattr()all()dir()hex()next()slice()any()divmod()id()object()sorted()ascii()enumerate()input()oct()staticmethod()bin()eval()int()open()str()bool()exec()isinstance()ord()sum()bytearray()filter()issubclass()pow()super()bytes()float()iter()print()tuple()callable()format()len()property()type()chr()frozenset()list()range()vars()classmethod()getattr()locals()repr()zip()compile()globals()map()reversed()__import__()complex()hasattr()max()round() delattr()hash()memoryview()set() '''# 絕對值print(abs(-199.23), 'abs')# 傳入的序列所有元素都為真(序列裡的元素為0,'',None,都是假.字典的話,判斷key)返回真,否則假print(all((1, 2, 3, 4, 5)), 'all')# 傳入的序列裡所有的元素都為假,才返回真,否則假print(any([0, '', None]), any((1, 0, '', None)), 'any')# 返回一個可列印對象的字串a = ascii([1, 2, 3, 4, 5])print(type(ascii(a)), 'ascii')# 返回一個可列印對象的字串print(repr('123'), 'repr')# 判斷對象的邏輯值print(bool(1), bool([]), 'bool')# 將序列,轉換成位元組數組,字串要加編碼(UTF8一個漢字=三個位元組,gkb=兩個位元組)print(bytearray([1, 2, 3, 4, 5]), bytearray(    '張三', encoding='gb2312'), 'bytearray')# 將序列,轉換成位元組字串,字串要加編碼(UTF8一個漢字=三個位元組,gkb=兩個位元組)print(bytes([1, 2, 3, 4, 5]), bytes(    '張三', encoding='gb2312'), 'bytes')# 對象可執行返回真,否則假fun1 = lambda x: x + 10print(callable(fun1), callable("1"), 'callable')# 將ascii碼轉成字元(小於0x110000 或1114112)print(chr(25106), 'chr')# 將單字元轉成ascii碼print(ord('我'), 'ord')# 類方法用# classmethod()# 類方法用# staticmethod()# 類方法用# super()# 類方法用# isinstance()# 類方法用# issubclass()# 類方法用# iter()# 類方法用# property()# 類方法用# slice()# 編譯代碼# compile('print(1)')# 複數形式print(complex(3.141592654), 'complex')# 對象操作# delattr()# 對象操作# getattr()# 對象操作# hasattr()# 對象操作# setattr()# 產生字典類print(dict(k1='1'), 'dict')# 查看類所有方法,為空白則查看當前所有變數名print(dir(), 'dir')# 查看類所有方法,為空白則查看當前所有變數名和值print(vars(list), 'vars')# 取商和餘數print(divmod(10, 3), 'divmod')# 枚舉序列,返回迭代數和序列元素,參數一為序列,參數二為迭代開始值(預設為0)l1 = {'a': 'a1', 'b': 'b1', 'c': 'c1', 'd': 'd1'}for i, m in enumerate(l1, 10):    print(i, m, 'enumerate')# 運算字串運算式,並返回結果print(eval('1+2*5/10'), 'eval')# 運算字串運算式,返回Noneprint(exec('1+2*5/10'), 'exec')# 迴圈將序列的每個元素取出來,傳遞進函數內(函數每次接收一個元素),行進操作後,返回一個新序列,將操作後的每一個元素添加進去def fun1(x):    if x > 4:        x += 5        x - +100        x *= 55    return x# lambda x: x + 5,也可以使用lambda函數print(list(map(fun1, [1, 2, 3, 4, 5, 6, 7])), 'map')# 過濾器,元素傳遞進函數,根據函數體內返回True,在新序列來添加,False的不添加def fun2(x):    if x > 4:        return True    else:        return Falseprint(list(filter(fun2, [1, 2, 3, 4, 5, 6, 7])), 'filter')# 將數字或數字字串轉成浮點數print(float('1.15164'), 'float')# 格式化字串print(format('3.14%d %f %s' % (5, 1.15, '6')))# 轉換到set集合a = set([1, 2, 3])print(a, 'set')# 凍結set集合b = frozenset(a)# b.add(6)#無法使用add和discardprint(b, 'frozenset')# 顯示所有全域變數# print(globals(), 'globals')# hash值print(hash('123456'), 'hash')# 查看協助文檔# print(help(list), 'help')# 10轉16進位print(hex(10), 'hex')# 10轉8進位print(oct(10), 'oct')# 10轉2進位print(bin(10), 'bin')# 查看對象記憶體位址print(id(fun1), 'id')# 輸入內容# print(input("請輸入內容:"),'input')# 轉換到整數print(int('1234'), int(1.345), 'int')# 擷取對象長度print(len('sdfsdfdiruri'), 'len')# 轉換到列表l1 = (1, 2, 4, 5, 6)print(list(l1), 'list')# 顯示局部變數# print(locals(),'locals')# 取最大值print(max(1, 2, 3, 4, ), 'max')# 取最小值print(min(1, 2, 3, 4, ), 'min')# 返回一個 memor vies 對象# print(memoryview())# 開啟檔案,返回一個對象f = open('E:\下載\Python\coding\老男孩\內建函數.py', 'r')print(f, 'open')f.close()# 冪print(pow(3, 3), 'pow')# 列印print('print')# 產生指定範圍迭代器,參數1=起始位置,參數2=結束位置,參數3=間隔(負數為遞減)for i in range(15, 10, -2):    print(i, 'range')# 反轉序列print(list(reversed([1, 2, 3])), 'reversed')# 四捨五入,參數2=保留的小數位,預設為0print(round(1.333, 2), 'round')# 排序,可使用reverse=True 進行反轉print(sorted([1, 5, 6, 2, 3, 4], reverse=True), 'sorted')# 轉換到字串print(str(1), 'str')# 求整形序列的和print(sum([10, 2, 3, 4, 5, 76]), 'sum')# 轉換到元祖print(tuple([1, 2, 3]), 'tuple')# 查看物件類型print(type('234'), 'type')# 將多個序列按順序,合并成一個新序列a = (1, 2, 3,)b = ['a', 'b', 'c', ]c = {'k1': 'k1', 'k2': 'k2', 'k3': 'k3'}nzip = list(zip(a, b, c))# [(1, 'a'), (2, 'b'), (3, 'c')]print(nzip, 'zip')

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.