python---內建函數

來源:互聯網
上載者:User

標籤:callable   cto   數字   abc   ems   lob   div   array   對象   

#print(abs(-5))                      #取絕對值# print(bin(4) )                     #把數字轉成二進位格式# print(bool())                      #判斷真假,0為假,1為真,空位假,非空為真# print(chr(98))                    #輸入ascill碼中的數字返回相應的字元# print(ord("a"))                   #輸入ascill碼中的字元返回相應的數字# __import__("模組名")              #以字串的格式匯入模組# print(divmod(5,3))                #返回商和餘數# enumerate()                       #可列出清單索引號#oct(23)                            #將十進位轉為八進位# pow(3,2)                          #返回3的2次方# reversed()                        #反轉# round(1.3342,2)                   #小數點後保留兩位# print(max([1,2,3,4]))             #返回最大值# print(min([1,2,3,4]))# print(callable(test))             #判斷是否可調用,如函數,類可調用。。# a = frozenset(set([1,4,7,34,45,33,33,34]))          #將集合變為不可修改# print(globals())                  #以key-vlaue的形式返回當前程式的所有的全域變數--值# print(locals())                   #以key-vlaue的形式返回當前程式的所有的局部變數--值# print(hash("fxl"))                #將字串轉化成一個hash值,python 中字典就是運用hash演算法,每一個key都對應了一個hash值# print(hex(255) )                  # 將一個數字轉化為16進位# print(id(a))                      #返回記憶體位址#----------------------#a = [-5,0,6]                 #零為假,非零為真# print(all(a))               # 可迭代對象中所有值為真返回Ture,否則返回False。空返回Ture# print(any(a))               #可迭代對象中的值只要有一個為真,返回Ture,否則False。空返回False# print(ascii(a))             #將可迭代對象變成字串格式# print(dir(a))               #查看資料對象有哪些方法#----------------------# c = bytes("abcde",encoding="utf-8")        #將字串轉化為bytes類型,不能修改# d = bytearray("abcde",encoding="utf-8")    #將字串轉化為bytes類型,可以修改# d[1] = 50# print(d)#----------------------code = '''import sys,timefor i in range(20):    sys.stdout.write("#")       #標準輸出到螢幕    sys.stdout.flush()          #輸出一個立即重新整理    time.sleep(0.1)'''exec(code)                           #exec 可以將字串轉化為可執行檔代碼#----------------------dic = '''{"name1":"cx","name2":"fxl"}'''print(eval(dic))                            #將字串變成字典#----------------------#匿名函數# calc = lambda n:print(n)                 #匿名函數,只能進行一些簡單的操作# calc(5)# calc = lambda n:3 if n>5 else 4# print(calc(6))#匿名函數結合內建方法使用#res = filter(lambda n:n>5,range(10))       #過濾出大於5的列表#res = map(lambda n:n*2,range(10))          #過濾出所有值#res = map(lambda n:n>5,range(10))          #符合條件Ture,不符合為false# for i in res:#     print(i)# import functools# res = functools.reduce(lambda x,y:x+y,range(10))        #累加# print(res)#----------------------# a = {2:4,8:16,1:4,4:32,-5:3}# print(sorted(a.items()))                                 #將字典變為列表,在按照key排序# #[(-5, 3), (1, 4), (2, 4), (4, 32), (8, 16)]# print(sorted(a.items(),key=lambda x:x[1]))                #將字典變為列表,在按照vlaue排序# #[(-5, 3), (1, 4), (2, 4), (8, 16), (4, 32)]#----------------------# a = [1,2,3,4]# b = ["a","b","c","d"]# c = zip(a,b)                                              #將兩個列表組合,按最少的那個列表組合# for i in c:#     print(i)# # (1, 'a')# # (2, 'b')# # (3, 'c')# # (4, 'd')


python---內建函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.