Python的內建函數

來源:互聯網
上載者:User

標籤:python

bool():判斷真假

In [51]: bool(None) #None為假Out[51]: FalseIn [52]: bool()     #空為假Out[52]: FalseIn [53]: bool([])Out[53]: FalseIn [55]: bool(())     #空列表,空元組都為假Out[55]: FalseIn [58]: bool(0)     #1是真,0是假Out[58]: FalseIn [59]: bool(1)Out[59]: True

all():都為真,才為真。否則就為假

In [49]: all([1,2])Out[49]: TrueIn [50]: all([1,0])Out[50]: FalseIn [61]: all(‘inter0‘)  #字串是真的Out[61]: True

any():只要有1個為真,就為真

bin():轉換成二進位

In [68]: bin(2)Out[68]: ‘0b10‘In [69]: bin(22)Out[69]: ‘0b10110‘

random模組 產生隨機數

In [70]: import randomIn [71]: random.random()Out[71]: 0.31270530531536544In [72]: random.random()Out[72]: 0.9740460087518201In [76]: random.randint(1,100)    #產生範圍之內的數字Out[76]: 69

enumerate()

In [78]: l = [‘x‘,‘y‘,‘z‘]In [80]: el = enumerate(l)In [83]: for i in el:    ...:     print(i)    ...:     (0, ‘x‘)(1, ‘y‘)(2, ‘z‘)In [84]: for i in enumerate(l,1):    ...:         ...:     print(i)    ...:     (1, ‘x‘)(2, ‘y‘)(3, ‘z‘)In [85]: for i,item in enumerate(l,1):    ...:         ...:     print(i,item)    ...:         ...:     1 x2 y3 z

map() 

接收2個參數,前面一個是函數名,後一個是列表(只能是列表?)

In [86]: l = [1,2,3,4,5]In [87]: new_l = map(lambda x:x+100,l)In [88]: new_lOut[88]: <map at 0x7f456449af28>In [89]: list(new_l)Out[89]: [101, 102, 103, 104, 105]

filter()函數

In [92]: lOut[92]: [1, 2, 3, 4, 5]In [94]: def func(x):    ...:     if x > 2:    ...:         return True    ...:     else:    ...:         return False    ...:     In [95]: nw = filter(func,l)In [96]: list(nw)Out[96]: [3, 4, 5]

reversed() 反轉

round() 四捨五入




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.