內建函數的表格與詳情

來源:互聯網
上載者:User

一、內建函數表格

詳細資料

二、內建函數詳情

2.1 abs(x)

返回絕對值

>>> abs(-5)5

  

2.2 all(iterable)

如果這個可迭代的元素都為真,就返回true。非0就為真,負數也為真,空也為真

>>> all([-1,2,3,4,5])True>>> all((-1,2,3,4))True>>> all([])True>>> all([-1,0,2,3,4])False

  

2.3 any(iterable)

可迭代的元素中,有一個為真,則返回真,空列表返回假。

>>> any([-1,0,1,2,3])True>>> any([])False>>> any([0])False>>> any([1])True

  

2.4 ascii(object)

把記憶體對象變成一個可列印的字串格式

>>> ascii([1,2,3,4])'[1, 2, 3, 4]'

2.5 bin(x)

把一個整數轉換為位元

>>> bin(11111)'0b10101101100111'>>> bin(-1223)'-0b10011000111'>>> bin(1.2)Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: 'float' object cannot be interpreted as an integer

  

2.6 boll([X])

不為空白則為真,反之為假;判斷正確為真,錯誤為假

>>> bool([1,2,3,4])True>>> bool([])False>>> bool("1")True>>> bool("sfasfsa")True>>> bool("")False>>> bool(-1)True>>> bool(0)False>>> bool()False>>> bool({})False>>> bool({"sdf":1})True>>> bool(())False>>> bool((1,2))True>>> bool(3>5)False>>> bool(3<5)True

  

2.7 bytearray([source[,encoding[,errors]]])

位元組數組,並且可以修改二進位的位元組

>>> b=bytearray("abcd",encoding="utf-8")>>> b[0]     # 列印第一個元素的ascii值97>>> b[0]=100   # 修改第一個元素的ascii值,賦值只能是ascii值>>> bbytearray(b'dbcd')

  

2.8 bytes([source[, encoding[, errors]]])

字串轉換成位元組

>>> b=bytes("abcd",encoding="utf-8")>>> bb'abcd'>>> b[0]97>>> b[0]=100Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: 'bytes' object does not support item assignment

  

2.9 callable(object)

判斷一個對象是否可以被調用,只有在後面有括弧的,表示可以調用,比如:函數、類

>>> callable([])False>>> def bus():pass...>>> callable(bus)True

  

2.10 chr(i)

通過ascii的值,找到對應的字元

>>> chr(99)'c'

2.11 ord(c)

根據字元,找到對應的ascii值

>>> ord("c")99

  

2.12 dict(**kwarg)、dict(mapping,**kwarg)、dict(iterable, **kwarg)

產生一個字典

#傳入非固定關鍵字參數>>> dict(name="bigberg",age=22){'name': 'bigberg', 'age': 22}# 傳入列表>>> s_list=[("name","bigberg"),("age",22)]>>> dict(s_list){'name': 'bigberg', 'age': 22}>>> n_list=[['names',['zhangsan','lisi','wangwu']],['job',['doctor','teacher','police']]]>>> dict(n_list){'names': ['zhangsan', 'lisi', 'wangwu'], 'job': ['doctor', 'teacher', 'police']}

  

2.13 dir(object)

查看方法

dir(list): 查看列表的方法

dir(dict): 查看字典的方法

  

2.14 divmod(a,b)

地板除,獲得一個元組,元組第一個元素是商,第二個元素是餘數。

>>> divmod(14,3)(4, 2)

  

2.15 enumerate(iterable,start=0)

擷取一個列表,列表中的每個元素都是一個元組,元組的第一個數是iterable的索引,第二個數是iterable的元素。

fruits = ['apple', 'orange', 'banana']print(list(enumerate(fruits)))#輸出[(0, 'apple'), (1, 'orange'), (2, 'banana')]

  

2.16 eval(expression, globals=None, locals=None)

把字典類型的字串變成字典,把一個整數類型的字元變成int類型,或者加減乘除這種簡單轉換成運算式。

>>> s = "5+989">>> eval(s)994

  

聯繫我們

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